2

I would like to put the annotation "a)" on the left had side of the y-axis of my plot. I have read that I should be using "custom_annotation", but the closest I have got is as follows:

#create dataframe for an example plot

df <- data.frame(matrix(c(1:3, 6:8, 11:13), nrow = 1))
names(df) <- paste0(rep(LETTERS[1:3], each = 3), 1:3)

sdf <- stack(df)

sdf <- transform(sdf, group = substring(ind, 1, 1),
             obs = substring(ind, 2))

#example plot
require(ggplot2)
plt <- ggplot(sdf, aes(x = group, y = values)) +
  geom_boxplot()+
annotation_custom(
  grob = textGrob(label = "a)", hjust = 0, gp = gpar(cex = 1.5)),
  ymin = 12.5,      
  ymax = 12.5,
  xmin = 0.2,         
  xmax = 1)
plt

When I try to change the x min/x max to a negative number (i.e. -0.2) my annotation just disappears rather than shifting to the left of the y-axis. Any help would be greatly appreciated!

Amanda
  • 107
  • 2
  • 10
  • 1
    what about this `plt <- ggplot(sdf, aes(x = group, y = values)) + geom_boxplot()+ theme(plot.margin = unit(c(0,0,0,1), "cm"))+ annotation_custom( grob = textGrob(label = "a)", gp = gpar(cex = 1.5)), ymin = 10, xmin = -3) gt <- ggplotGrob(plt) gt$layout$clip[gt$layout$name=="panel"] <- "off" grid.draw(gt)` – shiny Sep 11 '16 at 07:02
  • 2
    You need to turn off clipping. – Axeman Sep 11 '16 at 07:04
  • Thanks everyone. I had read the questions above that my question is marked as a duplicate of and their code didn't work for me initially... but it turns out that's because I didn't realise I'd failed to copy across the part about turning off clipping. Whoops, silly mistake! – Amanda Sep 21 '16 at 02:21

0 Answers0