0

Example figure link: http://www.nature.com/nature/journal/v528/n7580_supp_custom/fig_tab/nature16039_F5.html

I want my data to look as the figurein the link using ggplot. The "PCR+ RDT+" group should be above the x-axis grouped by age groups (called "variable" in the data set). Study site and "PCR+ RDT-" should be below the x-axis in a similar way. My Study sites should replace the prevalence groups of the figure. Values are the percentages calculated.

This is my data.

dat <- read.table(sep = ' ', text = 
'Study_Site Status variable value
AJ PCR+RDT+ "0-1 year" 0.00
AJ PCR+RDT- "0-1 year" 0.00
KA PCR+RDT+ "0-1 year" 0.00
KA PCR+RDT- "0-1 year" 0.00
KI PCR+RDT+ "0-1 year" 0.00
KI PCR+RDT- "0-1 year" 0.88
WE PCR+RDT+ "0-1 year" 0.00
WE PCR+RDT- "0-1 year" 0.00
AJ PCR+RDT+ "1-5 years" 5.69
AJ PCR+RDT- "1-5 years" 2.44
KA PCR+RDT+ "1-5 years" 0.00
KA PCR+RDT- "1-5 years" 0.22
KI PCR+RDT+ "1-5 years" 0.00
KI PCR+RDT- "1-5 years" 2.65
WE PCR+RDT+ "1-5 years" 3.19
WE PCR+RDT- "1-5 years" 2.13', header = TRUE)
Jake
  • 510
  • 11
  • 19
Srinew
  • 3
  • 2

1 Answers1

1

It's not perfect, but maybe start with something like this?

library(tidyverse)

dat <- read_delim(delim = ' ', file = 
'Study_Site Status variable value
AJ PCR+RDT+ "0-1 year" 0.00
AJ PCR+RDT- "0-1 year" 0.00
KA PCR+RDT+ "0-1 year" 0.00
KA PCR+RDT- "0-1 year" 0.00
KI PCR+RDT+ "0-1 year" 0.00
KI PCR+RDT- "0-1 year" 0.88
WE PCR+RDT+ "0-1 year" 0.00
WE PCR+RDT- "0-1 year" 0.00
AJ PCR+RDT+ "1-5 years" 5.69
AJ PCR+RDT- "1-5 years" 2.44
KA PCR+RDT+ "1-5 years" 0.00
KA PCR+RDT- "1-5 years" 0.22
KI PCR+RDT+ "1-5 years" 0.00
KI PCR+RDT- "1-5 years" 2.65
WE PCR+RDT+ "1-5 years" 3.19
WE PCR+RDT- "1-5 years" 2.13'
)

dat_plot <- dat %>% 
    mutate(new_value = if_else(Status == 'PCR+RDT-', -value, value))

dat_lab <- data_frame(x = 0.7, y = c(-0.25, 0.25), label = c('PCR+ RDT+', 'PCR- RDT-'))

ggplot(dat_plot) + 
    aes(x = Study_Site, y = new_value, fill = variable) + 
    geom_bar(stat = 'identity', position = 'dodge') + 
    geom_hline(yintercept = 0, color = 'black', linetype = 'dashed') + 
    geom_text(aes(y = new_value + 0.25 * sign(new_value), 
                  label = if_else(new_value == 0, NA_character_, paste0(abs(new_value), '%'))),
              position = position_dodge(width = 0.8)) + 
    geom_text(data = dat_lab, 
              aes(x = x, y = y, label = label, fill = NA))

ggplot

You may also want to check out these two Stack Overflow threads for inspiration:

Community
  • 1
  • 1
Alexey Shiklomanov
  • 1,592
  • 13
  • 23
  • This would definitely help! All I needed was an idea to start, this is what I was looking for. Thank you so much! Sri – Srinew Apr 12 '17 at 16:05
  • I tried adding more to your arguments and made the plot (wouldn't let me add the picture as my reputation is <10). Now my task is to add the labels right above and below the bars. I've been trying that with the geom_text, but couldn't adjust for the negative bars. Can you suggest anything? I was using 'geom_text(aes(y = new_value +1.5, label = paste0(new_value, '%')), position = position_dodge(width = 0.8))'. I'm guessing something to do with y position. Thanks! – Srinew Apr 12 '17 at 19:09
  • Also is there any way I can add different labels for y-axis above and below x-axis? Positive y-axis as "PCR+ RDT+" Negative y-axis as "PCR+ RDT-" Thankyou! – Srinew Apr 12 '17 at 19:41
  • See my edits. Putting labels outside of `ggplots` is somewhat trickier, but can be accomplished by disabling clipping (e.g. http://stackoverflow.com/questions/9690648/point-clipped-on-x-axis-in-ggplot). – Alexey Shiklomanov Apr 12 '17 at 20:13
  • Perfect, you're awesome! For the Positive and negative y-axis labels, I used ggdraw of cowplot from the link [link] (http://stackoverflow.com/questions/40598672/adding-two-y-axis-titles-on-the-same-axis). Thank you so much! My PI's gonna love this ;) – Srinew Apr 12 '17 at 20:41