【问题标题】:Reordering levels in stacked bar pplot in ggplot2, without count data重新排序ggplot2中堆积条形图pplot中的级别,没有计数数据
【发布时间】:2014-04-25 19:56:37
【问题描述】:

我正在使用 ggplot2 生成平均值(不是计数)的堆积条形图。这适用于以下代码:

new.stop <- read.delim("New_stop_data.txt")

A <- ggplot(new.stop, aes(x=Consonant, y=(Duration*1000), fill = Label)) + stat_summary(fun.y="mean", geom="bar") + coord_flip() + theme_bw() + ylab("Duration (ms)") + xlab("Stop")
A + scale_fill_grey()

原则上以这种方式绘制东西可以正常工作并产生以下plot。但是,“投票”和“关闭”级别的顺序应该颠倒,投票在右边。我尝试通过使用 relevel() 和分别使用 factor() 来解决这个问题,如下所示:

new.stop$Label <- relevel(new.stop$Label, ref="vot")
new.stop$Label <- factor(new.stop$Label, levels = c("vot", "closure"), labels=c("VOT", "Closure"))

但是,正如您所见,这只会产生barplot where both values are merged。我什至尝试在 ggplot2 中重新排序,但无济于事。由于此数据不是计数数据(并且 ggplot2 假设您只会将计数数据用于条形图),因此我无法找到任何似乎在这里有用的帮助。非常感谢任何帮助。

PS:就我所做的而言,条形图是典型的。

【问题讨论】:

  • Christian DiCanio 尝试使用guides(... reverse = T)

标签: r ggplot2


【解决方案1】:

当你开始用统计数据做一些花哨的事情时,会有一点深奥的魔力。或者至少,这是我愚蠢的观点。无论如何,更好的做法是:

  1. 将 geom_bar 与 stat 参数一起使用,而不是相反。
  2. 将 aes(fill) 放在 geom_bar() 中,而不是 ggplot() 函数中。

事物聚合的顺序有一些有趣的事情发生。按照上面的操作,它最初可以按预期工作,不需要重新排序级别或任何东西:

ggplot(new.stop, aes(x=Consonant, y=(Duration*1000))) + geom_bar(stat="summary", fun.y = mean, aes(fill = Label)) + coord_flip() + theme_bw() + ylab("Duration (ms)") + xlab("Stop") + scale_fill_grey()

FWIW,这就是我个人倾向于避免统计数据的原因。当我先做数据汇总(使用 plyr 或 dplyr 之类的)然后 ggplot 汇总表时,我总是会得到更可预测的结果。

【讨论】:

  • 是的!这行得通。谢谢你。我的另一位同事刚刚建议在绘图之前汇总我的数据,这似乎也有效。因此,我们可以得出的一个结论是 stat_summary() 函数不允许您重新排序级别(至少不容易)。因此,如果您使用它,所有关于重新排序的建议都会消失。
  • 但是,如果尝试使用 facet_wrap(~Speaker),留在 geom_bar(stat="summary", fun.y = mean, aes(fill = Label)) 将导致非常奇怪的行为。如果有人使用聚合函数(正如朋友现在建议的那样),它可以正常工作,例如

    plotdata

【解决方案2】:

对不起。我在访问用户提供的数据后编辑了我的答案,几小时后。

从提供的数据来看,我先看看它应该如何排列,数字。

dat

head(subset(dat, select = c(Consonant, Label, Duration)))

  Consonant   Label    Duration
1       /p/ closure 0.127097695
2       /p/     vot 0.007426832
3       /p/ closure 0.128724749
4       /p/     vot 0.006942377
5       /p/ closure 0.116493725
6       /p/     vot 0.007421805

总结一下

ddply(dat, .(Consonant, Label), summarise, length(Duration*1000))

  Consonant   Label   D
1       /k/ closure 119
2       /k/     vot 119
3      /kw/ closure  75
4      /kw/     vot  75
5       /p/ closure 117
6       /p/     vot 117
7       /t/ closure 116
8       /t/     vot 116

情节计数

ggplot(dat, aes(x=Consonant), y=(Duration*1000)) + 
      geom_bar(aes(fill = Label), stat="bin") + 
      coord_flip() +
      theme_bw() + 
      labs(list = c(x="Duration (ms)", y='Stop')) +
      scale_fill_grey()+
      guides(fill = guide_legend(reverse = TRUE))

和手段

ddply(dat, .(Consonant, Label), summarise, D = mean(Duration*1000))

  Consonant   Label         D
1       /k/ closure 108.35499
2       /k/     vot  26.52917
3      /kw/ closure 122.28891
4      /kw/     vot  31.46828
5       /p/ closure 135.90023
6       /p/     vot  11.27705
7       /t/ closure 124.73526
8       /t/     vot  16.91066

ggplot(dat, aes(x=Consonant, y=(Duration*1000))) + 
  geom_bar(stat="summary", fun.y = mean, aes(fill = Label)) + 
  coord_flip() + 
  theme_bw() + 
  labs(list(y="Duration (ms)", x='Stop')) +
  scale_fill_grey() +
  guides(fill = guide_legend(reverse = TRUE))

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-11-01
  • 2014-07-07
  • 1970-01-01
  • 2017-08-04
  • 2012-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多