1

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.

neilfws
  • 32,751
  • 5
  • 50
  • 63
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Aug 11 '22 at 05:58
  • This said: `position_dodge` will not work as there is no column to dodge by. To this end you have to reshape your data to long. Note also that the `position` is an argument of the geom not of `ggplot()`. – stefan Aug 11 '22 at 06:01

1 Answers1

0

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))

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    Thank you so much! This worked with very minor edits. I needed to keep the longer section of my code for scale_x_discrete and list all the names for it to populate on the graph. I worked on this problem independently for a full day before I posted here and I am so grateful for the quick and helpful responses! – Brittany St. John Aug 11 '22 at 14:28