rggplot2graphbar-chartaesthetics

Overlay centralised dots on grouped bar chart using ggplot


I want to overlay centralised dots on grouped bar chart using ggplot, but I could not achieve that. Any help will be appreciated. Below is my code:

# Dummy data
dummy_df <- data.frame(facility = rep(c("GP", "Hospital", "Other"), each = 2),
                 enrolled = rep(c("Enrolled", "Unenrolled"), 3),
                 completed = c(3500,80,70,20,110,20),
                 per = c(0.93,0.85,0.82, 0.85, 0.50, 0.41))


# Bar-chart
ggplot(dummy_df) +
     geom_bar(aes(x = facility, y = per, group = enrolled, fill = enrolled),
              stat = "identity", position = "dodge") +
     
     geom_point(data = dummy_df, 
                aes(x = facility, y = completed/4000, fill = enrolled), size = 3,
                position = position_jitterdodge(0.3), stat="identity",
                alpha = 0.8) +
     
     labs(y = "Percentage", x = "\nFacility") +
     

     #using the scales package use the label percent to change the decimal to %
     scale_y_continuous(labels = scales::label_percent(),
                        limits = c(0,1), 
                        breaks=c(0, 
                                 0.2, 
                                 0.4, 
                                 0.6, 
                                 0.8,
                                 1.0),
                        # Add a second axis and specify its features
                        sec.axis = sec_axis(trans=~.*4000, name="Dose volume\n")) +
     # guides(fill = "none") + 
     theme(axis.text=element_text(size=12),
           legend.text=element_text(size=12),
           axis.title=element_text(size=14),
           legend.justification='left',
           legend.margin = margin(-0.2,0.5,0.1,0.1, unit="cm"),
           
           # remove title
           plot.title = element_blank(),
           
           # y-axis title
           axis.title.y.left = element_blank()
     ) 

plot as per my code but this is not what I want.

What I want is that the black dots (which is Dose volume) on each bar is centralised.

Expected output


Solution

  • Instead of using position_jitterdodge(), you can use position_dodge() with the same width as the bars to ensure the points align in the middle of the bars for each group.

    Chart

    library(ggplot2)
    library(scales)
    
    # Dummy data 
    dummy_df <- data.frame(facility = rep(c("GP", "Hospital", "Other"), each = 2),
                           enrolled = rep(c("Enrolled", "Unenrolled"), 3),
                           completed = c(3500,80,70,20,110,20),
                           per = c(0.93,0.85,0.82, 0.85, 0.50, 0.41))
    
    # Bar-chart
    ggplot(dummy_df) +
      geom_bar(aes(x = facility, y = per, group = enrolled, fill = enrolled),
               stat = "identity", position = position_dodge(width = 0.9)) +
      
      geom_point(data = dummy_df, 
                 aes(x = facility, y = completed/4000, fill = enrolled), size = 3,
                 position = position_dodge(width = 0.9), stat = "identity",
                 alpha = 0.8) +
      
      labs(y = "Percentage", x = "\nFacility") +
      
    
      scale_y_continuous(labels = label_percent(),
                         limits = c(0, 1), 
                         breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1.0),
                         # Add a second axis and specify its features
                         sec.axis = sec_axis(trans = ~ . * 4000, name = "Dose volume\n")) +
      
      theme(axis.text = element_text(size = 12),
            legend.text = element_text(size = 12),
            axis.title = element_text(size = 14),
            legend.justification = 'left',
            legend.margin = margin(-0.2, 0.5, 0.1, 0.1, unit = "cm"),
            
      )