0

I would like to 'annotate' a text on the top right hand corner of ggplot2 bar chart that has character for x axis and numeric for y axis. All the documentation I see is that, to annotate a text, both x and y coordinates have to be given numeric value.

Here is an example chart:-

Here is the data frame

df1 <- data.frame( p=c("a","b","c","a","b","c"),
                   v=c(10,9,8,6,5,2),
                   u=c("aa","bb","cc","aa","bb","cc")
                  )

summarized data frame

df2 <- df1 %>% select(p, v) %>% group_by(p) %>% summarise_each(funs(sum))

bar plot

p <-    ggplot(data = df2, aes(p, v, label = v)) +
        geom_bar(stat = "identity", position = "dodge") +
        geom_text(position = position_dodge(.9),  vjust = -1, fontface = "bold", size = 5)

p
Antex
  • 1,364
  • 4
  • 18
  • 35
  • Couple of ways to do it! But here'a nice a new(to me) package https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html – infominer Sep 29 '16 at 16:42
  • a quick look at the example on the URL shows - to annotate, you still need to provide both x and y axis numeric coordinates. – Antex Sep 29 '16 at 16:48
  • Well you got to tell it where to draw the text. Or do you want it to be automatic? and just say top right corner? – infominer Sep 29 '16 at 16:53
  • If you use a date for x axis, for example, you can do this annotate(geom="text",x=as.Date("2014-10-05"), y=25,label="a",fontface="bold") - I was wondering if you could do the same for characters. – Antex Sep 29 '16 at 16:55
  • 1
    Yes you can, have a look at this answer http://stackoverflow.com/a/20226601/2747709 – infominer Sep 29 '16 at 17:02
  • Thanks infominer that helps. But I want to annotate 'any text'. For example, on the chart, annotate("text", x = "c", y = 15) - will permit to type text starting on y =15 and center of where "c" is. – Antex Sep 29 '16 at 17:13

1 Answers1

1

You should be able to do it just putting the location inside of aes(). This worked for me (unless I am misunderstanding your intent):

ggplot(data = df2, aes(p, v, label = v)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(position = position_dodge(.9),  vjust = -1, fontface = "bold", size = 5) +
  geom_text(aes(x = "c", y = 15, label = "Here I am"))

enter image description here

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
  • That is it! Thanks Mark Peterson!. – Antex Sep 29 '16 at 18:56
  • Only other issue is that when I set the 'color' or 'colour' in the aes - no matter what color choice I gave it, it renders in 'red'. Here is the syntax - geom_text(aes(x = "c", y = 18, label = "I am here", color = "darkblue",size = 15, fontface = "bold")) – Antex Sep 29 '16 at 19:24
  • 1
    Set the color outside of the `aes()` if you just want to set the color. Things inside of the `aes` are mapped, in the case of color to a default palette. An alternative, if you want the color to show up in a legend, is to use `scale_color_identity` (Or, to set `col ="Annotation"` in `aes()`, then use `scale_color_manual(values = c("Annotation" = "darkblue")` ) – Mark Peterson Sep 29 '16 at 19:29