繁体   English   中英

改变ggplot2 barplot中躲避条的顺序

[英]Changing the order of dodged bars in ggplot2 barplot

我有一个数据帧df.all ,我正在使用下面的代码在ggplot2的条形图中绘制它。 我想这样做,以便翻转躲闪的酒吧的顺序。 也就是说,标记为“奇异”的条形在标记为“复数”的条形之前。

ggplot(df.all, aes(gram, V1, fill=number)) + 
    geom_bar(stat="identity", position="dodge") + 
    scale_x_discrete(labels=c("Grammatical","Ungrammatical")) +
    scale_y_continuous(formatter="percent", limits=c(0,1)) +
    facet_grid(. ~ experiment) + 
    scale_fill_hue("Attractor", breaks=c("S","P"), labels=c("Singular","Plural"))

我已经尝试过做levels(df.all$number) = c("S", "P") ,认为ggplot可能会使用等级的顺序来决定绘图顺序,但这不起作用。 我不知道还有什么可以尝试的。 有任何想法吗?

df.all的内容,以防它有用:

> df.all
  number gram     experiment        V1
1      S    G BERIMBAU_AGR_A 0.8133333
2      S    G BERIMBAU_AGR_B 0.8658537
3      S    U BERIMBAU_AGR_A 0.5436242
4      S    U BERIMBAU_AGR_B 0.4597701
5      P    G BERIMBAU_AGR_A 0.8580645
6      P    G BERIMBAU_AGR_B 0.8536585
7      P    U BERIMBAU_AGR_A 0.3087248
8      P    U BERIMBAU_AGR_B 0.3975904

> str(df.all)
'data.frame':   8 obs. of  4 variables:
 $ number    : Factor w/ 2 levels "S","P": 2 2 2 2 1 1 1 1
  ..- attr(*, "scores")= num [1:2(1d)] 0 -1
  .. ..- attr(*, "dimnames")=List of 1
  .. .. ..$ : chr  "P" "S"
 $ gram      : Factor w/ 2 levels "G","U": 1 1 2 2 1 1 2 2
 $ experiment: Factor w/ 4 levels "BERIMBAU_AGR_A",..: 1 4 1 4 1 4 1 4
 $ V1        : num  0.813 0.866 0.544 0.46 0.858 ...

在某些情况下,我认为这是不可能的:

layerCake<-data.frame(group=c(rep("normal",4),rep("tumor",4)),
                      class=factor(rep(c("exon","intron","intergenic","unmapped"),2),levels=rev(c("exon","intron","intergenic","unmapped")),ordered=TRUE),
                      fraction=c(.02,.25,.50,.23,.015,.20,.555,.23)
)
layerCake[layerCake$group=='normal',"reads"]<-130948403*layerCake[layerCake$group=='normal',"fraction"]
layerCake[layerCake$group=='tumor',"reads"]<-200948403*layerCake[layerCake$group=='tumor',"fraction"]
g<-ggplot(layerCake, aes(x=factor(group),y=reads, fill=factor(class),order = as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))

正确的堆叠顺序:
克+ geom_bar(STAT = “同一性”,位置= “堆叠”) 在此输入图像描述

闪避中的错误顺序:

g+geom_bar(stat="identity",position="dodge")

在此输入图像描述

让我们尝试颠倒ggplot中的顺序:

g<-ggplot(lc, aes(x=factor(group),y=reads, fill=factor(class),order = -as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))
g+geom_bar(stat="identity",position="dodge")

没有骰子

让我们尝试重新排序数据框

lc <- with(lc, lc[order(-as.numeric(class)), ])
g<-ggplot(lc, aes(x=factor(group),y=reads, fill=factor(class),order = -as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))
g+geom_bar(stat="identity",position="dodge")

我认为df.all$number需要是一个有序因子。 尝试df.all$number <- ordered(df.all$number)

哈德利提供了一个解决方案。 这是问题和解决方案的复制。

目标是使标有“S”的条形在标有“P”的条形之前。 默认情况下不会发生这种情况,因为R命令按字母顺序排

df <- read.csv("http://pealco.net/code/ggplot_dodge/df.txt")
ggplot(df, aes(gram, V1, fill=number))
    + geom_bar(stat="identity", position="dodge")

正如哈德利在另一个答案中评论的那样,“你需要根据x变量而不是y变量重新排序”。 虽然我不确定为什么会这样。

要翻转此示例中的因子顺序,您可以将因子转换为数字并乘以-1。

df <- with(df, df[order(gram, -as.numeric(number)), ])

我还是想更多解释为什么df <- with(df, df[order(gram, -as.numeric(number)), ])有效。

改变因子水平确实会改变躲避酒吧的顺序! 常见的陷阱:颜色仍然停留在某个位置,因此快速浏览一下就会使订单看起来没有改变。 但是如果你看一下这些价值,你就会看到订单真的发生了变化。

编辑:我之前的答案仅改变了给出条形图的颜色方案的顺序。 这仍然有用,因为我们可能经常想要在改变条形顺序的同时反转颜色方案:

我使用scale_fill_manual因为我想手动填充我的酒吧的颜色。

ggplot(data, aes_string(x = "countries", y = "population", fill = "agegroups")) +
scale_fill_manual(values = CustomColorFunction(), limits = (levels(data$agegroups)))

花了5个小时来修改因素水平并安排数据框架希望这有助于某人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM