简体   繁体   中英

Missing Custom Annotation Grob in ggplot2

I was trying to add a custom text annotation grob to my plot according to the website http://zevross.com/blog/2014/08/04/beautiful-plotting-in-ra-ggplot2-cheatsheet-3 . I am using this technique so that I can add the text a normalized coordinates. When I add the grob the text does not appear on the plot. I had used this procedure before and it worked like a champ. For the life of me I can not figure out why this one is not working.

Minimal example here:

len = 100
pd = data.frame(x = runif(50)*len - len/2, y = runif(50)*len - len/2)

my_grob = grobTree(textGrob('Some Text', x=0.5,  y=0.5, hjust=0.5, gp=gpar(col='black', fontsize=12, fontface="italic")))

rp = ggplot() + theme_bw() +
  theme(axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank()) +
  theme(panel.grid = element_blank(), panel.border = element_blank()) +
  geom_point(data = pd, aes(x = x, y = y)) +
  coord_equal(xlim = c(-len/2, len/2)*1.1, ylim = c(-len/2, len/2)*1.1) +
  annotation_custom(my_grob)
rp

Can anybody help me to see what I am missing here? I thought it might have been the theme alterations, but when I took them out the problem still persisted. (Yea, after saying that I could remove them from the above to make it more minimal... But I am going to leave it in for full effect). Thanks for any insight.

As was stated in the comment above by RStudent, all I needed to do was choose one of my datasets to feed to the ggplot() constructor. Although this worked, I was hopeful that I would not have to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM