I have a barplot made primarily in ggplot2. I want two vertical lines added and some text below the x-axis.
#Load data
d <- structure(list(author = structure(c(1L, 2L, 4L, 3L, 5L, 6L, 8L, 11L, 13L, 12L, 10L, 9L, 7L), .Label = c("Bahr et al", "Fuller et al", "Garbossa et al", "Gokhale et al", "Iuchi et al", "Lee et al", "Lee Y et all", "Merrel et al", "Newton et al", "Rossetti et al", "Usery et al", "Wychowski et al", "Zachenhofer et al"), class = "factor"), nAE = c(-22L, -34L, -158L, -90L, -70L, -41L, -48L, -32L, -73L, -23L, -25L, -13L, -46L), AE = c(3L, 1L, 7L, 1L, 3L, 10L, 3L, 6L, 3L, 5L, 4L, 6L, 5L), SAE = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 2L, 0L, 0L)), .Names = c("author", "nAE", "AE", "SAE"), class = "data.frame", row.names = c(NA, -13L))
Code to my barplot:
library(dplyr)
library(tidyr)
library(ggplot2)
categories <- c("Adverse Effect", "No adverse effects", "Severe side effects")
cols <- c("#f6766d", "#01bfc4", "orange")
q <- d %>%
gather(key, value, -author) %>%
ggplot(aes(author, value, fill = key)) +
geom_col(alpha=0.9) +
scale_x_discrete(name="Author") +
scale_y_continuous(name="Number of observations", limits=c(-160,15),
seq(-160, 15, by=10)) +
theme_grey() +
theme(legend.position = "top") +
scale_fill_manual(labels = categories, values = cols) +
labs(fill = "")
I have attached a picture below of how I want my barplot to look like. As you can see, I have added two vertical lines (at random position) and three texts (in photoshop).
Thanks in advance,
C.
2
Answers
Will
gridExtra
package be of any help? It should deliver something close enough. You can combine 3 plots into 1 using methodarrangeGrob
orgrid.arrange
.https://cran.r-project.org/web/packages/gridExtra/index.html
The vertical lines are no problem at all. Simply use:
The values 3.5 and 10.5 mean that the lines intercept the x-axis between the third and fourth and respective between the tenth and eleventh author.
Adding text outside the plot is a whole different beast though. The “cleanest” way I could think of is adding the text inside the plot:
EDIT: Just found a relatively easy way to add text outside the plot here. But I don’t think it’s a very nice solution: