12

我有一个数据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 ...
4

4 回答 4

5

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

于 2009-11-13T16:07:37.727 回答
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"))

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

闪避顺序不正确:

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")

于 2012-04-16T17:22:06.980 回答
4

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

目标是让标有“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)), ])有效的解释。

于 2009-11-14T19:13:19.927 回答
1

改变因子水平确实改变了躲避柱的顺序!常见误区:颜色仍然停留在某个位置,所以快速浏览一下就好像顺序没有改变一样。但是,如果您查看这些值,您会发现顺序确实发生了变化。

编辑:我之前的回答只改变了给酒吧的配色方案的顺序。这仍然很有用,因为我们可能经常希望在更改条形顺序的同时反转配色方案:

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

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

花了 5 个小时修补不断变化的因素水平并安排数据框,希望这对某人有所帮助!

于 2017-08-04T07:41:31.700 回答