如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?

huangapple go评论74阅读模式
英文:

How to create a stacked barplot using Groups to determine Fill and X variable to determine Color (ggplot2)?

问题

I cannot manage to get a stacked barplot using different colors for the fill (same in each column) and for the trim around the columns (different trim for each column) using ggplot2. The fill is defined by the same variable than the group, and the color by the same variable than the x axis.

我无法通过ggplot2来制作一个堆叠条形图,其中使用不同的颜色来填充(每列相同)和用于围绕列的修剪(每列不同的修剪),填充由与组相同的变量定义,颜色由与x轴相同的变量定义。

The fill works, but the color doesn't, as R does not seem to recognize the grouping

填充正常工作,但颜色不正常,因为R似乎无法识别分组。

I am creating a percentual stacked barplot using ggplot2. One variable with three levels defines the Group ("Geology") and another one with four levels the x axis ("Region"). The y axis is percentual, as the four regions have a different number of datapoints and I want to do a visual comparison between them. This is, the resulting plot has 4 columns of the same height (one per region) and each column can be divided into three (or less) segments.

我正在使用ggplot2创建一个百分比堆叠条形图。一个具有三个级别的变量定义了组("Geology"),另一个具有四个级别的变量定义了x轴("Region")。y轴是百分比的,因为这四个区域具有不同数量的数据点,我想要进行视觉比较。也就是说,生成的图具有4个相同高度的列(每个区域一个),每个列可以分为三个(或更少)段。

I want a different filling color for each Group, hence I use "group=Geology, fill=Geology". I also want to have every column encased by one color each, that should be defined by the Region (matching other figures in the same project), so I use "x=Region, color=Region".

我希望每个组有不同的填充颜色,因此我使用"group=Geology, fill=Geology"。我还希望每个列都被一个颜色包围,这个颜色应该由区域定义(与同一项目中的其他图形相匹配),所以我使用"x=Region, color=Region"。

Nevertheless, when I try to plot it, the "color" part doesn't work

然而,当我尝试绘制时,"color"部分不起作用

To be more clear, this is the desired outcome:

为了更清楚,这是期望的结果:

如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?

I am not sure where is my mistake, maybe you can help me

我不确定我的错误在哪里,也许你可以帮助我

Thanks a lot!

非常感谢!

Edit:

Following the code suggested in the comments, I added stat="identity" and a numeric value for y, and that seems to partially solve the issue (I don't understand why!)

根据评论中建议的代码,我添加了stat="identity"和一个数值y,这似乎部分解决了问题(我不明白为什么!)

Nevertheless, instead of producing a single trim surrounding each segment of the columns, as in your example, it generates a number of divisions equal to the number of observations (ie. if there are three cases where the Geology is "Calcareous" in the Region "Iberia", the corresponding portion of the column is divided in three)

然而,与你的示例不同,它不是产生包围每个列段的单个修剪,而是生成与观察数量相等的划分(即,如果存在三种情况,地质学是“钙质”的,地区是“伊比利亚”的,那么相应部分的列会分为三部分)

Edit 2:

The problem was solved in the comments. The solution was to get rid of the "group". Check out the comment section for the explanation.

问题已在评论中得到解决。解决方案是去掉"group"。请查看评论部分以获取解释。

英文:

I cannot manage to get a stacked barplot using different colors for the fill (same in each column) and for the trim around the columns (different trim for each column) using ggplot2. The fill is defined by the same variable than the group, and the color by the same variable than the x axis.
The fill works, but the color doesn't, as R does not seem to recognize the grouping

I am creating a percentual stacked barplot using ggplot2. One variable with three levels defines the Group ("Geology") and another one with four levels the x axis ("Region"). The y axis is percentual, as the four regions have a different number of datapoints and I want to do a visual comparison between them.
This is, the resulting plot has 4 columns of the same height (one per region) and each column can be divided into three (or less) segments.

I want a different filling color for each Group, hence I use "group=Geology, fill=Geology". I also want to have every column encased by one color each, that should be defined by the Region (matching other figures in the same project), so I use "x=Region, color=Region".

ggplot() + 
  geom_bar(data=All_data, position=position_fill(), aes(group=Geology, x=Region, color=Region, fill=Geology), show.legend = TRUE) +
  theme_classic() +
  ggtitle("Geology") +
  scale_y_continuous(name = NULL, labels= NULL) +
  scale_color_manual(values=c("#B71C1C",
                              "#923ffc",
                              "#fce571",
                              "#70C339"))+
  scale_fill_manual(values=c("black",
                             "darkgrey",
                             "lightgrey"))+
  scale_x_discrete(labels=c("Alps","Central Highlands","Central Plains","Iberia"), name=NULL, guide = guide_axis(angle = 45))

Nevertheless, when I try to plot it, the "color" part doesn't work

如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?

And R provides me an error message

如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?

To be more clear, this is the desired outcome:

如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?

I am not sure where is my mistake, maybe you can help me

Thanks a lot!

Edit:

Following the code suggested in the comments, I added stat="identity" and a numeric value for y, and that seems to partially solve the issue (I don't understand why!)

ggplot() + 
  geom_bar(data=All_data, position="fill", stat="identity", aes(fill=Geology, y=1, x=Region, colour=Region), show.legend = TRUE) +
  theme_classic() +
  ggtitle("Geology") +
  scale_y_continuous(name = NULL, labels= NULL) +
  scale_colour_manual(values=c("#B71C1C",
                              "#923ffc",
                              "#fce571",
                              "#70C339"))+
  scale_fill_manual(values=c("#555555",
                             "#999999",
                             "#DDDDDD"))+
  scale_x_discrete(labels=c("Alps","Central Highlands","Central Plains","Iberia"), name=NULL, guide = guide_axis(angle = 45))

Nevertheless, instead of producing a single trim surrounding each segment of the columns, as in your example, it generates a number of divisions equal to the number of observations (ie. if there are three cases where the Geology is "Calcareous" in the Region "Iberia", the corresponding portion of the column is divided in three)

如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?

Edit 2:

The problem was solved in the comments. The solution was to get rid of the "group". Check out the comment section for the explanation

答案1

得分: 1

Sure, here's the translated part:

只需从aes内部删除group美学。这里的group = Geology美学映射意味着您希望在每种地质类型内所有的填充和颜色都相同,但这明确地_不是_您要做的事情。相反,您希望每个区域和地质的唯一组合都有自己独特的颜色和填充组合。

如果删除group美学,这相当于使用默认分组,即分配给颜色和填充美学的变量的所有组合(换句话说,如果我们使用group = interaction(Region, Geology),将获得相同的效果)。

library(ggplot2)

ggplot(All_data) + 
  geom_bar(position = position_fill(), size = 1.5,
           aes(x = Region, color = Region, fill = Geology)) +
  theme_classic() +
  ggtitle("Geology") +
  scale_y_continuous(name = NULL, labels= NULL) +
  scale_color_manual(values=c("#B71C1C", "#923ffc", "#fce571", "#70C339"),
                     guide = 'none') +
  scale_fill_manual(values=c("black", "darkgrey","lightgrey")) +
  scale_x_discrete(labels = c("Alps", "Central Highlands", "Central Plains", 
                              "Iberia"), name = NULL, 
                   guide = guide_axis(angle = 45))

I hope this helps! If you have any more translation needs, please feel free to ask.

英文:

Simply remove the group aesthetic from inside aes. The group = Geology aesthetic mapping here means that you want all fills and colours to be the same within each geology type, but this is explicitly not what you are trying to do. Instead, you wish each unique combination of region and geology to have its own unique combination of colour and fill.

If you remove the group aesthetic, this is equivalent to using the default grouping, which is all the combinations of the variables assigned to the color and fill aesthetics (in other words, we would get the same effect if we used group = interaction(Region, Geology)

library(ggplot2)

ggplot(All_data) + 
  geom_bar(position = position_fill(), size = 1.5,
           aes(x = Region, color = Region, fill = Geology)) +
  theme_classic() +
  ggtitle("Geology") +
  scale_y_continuous(name = NULL, labels= NULL) +
  scale_color_manual(values=c("#B71C1C", "#923ffc", "#fce571", "#70C339"),
                     guide = 'none') +
  scale_fill_manual(values=c("black", "darkgrey","lightgrey")) +
  scale_x_discrete(labels = c("Alps", "Central Highlands", "Central Plains", 
                              "Iberia"), name = NULL, 
                   guide = guide_axis(angle = 45))

如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?


Data used (inferred approximately from plot in question)

All_data <- data.frame(Geology = rep(rep(c('Calcareous', 'Mixed', 'Siliceous'), 
                                         4), times = c(32, 0, 68, 23, 0, 77,
                                                       15, 85, 0, 11, 11, 78)),
                       Region = rep(c("Alps", "Central Highlands", 
                                      "Central Plains", "Iberia"), each = 100))

答案2

得分: 0

在没有可重现的示例的情况下,我已经从 https://r-graph-gallery.com/48-grouped-barplot-with-ggplot2 中适应了一个示例。

# 载入库
library(ggplot2)

# 创建数据集
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)

# 堆叠 + 百分比
ggplot(data, aes(fill=condition, y=value, x=specie, colour = specie)) + 
  geom_bar(position="fill", stat="identity") + 
  scale_colour_manual(values = c("red", "green", "blue", "orange")) +
  scale_fill_manual(values = c("grey28", "grey55", "lightgrey"))

生成的图像如下所示:
如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?

英文:

In the absence of a reproducible example I have adapted one from https://r-graph-gallery.com/48-grouped-barplot-with-ggplot2

# library
library(ggplot2)

# create a dataset
specie &lt;- c(rep(&quot;sorgho&quot; , 3) , rep(&quot;poacee&quot; , 3) , rep(&quot;banana&quot; , 3) , rep(&quot;triticum&quot; , 3) )
condition &lt;- rep(c(&quot;normal&quot; , &quot;stress&quot; , &quot;Nitrogen&quot;) , 4)
value &lt;- abs(rnorm(12 , 0 , 15))
data &lt;- data.frame(specie,condition,value)

# Stacked + percent
ggplot(data, aes(fill=condition, y=value, x=specie, colour = specie)) + 
  geom_bar(position=&quot;fill&quot;, stat=&quot;identity&quot;) + 
  scale_colour_manual(values = c(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;orange&quot;)) +
  scale_fill_manual(values = c(&quot;grey28&quot;, &quot;grey55&quot;, &quot;lightgrey&quot;))

gives
如何使用组确定填充和X变量确定颜色来创建堆叠条形图(ggplot2)?

huangapple
  • 本文由 发表于 2023年7月6日 19:43:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定