0

I have difficulties producing a barplot in ggplot2. I have followed the instructions but I am not so sure in programming yet.

library(ggplot2)

## Load my data
d <- structure(list(author = structure(c(1L, 2L, 4L, 3L, 5L, 6L), .Label = c("Bahr et al", "Fuller et al", "Garbossa et al", "Gokhale et al", "Iuchi et al", "Lee et al"), class = "factor"), nAE = c(22L, 34L, 158L, 90L, 70L, 41L), AE = c(3L, 1L, 7L, 1L, 3L, 10L), SAE = c(0L, 1L, 0L, 0L, 0L, 0L)), .Names = c("author", "nAE", "AE", "SAE"), class = "data.frame", row.names = c(NA, -6L))

## This is as far as I get
ggplot(data=d, aes(x=nAE, y=author, fill=AE))

I want my plot to look like this: enter image description here

My data consists of

head(d)
          author nAE AE SAE
1     Bahr et al  22  3   0
2   Fuller et al  34  1   1
3  Gokhale et al 158  7   0
4 Garbossa et al  90  1   0
5    Iuchi et al  70  3   0
6      Lee et al  41 10   0

This is six studies on side effects of drug A. d$nAE is the number of people not having side effects, d$AE represents patients having adverse effects whereas d$sAE represents patients having severe side effects.

I want my plot to illustrate these three values per study. To compare to the attached ggplot, the red color would visualize d$nAE, the green color d$AE and the blue color d$sAE.

Thanks in advance, C.

cmirian
  • 2,572
  • 3
  • 19
  • 59

1 Answers1

2

We can do the following with dplyr, tidyr, and ggplot2:

library(dplyr)
library(tidyr)
library(ggplot2)

d <- structure(list(author = structure(c(1L, 2L, 4L, 3L, 5L, 6L), .Label = c("Bahr et al", "Fuller et al", "Garbossa et al", "Gokhale et al", "Iuchi et al", "Lee et al"), class = "factor"), nAE = c(22L, 34L, 158L, 90L, 70L, 41L), AE = c(3L, 1L, 7L, 1L, 3L, 10L), SAE = c(0L, 1L, 0L, 0L, 0L, 0L)), .Names = c("author", "nAE", "AE", "SAE"), class = "data.frame", row.names = c(NA, -6L))

categories <- c("Adverse Effect", "No adverse effects", "Severe side effects")
cols <- c("#E61515", "#105415", "#5121E0")

d %>% 
  gather(key, value, -author) %>% 
  ggplot(aes(author, value, fill = key)) +
  geom_col() + 
  coord_flip() +
  theme_bw() +
  theme(legend.position = "top") +
  scale_fill_manual(labels = categories, values = cols) +
  labs(fill = "Authors")

enter image description here

To choose your own colors, RStudio has a convenient addin called Colour Picker that you may want to check out.

tyluRp
  • 4,678
  • 2
  • 17
  • 36
  • Yes - thank you! If i can request some extra features: 1) how can I change ggtheme (to like theme_bw())? 2) Can I get a self-chosen text in stead of the word "key" in top legend? 3) How can I re-label AE to "Adverse Effect", nAE to "No adverse effects" and SAE to "Severe side effects" and lastly, 4) How can I change the colors to selv-chosen RGB-colors? (Like #FFFFFF). Thank you! – cmirian Jan 18 '18 at 12:26
  • You are really good at this :) Thanks. Would it be possible to add a percentage in the middle of the bar for each study showing how many percent of the patients experienced side effects? That would be (AE+sAE)/(nAE+AE+sAE)*100 – cmirian Jan 18 '18 at 13:04
  • Okay - last question, I promise! Can I apply an opacity through the alpha argument (or in any other ways)? Lets say I want a bar with a color + 50% opacity. Thanks! – cmirian Jan 18 '18 at 14:30
  • Hi tyluRp. I hope that its okay Im writing to you here. I have posted a question I think you might know the answer to - hope you will look into to it :) https://stackoverflow.com/questions/48488123/how-can-i-add-vertical-lines-and-horizontal-text-out-of-the-plot-axis-limits-in – cmirian Jan 28 '18 at 15:50