0
votes

I'm trying to create a barplot by month that includes two columns, with each column stacked. For each month, the first column would be the total number of video visits, split by vid_new and vid_return. The second column would be the total number of phone visits, split by phone_charge and phone_nocharge.

I still haven't been able to get the bars side-by-side correct. This code uses the data frame in the second picture and it's counting the instances of the word "video" and "phone", not the Count column resulting in the third picture.

plot <- ggplot(data=new_df, aes(x=Month, y = count, fill = gen_type)) +
 geom_bar(stat = "identity", position = "dodge")

Below is a pic of the data I'm working with. I've converted it into a few different forms to try new methods by have not been able to form this graph.

Dataset

enter image description here

enter image description here

How can I make a barplot by group and by stack in ggplot? What data structure do I need to get make it?

Thanks in advance for your advice!

1

1 Answers

2
votes

You can try any of these options reshaping your data to long and creating and additional variable so that you can identify the types. Here the code using tidyverse functions:

library(ggplot2)
library(dplyr)
library(tidyr)
#Date
df <- data.frame(Month=c(rep('Mar',4),rep('Apr',4),rep('May',2)),
                 spec_type=c('vid_new','vid_return','phone_charge','phone_nocharge',
                             'vid_new','vid_return','phone_charge','phone_nocharge',
                             'vid_new','vid_return'),
                 Count=c(7,85,595,56,237,848,2958,274,205,1079))
#Plot 1
df %>% mutate(Month=factor(Month,levels = unique(Month),ordered = T)) %>%
  mutate(Dup=spec_type) %>%
  separate(Dup,c('Type','Class'),sep='_') %>% select(-Class) %>%
  ggplot(aes(x=Type,y=Count,fill=spec_type))+
  geom_bar(stat = 'identity',position = 'stack')+
  facet_wrap(.~Month,strip.position = 'bottom')+
  theme(strip.placement = 'outside',
        strip.background = element_blank())

Output:

enter image description here

Or this:

#Plot 2
df %>% mutate(Month=factor(Month,levels = unique(Month),ordered = T)) %>%
  mutate(Dup=spec_type) %>%
  separate(Dup,c('Type','Class'),sep='_') %>% select(-Class) %>%
  ggplot(aes(x=Type,y=Count,fill=spec_type))+
  geom_bar(stat = 'identity',position = 'fill')+
  facet_wrap(.~Month,strip.position = 'bottom',scales = 'free')+
  theme(strip.placement = 'outside',
        strip.background = element_blank())

Output:

enter image description here

Or this:

#Plot 3
df %>% mutate(Month=factor(Month,levels = unique(Month),ordered = T)) %>%
  mutate(Dup=spec_type) %>%
  separate(Dup,c('Type','Class'),sep='_') %>% select(-Class) %>%
  ggplot(aes(x=Type,y=Count,fill=spec_type))+
  geom_bar(stat = 'identity',position = position_dodge2(preserve = 'single'))+
  facet_wrap(.~Month,strip.position = 'bottom',scales = 'free')+
  theme(strip.placement = 'outside',
        strip.background = element_blank())

Output:

enter image description here

In order to see by month you can use facet_wrap() and placing labels in a smart way.