-6

I am trying to create a bar graph displaying the percentage of PLE and their corresponding 95% credible intervals. I would like to have age groups in the horizontal line and the percentages in the vertical line and Non_C, CG(<14h/w), CG (>=14h/w) as the bars. Given that I have around 20 tables, I was wondering if R codes can create my graphs of interest. I would like to compare the PLEs and 95% credible intervals around them (min, max) across age groups.

The below code converts the below table into the data frame.

    structure(list("Caregiving status" = c("Non-C", "Non-C","Non-C","CG<14h/w", "CG<14h/w","CG<14h/w","CG>=14h/w","CG>=14h/w","CG>=14h/w"),

  Age = c("50-51", "60-61", "70-71", "50-51", "60-61", "70-71","50-51", "60-61", "70-71"), 

                 PLE = c(78,78,78,81,81,80,78,78,77),
                 Min = c(78,77,76,79,79,78,75,74,74),
                 Max = c(79,79,79,83,83,83,80,80,80)), 
               class = "data.frame", row.names = c(NA, -9L))

enter image description here

Nader Mehri
  • 514
  • 1
  • 5
  • 21
  • 1
    What code have you tried so far? Can provide an example of your expected result? You give good detail on what you want, but it's unclear how best to help you! – OTStats Mar 11 '20 at 01:09
  • 1
    Do you have any sample data? Any code attempted? – r2evans Mar 11 '20 at 01:11

1 Answers1

1

The following code gives you a simple bar chart with errobars. Not really nice yet, but as starting point ... To adjust the width of the errorbars, ... have a look at the documentation of position_dodge2.

library(ggplot2)

df <- structure(list(caregiving_status = c("Non-C", "Non-C","Non-C","CG<14h/w", "CG<14h/w","CG<14h/w","CG>=14h/w","CG>=14h/w","CG>=14h/w"),

               age = c("50-51", "60-61", "70-71", "50-51", "60-61", "70-71","50-51", "60-61", "70-71"), 

               ple = c(78,78,78,81,81,80,78,78,77),
               min = c(78,77,76,79,79,78,75,74,74),
               max = c(79,79,79,83,83,83,80,80,80)), 
          class = "data.frame", row.names = c(NA, -9L))

ggplot(df, aes(age, group = caregiving_status)) +
  geom_col(aes(y = ple, fill = caregiving_status), position = "dodge2") +
  geom_point(aes(y = ple), position = position_dodge2(.9)) +
  geom_errorbar(aes(ymin = min, ymax = max), position = position_dodge2(padding = .5)) +
  labs(x = "Age", y = "PLE", fill = "Caregiving Status")

Created on 2020-03-11 by the reprex package (v0.3.0)

stefan
  • 90,330
  • 6
  • 25
  • 51