更改 ggplot2 barplot 中躲避条的顺序

发布于 2024-08-11 07:16:51 字数 1423 浏览 1 评论 0原文

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

I have a dataframe df.all and I'm plotting it in a bar plot with ggplot2 using the code below. I'd like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "Singular" come before the bars labeled "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"))

I've tried doing levels(df.all$number) = c("S", "P") thinking that maybe ggplot uses the order of the levels to decide plotting order, but that didn't work. I'm not sure what else to try. Any ideas?

The contents of df.all, in case it's useful:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滿滿的愛 2024-08-18 07:16:51

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

I think df.all$number needs to be an ordered factor. Try df.all$number <- ordered(df.all$number)

在风中等你 2024-08-18 07:16:51

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

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="身份",position="堆栈")
在此处输入图像描述

闪避顺序不正确:

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

In some cases I don't think this is possible:

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

correct order in stacked:
g+geom_bar(stat="identity",position="stack")
enter image description here

incorrect order in dodge:

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

enter image description here

let's try to reverse the order in 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")

no dice

let's try to reorder the data frame

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

nope

独留℉清风醉 2024-08-18 07:16:51

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

目标是使标有“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")

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

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

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

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

Hadley has provided a solution. Here's a replication of the problem and the solution.

The goal is to get the bars labeled "S" to come before the bars labeled "P". This doesn't happen by default because R orders levels alphabetically.

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

As Hadley commented in another answer, "you need to reorder based on the x variables, not the y variable". Though I'm not sure why this works.

To flip the order of the factors in this example, you can convert the factor to numeric and multiply by -1.

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

I'd still like some more explanation about why df <- with(df, df[order(gram, -as.numeric(number)), ]) works.

久而酒知 2024-08-18 07:16:51

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

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

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

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

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

Changing factor levels really does change the order of dodged bars! Common pitfall: the colors still stay at a certain position, so taking a quick glance makes it look like the order has not changed. But if you look at the values you will see that the order really has changed.

Edit: My previous answer below only changes order of color scheme given to bars. This is still useful, as we may often want to reverse the color scheme at the same time as changing the order of the bars:

I was using scale_fill_manual because I wanted to manually fill the colors of my bars.

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

Spent 5 hours tinkering with changing factor levels and arranging the dataframe hopes this helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文