cowboy
cowboy

Reputation: 661

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. 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. 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. 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). As it is now all of the R.12 and R.3 values are stacked as one graph. 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)

Upvotes: 1

Views: 1605

Answers (2)

Dave2e
Dave2e

Reputation: 24069

Here is another variation, using the "group" option in the aes definition:

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)

enter image description here

Upvotes: 3

desval
desval

Reputation: 2435

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)

enter image description here

Upvotes: 1

Related Questions