Warm tip: This article is reproduced from stackoverflow.com, please click
ggplot2 r

R: Add Text(field) outside of Plotting Area

发布于 2020-12-14 00:40:46

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.

enter image description here

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")
Questioner
BeccaLi
Viewed
0
Allan Cameron 2020-08-05 00:36

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)))

enter image description here