ggvis数字类别排序

时间:2016-01-11 20:08:09

标签: r ggvis

我有一个数据框我想创建一个条形图。 X轴将只是快照编号,y轴将是该类别中的观测数。不幸的是,我必须将观察号转换为字符,因此ggvis()可以工作。这打破了排序,因为" 2" > " 10"例如。我无法让两者同时工作。

eachObs <-
structure(list(obs = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
16, 17, 18, 19, 20, 21, 22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 1, 2, 3, 4, 5, 6, 
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
19, 20, 21, 22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15, 16, 17, 18, 19, 20, 21, 22), key = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("Sadness", 
"Neutral", "Contempt", "Disgust", "Anger", "Surprise", "Fear", 
"Happiness"), class = "factor"), value = c(0, 0, 4, 4, 4, 0, 
0, 0, 0, 8, 0, 0, 0, 0, 8, 4, 4, 0, 0, 8, 4, 4, 68, 48, 44, 28, 
40, 28, 40, 36, 36, 40, 40, 44, 44, 40, 40, 44, 36, 36, 48, 40, 
60, 32, 12, 12, 4, 28, 8, 8, 4, 8, 20, 20, 24, 16, 16, 16, 12, 
20, 20, 0, 8, 4, 12, 12, 8, 4, 20, 12, 4, 12, 16, 12, 16, 12, 
8, 8, 4, 4, 8, 0, 16, 8, 4, 4, 4, 8, 0, 4, 8, 0, 4, 8, 4, 4, 
4, 0, 8, 8, 4, 8, 8, 0, 4, 8, 8, 4, 12, 8, 4, 8, 8, 8, 8, 8, 
8, 20, 4, 0, 4, 4, 4, 8, 0, 16, 0, 12, 4, 12, 4, 0, 0, 4, 0, 
4, 0, 4, 4, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 16, 8, 20, 0, 8, 8, 
20, 12, 16, 32, 32, 24, 20, 20, 16, 16, 20, 28, 20, 24, 16, 20, 
20, 20, 8, 4, 28)), .Names = c("obs", "key", "value"), row.names = c(NA, 
-176L), class = "data.frame")
EMO_COLORS <- #used with the colors above
  c("blue", "grey", "darkred",
    "green", "red", "orange",
    "black", "yellow")

使用此数据,会返回错误

eachObs %>% 
    ggvis(x = ~obs, y = ~value, fill = ~key, fillOpacity := 0.5) %>%
    layer_bars() %>%
    scale_nominal("fill",
                  domain = unique(as.character(eachObs[, 'key'])),
                  range = EMO_COLORS)

Error in eval(expr, envir, enclos) : object 'key' not found

这解决了它,但会产生排序问题

eachObs[, 1] <- as.character(eachObs[, 1])
eachObs %>% 
    ggvis(x = ~obs, y = ~value, fill = ~key, fillOpacity := 0.5) %>%
    layer_bars() %>%
    scale_nominal("fill",
                  domain = unique(as.character(eachObs[, 'key'])),
                  range = EMO_COLORS)

2 个答案:

答案 0 :(得分:1)

为了获得更好的订单,您可以尝试:

eachObs[, 1] <- as.ordered(eachObs[, 1])
eachObs %>% 
        ggvis( x = ~obs, y = ~value, fill = ~key, fillOpacity := 0.5) %>%
        layer_bars()

这就是我得到的: enter image description here

答案 1 :(得分:0)

这,就在最终情节之前,而不是as.character()

eachObs[, 1] <- as.factor(eachObs[, 1])