簡體   English   中英

分類數據的 R 數據可視化

[英]R Data Visualization for categorical data

我正在尋找可視化分類數據的方法。

想象一下,我是一個狂熱的觀鳥者,我有一份鳥類清單,我想在俄勒岡州和愛達荷州這兩個不同的州查看並拍攝它們的照片。

我正在尋找一種方法來直觀地表示進度。

我的第一個想法是我想要一個表格,第一列是物種,接下來的兩列是陳述,然后是一個帶有代表進度的顏色的分割方塊。 有點像對角線分割的熱圖,但我很短。 這是一個示例的模型。

在此處輸入圖片說明

其他建議將是最受歡迎的。

這是一個可以使用的示例數據集:

progress <- read.table(header = TRUE, text = "
bird  location  action  progress
osprey  Oregon  view    completed
osprey  Oregon  photo   completed
osprey  Idaho   view    completed
osprey  Idaho   photo   not_yet
white-tailed_kite   Oregon  view    wait_till_spring
white-tailed_kite   Oregon  photo   wait_till_spring
white-tailed_kite   Idaho   view    not_present
white-tailed_kite   Idaho   photo   not_present
bald_eagle  Oregon  view    completed
bald_eagle  Oregon  photo   completed
bald_eagle  Idaho   view    completed
bald_eagle  Idaho   photo   completed")

謝謝你的建議!

三角形可能很難,並且可以使用自定義字形/圖像或通過制作在適當位置繪制三角形多邊形的函數來完成。

更簡單地說,您可能只使用正方形:

ggplot(progress, 
       aes(x = as.numeric(location) + if_else(action == "view", -0.1, 0.1),
           y = bird, 
           fill = progress)) +
  geom_tile(height = 0.2, color = "white", size = 2) +
  annotate("text", x = c(0.95, 1.05), y = 3.2, 
           label = c("view", "photo"), hjust = c(1,0)) +
  scale_x_discrete(limits = unique(progress$location), name = "") +
  scale_fill_manual(values = c("completed" = "olivedrab",
                                "not_present" = "gray70",
                                "not_yet" = "tomato4",
                                "wait_till_spring" = "lightskyblue")) +
  theme_minimal()

在此處輸入圖片說明

可以做得更優雅,但希望這個解決方案能幫助你實現你正在尋找的設計。

## variables related to heatmap squares
sz.square = 0.6
spacer = 0.05
col = c(completed="forestgreen", not_present="gray70", not_yet="orangered4",
        wait_till_spring="skyblue2")
## variables related to plot layout
sz.rowlabels = 3
sz.collabels = 0.2
sz.legend = 4

## plotting functions for heat map triangles
plot.action = c(
    ## plot "viewed"
    view = function(x, y, col) {
        polygon(
            c(
                x - sz.square/2 + spacer,
                x + sz.square/2,
                x + sz.square/2),
            c(
                y + sz.square/2,
                y - sz.square/2 + spacer,
                y + sz.square/2),
            col=col)
    },
    ## plot "photographed"
    photo  = function(x, y, col) {
        polygon(
            c(
                x - sz.square/2,
                x + sz.square/2 - spacer,
                x - sz.square/2),
            c(
                y + sz.square/2 - spacer,
                y - sz.square/2,
                y - sz.square/2),
            col=col)
    })

xlim = c(1 - sz.square - sz.rowlabels,
         length(levels(progress$location)) + sz.square + sz.legend)
ylim = c(length(levels(progress$bird)) + sz.square,
         1 - sz.square - sz.collabels)

## initialize the plot
par(mar=c(1, 1, 1, 1))
plot(c(0,2), c(2,0), type="n", xlim=xlim, ylim=ylim,
     main=NA, xlab=NA, ylab=NA, xaxt="n", yaxt="n",
     asp=1)

## plot heat map
for (i in 1:nrow(progress)) {
    plot.action[[progress$action[i]]](
        as.integer(progress$location[i]),
        as.integer(progress$bird[i]),
        col = col[progress$progress[i]])
}

## add axix labels
text(xlim[1], 1:nlevels(progress$bird), levels(progress$bird), adj=0, cex=2)
text(1:nlevels(progress$location), ylim[2], levels(progress$location),
     adj=c(0.5,0), cex=2)

## legend
text(xlim[2] - sz.legend/2, ylim[2], "Legend", cex=2)
sz.square = 0.25
x.legend = rep(xlim[2] - 5/8*sz.legend, nlevels(progress$progress) + 2)
y.legend = ylim[2] + 1:(nlevels(progress$progress) + 2) * 0.35 + 0.2
plot.action[["view"]](x.legend[2], y.legend[2], col="white")
plot.action[["photo"]](x.legend[1], y.legend[1], col="white")
rect(
    x.legend[3:length(x.legend)] - sz.square/2,
    y.legend[3:length(y.legend)] - sz.square/2,
    x.legend[3:length(x.legend)] + sz.square/2,
    y.legend[3:length(y.legend)] + sz.square/2,
    col=col)

text(x.legend + sz.square, y.legend,
     c("viewed", "photographed", levels(progress$progress)),
     adj=0, cex=1.3)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM