Home > Net >  Trying to separate bars in two overlayed bar charts in ggplot
Trying to separate bars in two overlayed bar charts in ggplot

Time:08-11

I am trying to create a single chart from two created bar charts to show the differences in their distribution. I have both charts merging together, and the axis labels are correct. However, I cannot figure out how to get the bars in each section to be next to each other for comparison instead of overlaying. Data for this chart are two variables within the same DF. I am relatively new to r and new to ggplot so even plotting what I have was a challenge. Please be kind and I apologize if this is a question that has been answered before.

Here is the code I am using:

Labeled <- ggplot(NULL, aes(lab),position_dodge(.5))   ggtitle("Figure 1. Comparison of Distribution of Age of Diagnosis and Age of Feeding Challenges") 
  geom_bar(aes(x=AgeFactor,fill = "Age of Autism Diagnosis"), data = Graph, alpha = 0.5,width = 0.6)  
  geom_bar(aes(x=FdgFactor,fill = "Feeding Challenge Onset"), data = Graph, alpha = 0.5,width=.6) 
  scale_x_discrete(limits=c("0-6months","7-12months","1-1.99","2-2.99","3-3.99","4-4.99","5-5.99","6-6.99","7-7.99","8-8.99","9-9.99","10-10.99")) 
  xlab("Age") 
  ylab("") 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 
  scale_fill_discrete(name = "")

and this is the graph it is creating for me:

enter image description here

I really appreciate any insight. This is my first time asking a question on stack too - so I am happy to edit/adjust info as needed.

CodePudding user response:

The issue is that you plot from different columns of your dataset. To dodge your bars position_dodge requires a column to group the data by. To this end you could reshape your data to long format using e.g. tidyr::pivot_longer so that your two columns are stacked on top of each other and you get a new column containing the column or group names as categories.

Using some fake random example data. First I replicate your issue with this data and your code:

set.seed(123)

levels <- c("0-6months", "7-12months", "1-1.99", "2-2.99", "3-3.99", "4-4.99", "5-5.99", "6-6.99", "7-7.99", "8-8.99", "9-9.99", "10-10.99")

Graph <- data.frame(
  AgeFactor = sample(levels, 100, replace = TRUE),
  FdgFactor = sample(levels, 100, replace = TRUE),
  lab = 1:100
)

library(ggplot2)

ggplot(NULL, aes(lab), position_dodge(.5))  
  ggtitle("Figure 1. Comparison of Distribution of Age of Diagnosis and Age of Feeding Challenges")  
  geom_bar(aes(x = AgeFactor, fill = "Age of Autism Diagnosis"), data = Graph, alpha = 0.5, width = 0.6)  
  geom_bar(aes(x = FdgFactor, fill = "Feeding Challenge Onset"), data = Graph, alpha = 0.5, width = .6)  
  scale_x_discrete(limits = c("0-6months", "7-12months", "1-1.99", "2-2.99", "3-3.99", "4-4.99", "5-5.99", "6-6.99", "7-7.99", "8-8.99", "9-9.99", "10-10.99"))  
  xlab("Age")  
  ylab("")  
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))  
  scale_fill_discrete(name = "")

And now the fix using reshaping. Additionally I simplified your code a bit:


library(tidyr)
library(dplyr)

Graph_long <- Graph %>%
  select(AgeFactor, FdgFactor) %>%
  pivot_longer(c(AgeFactor, FdgFactor))
  
ggplot(Graph_long, aes(x = value, fill = name))  
  geom_bar(alpha = 0.5, width = 0.6, position = position_dodge())  
  scale_fill_discrete(labels = c("Age of Autism Diagnosis", "Feeding Challenge Onset"))  
  scale_x_discrete(limits = levels)  
  labs(x = "Age", y = NULL, fill = NULL, title = "Figure 1. Comparison of Distribution of Age of Diagnosis and Age of Feeding Challenges")  
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

  • Related