改变ggplot2 barplot中躲避条的顺序

pea*_*lco 11 r ggplot2

我有一个数据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"))
Run Code Online (Sandbox Code Playgroud)

我试过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 ...
Run Code Online (Sandbox Code Playgroud)

Joh*_*son 5

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

  • 是的,然后您可以使用选项 `levels=c()` 手动更改顺序 (2认同)

Jer*_*zig 5

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

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"))
Run Code Online (Sandbox Code Playgroud)

正确的堆叠顺序:
g+geom_bar(stat="identity",position="stack") 在此处输入图片说明

闪避顺序不正确:

g+geom_bar(stat="identity",position="dodge")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

让我们尝试颠倒 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")
Run Code Online (Sandbox Code Playgroud)

没有骰子

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

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")
Run Code Online (Sandbox Code Playgroud)


pea*_*lco 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")
Run Code Online (Sandbox Code Playgroud)

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

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

df <- with(df, df[order(gram, -as.numeric(number)), ])
Run Code Online (Sandbox Code Playgroud)

我仍然想要更多关于为什么df <- with(df, df[order(gram, -as.numeric(number)), ])有效的解释。