R 如何使用两个都决定填充的因素创建堆叠条形图?
这是我正在处理的数据R 如何使用两个都决定填充的因素创建堆叠条形图?,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,这是我正在处理的数据 structure(list(NewRegion = c("Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic"
structure(list(NewRegion = c("Northeastern Atlantic", "Northeastern Atlantic",
"Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic",
"Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic",
"Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic",
"Northeastern Atlantic"), New_categories = c("Behaviour", "Behaviour",
"Environmental conditions", "Environmental conditions", "Habitat use",
"Habitat use", "Interaction", "Invasive species", "Management",
"Management", "Movement", "Movement"), New_Water = c("Fresh",
"Marine", "Fresh", "Marine", "Fresh", "Marine", "Fresh", "Fresh",
"Fresh", "Marine", "Fresh", "Marine"), count = c(12L, 17L, 10L,
7L, 5L, 11L, 1L, 12L, 5L, 11L, 18L, 24L)), row.names = c(NA,
12L), class = "data.frame")
这是我当前的绘图代码
ggplot(EAtl, aes(x = New_categories, y = count, fill = New_categories, color = New_Water)) +
geom_bar(position = 'stack', stat = 'identity', color = 'black')+ scale_fill_manual(values = colforplots)+
scale_y_continuous(expand = c(0.01,0.01))+
theme(panel.background = element_rect(fill = 'transparent', colour = NA),
plot.background = element_rect(fill = 'transparent', colour = NA),
panel.grid.minor = element_line(color = 'black'),
panel.grid.major = element_line(color = 'black'),
legend.position = 'none', axis.title.x = element_blank(),
axis.text.x = element_blank(), axis.ticks.x = element_blank(),
axis.text.y = element_text(size = 19, face = 'bold'), axis.title.y = element_blank())
此条形图显示了东北大西洋地区各类别的计数。我添加了一个颜色参数来表示新的水类别。但是,这不会改变任何颜色,只会添加一条线,将每个条分成两条。我想保留颜色方案,但每个条的一部分(比如淡水)都有纹理(即斜线)
我发现了一些关于类似主题的老讨论,但没有具体的答案/更新。这里有几点需要注意,应该可以帮助您:
- 在
中设置ggplot()
美学,然后通过设置color=
在color=“black”
geom\u bar()中覆盖它。如果删除
,您现在将拥有基于“新水”创建的彩色框和图例。这仍然对你没有太大帮助,因为很难区分,但这是问题的一部分color='black'
中不直接支持填充图案。调用了ggplot
,但我无法在我的R版本上安装。看来ggpattern
可能会对您有所帮助geom\u col\u patterned()
- 如果不使用
的话,一个简单的解决方案是使用ggpattern
美学来根据“新水”改变整体颜色强度。这在这里特别有用,因为您只有两个标签:淡水和海水。我将在下面向您展示它的一个实现alpha=
alpha=
美观,只需在代码的ggplot()
行中将color=
更改为alpha=
。alpha的默认变化看起来不大;但是,为了让这一切顺利进行,我不得不做一些修改:
- 删除
参数主题(legend.position=“无”
表示条边的颜色,而不是填充条内的颜色,如果在color
中删除参数geom\u bar
,您将看到差异。在我看来,您已经有了指示类别的x轴,也许您不需要填充不同的类别为了强调这一点,我的建议是color=''black'
。以下是我建议的前两行代码:fill=New\u water
这应该可以很好地工作!我没有意识到alpha参数可以作为一种美学包含在内!ggplot(EAtl,aes(x=New\u categories,y=count,fill=New\u water))+geom\u bar(position='stack',stat'identity')+scale\u fill\u手册(values=colforplots)+…
ggplot(EAtl, aes(x = New_categories, y = count, fill = New_categories, alpha = New_Water)) + geom_bar(position = 'stack', stat = 'identity', color = 'black')+ scale_alpha_manual(values=c(0.8,0.4)) + scale_y_continuous(expand = c(0.01,0.01))+ theme(panel.background = element_rect(fill = 'transparent', colour = NA), plot.background = element_rect(fill = 'transparent', colour = NA), panel.grid.minor = element_line(color = 'gray80'), panel.grid.major = element_line(color = 'gray80'), axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.text.y = element_text(size = 19, face = 'bold'), axis.title.y = element_blank())