简体   繁体   English

你如何在 R 中制作一个堆叠条形图 plot,并在每个条形图上为每个堆叠的变量和 facet wrap 设置误差条?

[英]How do you make a stacked bar plot in R with error bars on each bar for each variable that is stacked, and facet wrap?

I am trying to make a stacked bar plot in R with facet wrap, filled by variable, but am not able to stack the bars.我正在尝试在 R 中制作一个堆叠条形图 plot,其中包含小平面环绕,由变量填充,但我无法堆叠条形图。 So far, I am able to make a box plot but cannot stack the boxes.到目前为止,我能够制作一个盒子 plot 但不能堆叠盒子。

Example data:示例数据:

structure(list(place = c("A", "B", "C", "A", "B", "C", "A", "B", 
"C"), person = c("Mica", "Mica", "Drew", "Sandra", "Tom", "Tom", 
"Nikata", "Beth", "Enrique"), value = c(95L, 96L, 94L, 70L, 78L, 
82L, 85L, 100L, 101L), variable = c("eng", "eng", "sci", "ma", 
"sci", "ma", "ar", "ar", "eng")), class = "data.frame", row.names = c(NA, 
-9L))
ggplot(data, aes(place, value)) +
  geom_boxplot(aes(color = variable))+
  facet_wrap(~person) +
  scale_color_manual(values = c("#3b3294","#f6cca0","#000000")) 

Is it possible to stack the bars instead of them being side by side?是否可以将条堆叠起来而不是并排放置? Thank you!谢谢你!

It seems you're looking for geom_bar and the position argument看来您正在寻找geom_barposition参数

ggplot(data, aes(place, value)) + 
  geom_col(aes(color = variable, fill = variable))

This will create stacked bar plots, while setting position = dodge will create bar plots side by side.这将创建堆叠条形图,而设置position = dodge将并排创建条形图。

ggplot(data, aes(place, value)) + 
  geom_col(aes(color = variable, fill = variable), position = 'dodge')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM