I am trying to plot several graphs, labelled "A", "B", "C" in the top left corner. I can't figure out (or find) a solution to add this textfield like label. Ideally, this would expand the entire size to the left. Any help is appreciated!
The "A" is what I would like to add, the plot part is what I have already.
Here are an example dataset and my current code:
Day=rep(1:6, times=15)
Colour=c("Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Blue","Blue","Blue","Blue","Blue","Blue",
"Purple","Purple","Purple","Purple","Purple","Purple",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow")
Values=rep(c(9,8,7,6,5,8,7,6,5,4,7,6,5,4,3), times=6)
Fruit=rep(c("Apple", "Banana", "Blueberry","Mango", "Melon", "Pear"),
times = c(18,18,12,18,18,6))
Data <-data.frame(Day, Fruit, Colour, Values) %>%
mutate(unten=Values-0.2, oben=Values+0.2) %>% filter(Day==1)
design <- theme( strip.background = element_blank(),
panel.background = element_blank(), panel.border=element_blank(),
panel.grid.major = element_blank(), strip.placement = "outside",
axis.line.x = element_line(colour = "white"),
axis.ticks.x=element_blank(), legend.position = "none",
axis.text.x=element_text(vjust=5), axis.title=element_text(size=15),
axis.title.y = element_text(
margin = margin(t = 0, r = 15, b = 0, l = 0)), strip.text = element_text(size = 15), axis.text=element_text(size=11),
plot.title = element_text(size = 18, face = "bold"))
ForPlot <- ggplot(Data, aes(Colour, Values), fill=Colour)
ylines <- c(0, 5, 10)
ForPlot + theme_classic() +
geom_hline(yintercept = ylines, colour= "lightgrey" )+
scale_y_continuous(breaks=ylines,
limits=c(0,10),
labels=ylines
)+
geom_col(position=position_dodge2 (preserve = "single"),
aes(fill=`Colour`),width=.7)+
ylab("Values") + xlab("Colour") +
ggtitle("Title")+
facet_grid(~`Fruit`, scales = "free_x", space = "free_x", switch = "y") +
design +
scale_fill_manual("Colour", values=c('Blue', 'Green', 'Purple','Red', 'Yellow'))+
geom_text(aes(label = Values, y = Values), size=3, vjust = -.3) +
geom_segment(aes(x=0.4,xend=Inf, y=0, yend=0), color="black")
You can create some space around the plot with theme(plot.margin = ...
then simply add a textGrob over your completed plot.
# plot as above ... +
theme(plot.margin = margin(30, 30, 30, 30))
library(grid)
grid.draw(grid.text(label = "A",
x = unit(0.05, "npc"),
y = unit(0.9, "npc"),
gp = gpar(fontsize = 30)))
Hi, thanks for your reply. How do I add the grid.draw to my plot, or how does textGrob work? I tried the [plot.....+grid.draw] but nothing happened.
@BeccaLi after you have drawn your plot, just call the function as it is above. You don't add it to the plot - you call this function, which draws over your plot
Okay, understood. In this case, how can I save the plot afterwards? So far I assigned the plot to "p" and used ggsave(plot, file="..")
@BeccaLi just the way you would save a base R plot. Call
png("my_file.png")
THEN draw your plot and annotation, THEN dodev.off()
and your file will be right there. You can set the dimensions of the plot inpng
as well as other options. Or, instead of saving to png format, you can save topdf()
orsvg()
if you prefer. If you're in R studio you can also use the "export" button in the plot window to save the file as you'd like.