[英]How to created a grouped and stacked bar chart in R with ggplot2
I am trying to create a combination grouped and stacked by chart in R using ggplot2.我正在尝试使用 ggplot2 在 R 中创建按图表分组和堆叠的组合。 I was able to create the basic chart using the code below, however its still not where I want to be.
我能够使用下面的代码创建基本图表,但它仍然不是我想要的。 I technically want 2 items in the x axis.
从技术上讲,我想要 x 轴上的 2 个项目。 Right now it is displaying each of the types for each period.
现在它正在显示每个时期的每种类型。 What I would like is to have each
type
combination as a separate bar graph.我想要的是将每种
type
组合作为单独的条形图。 For example the R.12
and the R.3
types would be separate bar graphs side by side for each period (while keeping the group
in the facet_grid).例如,
R.12
和R.3
类型将是每个周期并排的单独条形图(同时将group
保留在 facet_grid 中)。 As it is now all of the R.12
and R.3
values are stacked as one graph.因为现在所有的
R.12
和R.3
值都堆叠为一张图。 Hope this makes sense.希望这是有道理的。 Thanks for any help.
谢谢你的帮助。
library(ggplot2)
group <-c("group1","group1","group1","group1","group1","group1","group1","group1","group1","group1","group1","group1","group2","group2","group2","group2","group2","group2","group2","group2","group2","group2","group2","group2")
period <-c("201912","201912","201912","201912","201911","201911","201911","201911","201910","201910","201910","201910","201912","201912","201912","201912","201911","201911","201911","201911","201910","201910","201910","201910")
type <- c("R.12_In","R.12_Out","R.3_In","R.3_Out","R.12_In","R.12_Out","R.3_In","R.3_Out","R.12_In","R.12_Out","R.3_In","R.3_Out","R.12_In","R.12_Out","R.3_In","R.3_Out","R.12_In","R.12_Out","R.3_In","R.3_Out","R.12_In","R.12_Out","R.3_In","R.3_Out")
amount <- c(100,20,50,5,95,25,50,7,97.5,27.5,52.5,9.5,105,25,55,10,100,30,55,12,98.5,28.5,53.5,10.5)
df <- data.frame(group,period, type, amount)
df<- with(df, df[order(period, type, amount),])
ggplot(df, aes(fill= type, y = amount, x= period)) +
geom_bar( stat = "identity") +
scale_y_continuous(labels = scales::percent_format(acurracy=1)) +
scale_fill_manual(values = c("steelblue4", "#999999", "darkorange","lightslategray" )) +
theme_minimal()+
ylab("") +
xlab("") +
facet_grid(~group)
Here is another variation, using the "group" option in the aes definition:这是另一个变体,使用 aes 定义中的“组”选项:
df <- data.frame(group, period, type, amount)
#Create the major grouping variable
df$majortype<-sub("_.+$", "", df$type)
df<- with(df, df[order(period, type, amount),])
ggplot(df, aes(fill= type, y = amount, x= period, group=majortype)) +
geom_col(position="dodge") +
scale_y_continuous(labels = scales::percent_format(acurracy=1)) +
scale_fill_manual(values = c("steelblue4", "#999999", "darkorange","lightslategray" )) +
theme_minimal()+
ylab("") +
xlab("") +
facet_grid(~group)
Not sure I understand correctly.不确定我理解正确。 Something like that:
像这样的东西:
df$type2 <- ifelse(grepl("R\\.12", type), "R.12", "R.3")
ggplot(df, aes(fill= type, y = amount, x= period)) +
geom_bar( stat = "identity") +
scale_y_continuous(labels = scales::percent_format(acurracy=1)) +
scale_fill_manual(values = c("steelblue4", "#999999", "darkorange","lightslategray" )) +
theme_minimal()+
ylab("") +
xlab("") +
facet_grid(~group + type2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.