grouped bar plot

Hello, I face a new question. Since it is the same type of question on graphing grouped bar plot, I just post here.

library(tidyverse)

df.1b <- data.frame(
  stringsAsFactors = FALSE,
             yr_mo = c("2012-01","2012-01","2012-02",
                       "2012-02","2012-03","2012-03","2012-04","2012-04",
                       "2012-05","2012-05","2012-06","2012-06","2012-07",
                       "2012-07","2012-08","2012-08","2012-09","2012-09",
                       "2012-10","2012-10","2012-11","2012-11","2012-12",
                       "2012-12","2013-01","2013-01","2013-02","2013-02",
                       "2013-03","2013-03","2013-04","2013-04","2013-05","2013-05",
                       "2013-06","2013-06","2013-07","2013-07","2013-08",
                       "2013-08","2013-09","2013-09","2013-10","2013-10",
                       "2013-11","2013-11","2013-12","2013-12"),
         treatment = c(0L,1L,0L,1L,0L,1L,0L,1L,
                       0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,
                       0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,
                       0L,1L,0L,1L,0L,1L,0L,1L,0L,1L,0L,1L),
        proportion = c(0.99946050927924,
                       0.999056358193414,1,1,0.999892101855848,0.999934921254718,1,1,1,
                       1,1,1,1,1,1,1,1,0.999967460627359,1,1,
                       0.998165731549417,0.997201613952883,0.994820889080708,
                       0.993947676688793,0.991476046611998,0.990758818169986,
                       0.98597324126025,0.982884289990889,0.976586102719033,
                       0.974814525575947,0.96406991799741,0.960724977222439,
                       0.956517047906776,0.949629051151894,0.947561501942167,
                       0.940192633086034,0.93817436340095,0.929780033840948,
                       0.931700474751834,0.923662631784459,0.923716012084592,
                       0.91751269035533,0.918213206732844,0.911265130808278,
                       0.911739318083729,0.905180268124431,0.906560207164437,
                       0.901047767799037)
)

Now I need to create a figure of the proportion of observations over the 24-month study period, with separate plots on the same graph for the treatment group and the control group.

The idea is that I should have 24 year-months in x axis, and for each year-month, the treatment and control group are grouped.

I was trying the same codes above, but failed. My attempt:

Q1bPlot <- df.1b %>% 
  group_by(yr_mo) %>% 
  ggplot(aes(x = yr_mo, y = proportion, fill = treatment)) + 
  geom_col(position = 'dodge')

print(Q1bPlot)

I don't see the grouped bar here.

Are you after something like this?

df.1b_new <- df.1b %>% 
  group_by(yr_mo) %>% 
  mutate(treatment = factor(treatment)) #the treatment variable has been converted to factors

ggplot(df.1b_new, aes(x = yr_mo, y = proportion, fill = treatment)) + 
  geom_col(position = 'dodge') + facet_wrap(~treatment) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  theme(legend.position = "None")

1 Like

Yes! Thank you so much!

Can I inquire some minor changes?

  1. how to change 0 and 1 on the top of graph to control group and treatment group?
  2. how to change x label from "yr_mo" to "year by month"?

Here:

df.1b_new <- df.1b %>% 
  group_by(yr_mo) %>% 
  mutate(treatment_label = if_else(treatment == 0, "Control Group", "Treatment Group")) # treatment group label

ggplot(df.1b_new, aes(x = yr_mo, y = proportion, fill = treatment_label)) + 
  geom_col(position = 'dodge') + facet_wrap(~treatment_label) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  theme(legend.position = "None") +
  labs(x = "Year by month", y = "Proportion", title = "Put a title here") # the labels

Thank you so much! I really appreciate it!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.