R Визуализация данных для категориальных данных - PullRequest
0 голосов
/ 07 января 2020

Я ищу способы визуализации категориальных данных.

Представьте, что я заядлый птичник и у меня есть список птиц, которых я хочу просмотреть и получить фотографию в двух разных штатах, Орегоне и Айдахо.

Я ищу способ визуально изобразить прогресс.

Сначала я подумал, что мне нужно что-то вроде таблицы, в которой вид в качестве первого столбца будет содержать следующие два вида. столбцы, затем разделенный квадрат с цветами, которые представляют прогресс. Что-то вроде тепловой карты, разделенной по диагонали, но я не выдержу. Вот макет примера.

enter image description here

Другие предложения приветствуются.

А вот пример набора данных работать с:

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")

Спасибо за ваши предложения!

Ответы [ 2 ]

1 голос
/ 07 января 2020

Можно сделать более элегантным, но, надеюсь, это решение поможет вам достичь дизайна, который вы ищете.

## 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)

enter image description here

1 голос
/ 07 января 2020

Треугольники, вероятно, жесткие, и могут быть выполнены с использованием пользовательских глифов / изображений или с помощью функции рисования многоугольника треугольника в нужных местах.

Проще говоря, вы можете просто использовать квадраты:

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()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...