改变ggplot2 barplot中躲避条的顺序

时间:2009-11-12 11:20:49

标签: r ggplot2

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

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 ...

4 个答案:

答案 0 :(得分:4)

我认为df.all$number需要成为有序因素。试试df.all$number <- ordered(df.all$number)

答案 1 :(得分:4)

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

目标是让标有“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)), ])为什么有用的更多解释。

答案 2 :(得分:4)

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

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 = “同一性”,位置= “堆叠”) enter image description here

闪避中的错误顺序:

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

enter image description here

让我们尝试颠倒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")

没了

答案 3 :(得分:1)

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

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

我使用的是scale_fill_manual,因为我想手动填充条形图的颜色。

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

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