首页 文章

使用coord_flip()在ggplot2条形图中的图例条目顺序

提问于
浏览
10

我正在努力在我用R中的ggplot2制作的图表中获得正确的变量排序 .

假设我有一个数据帧,例如:

set.seed(1234)
my_df<- data.frame(matrix(0,8,4))
names(my_df) <- c("year", "variable", "value", "vartype")
my_df$year <- rep(2006:2007)
my_df$variable <- c(rep("VX",2),rep("VB",2),rep("VZ",2),rep("VD",2))
my_df$value <- runif(8, 5,10) 
my_df$vartype<- c(rep("TA",4), rep("TB",4))

产生下表:

year variable    value vartype
1 2006       VX 5.568517      TA
2 2007       VX 8.111497      TA
3 2006       VB 8.046374      TA
4 2007       VB 8.116897      TA
5 2006       VZ 9.304577      TB
6 2007       VZ 8.201553      TB
7 2006       VD 5.047479      TB
8 2007       VD 6.162753      TB

有四个变量(VX,VB,VZ和VD),属于两组变量类型(TA和TB) .

我想将值绘制为y轴上的水平条, ordered vertically first by variable groups and then by variable names ,以年为面,x轴上的值和对应于变量组的填充颜色 . (即在这个简化的例子中,顺序应该是,从上到下,VB,VX,VD,VZ)

1)我的第一次尝试是尝试以下方法:

ggplot(my_df,        
    aes(x=variable, y=value, fill=vartype, order=vartype)) +
       # adding or removing the aesthetic "order=vartype" doesn't change anything
     geom_bar() + 
     facet_grid(. ~ year) + 
     coord_flip()

但是,变量按反向字母顺序列出,但不是 vartype :忽略 order=vartype 美学 .

enter image description here

2)在我昨天发布的类似问题的答案之后,我尝试了以下内容,基于帖子Order Bars in ggplot2 bar graph

my_df$variable <- factor(
  my_df$variable, 
  levels=rev(sort(unique(my_df$variable))), 
  ordered=TRUE
)

这种方法确实在图中以垂直字母顺序获取变量,但忽略了变量应该被排序的事实 first by variable goups (TA变量位于顶部,TB变量位于下方) .

enter image description here

3)以下与2(上述)相同:

my_df$vartype <- factor(
  my_df$vartype, 
  levels=sort(unique(my_df$vartype)), 
  ordered=TRUE
)

...与第一种方法有相同的问题(以反向字母顺序列出的变量,忽略的组)

4)另一种方法,基于Order Bars in ggplot2 bar graph的原始答案,也给出了与上面2相同的平台

my_df <- within(my_df, 
                vartype <- factor(vartype, 
                levels=names(sort(table(vartype),
                decreasing=TRUE)))
                )

令我感到困惑的是,尽管采用了几种方法,美学仍然被忽略了 . 不过,它似乎在一个无关的问题上工作:http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking-of-data/

我希望问题很清楚,欢迎任何建议 .

马特奥

我昨天发布了一个类似的问题,但遗憾的是,在解决问题并提供可重复的示例时,我犯了几个错误 . 我已经听了几个建议,并且 thoroughly searched stakoverflow for similar question and applied, to the best of my knowledge, every suggested combination of solutions, to no avail. 我再次发布了这个问题,希望能够解决我的问题,并希望对其他人有所帮助 .

1 回答

  • 10

    这与 ggplot 几乎没有关系,而是关于生成用于重新排序因子级别的变量排序的问题 . 这是您的数据,使用各种功能实现以更好地实现:

    set.seed(1234)
    df2 <- data.frame(year = rep(2006:2007), 
                      variable = rep(c("VX","VB","VZ","VD"), each = 2),
                      value = runif(8, 5,10),
                      vartype = rep(c("TA","TB"), each = 4))
    

    请注意,这种方式 variablevartype 是因素 . 如果它们不是因素, ggplot() 将强制它们然后你按字母顺序排列 . 我以前说过这个,毫无疑问会再说一遍;在开始绘制/进行数据分析之前,将数据转换为正确的格式 first .

    您需要以下订购:

    > with(df2, order(vartype, variable))
    [1] 3 4 1 2 7 8 5 6
    

    你应该注意到我们首先按 vartype 进行排序,然后在 vartype 的级别内按 variable 进行排序 . 如果我们使用它来重新排序 variable 的级别,我们得到:

    > with(df2, reorder(variable, order(vartype, variable)))
    [1] VX VX VB VB VZ VZ VD VD
    attr(,"scores")
     VB  VD  VX  VZ 
    1.5 5.5 3.5 7.5 
    Levels: VB VX VD VZ
    

    (忽略 attr(,"scores") 位并关注级别) . 这有正确的顺序,但 ggplot() 将从下到上绘制它们,你想要从上到下 . 我不太熟悉 ggplot() 知道这是否可以控制,所以我们还需要在调用 order() 时使用 decreasing = TRUE 来反转排序 .

    把这一切放在一起我们有:

    ## reorder `variable` on `variable` within `vartype`
    df3 <- transform(df2, variable = reorder(variable, order(vartype, variable,
                                                             decreasing = TRUE)))
    

    当与您的绘图代码一起使用时:

    ggplot(df3, aes(x=variable, y=value, fill=vartype)) +
           geom_bar() + 
           facet_grid(. ~ year) + 
           coord_flip()
    

    产生这个:

    reordered barplot

相关问题