Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using plotmath symbol in ggplot2 geom_text - legend is altered - why?

Tags:

r

ggplot2

I want to position a plotmath symbol (x bar) in a plot using ggplot2. Somehow the way I do it alters the legend. The letter "a" suddenly appears. Where do I go wrong here?

d <- data.frame(x=rnorm(10), y=rnorm(10), g=rep(c("m", "w"), 5))
ggplot(d, aes(x, y, group=g, color=g)) + geom_point() +
    geom_text(x=0, y=0, label="bar(x)", parse=T)

like image 648
Mark Heckmann Avatar asked Sep 11 '25 14:09

Mark Heckmann


1 Answers

This will fix the problem:

ggplot(d, aes(x, y, group = g)) +   
  geom_point(aes(colour = g)) + 
  geom_text(x = 0, y = 0, label = "bar(x)", parse=T)

Only add colour the points.

Or, if you want to annotate the plot, annotations will not be placed in the legend so

ggplot(d, aes(x, y, group = g,colour = g)) +   
  geom_point() + 
  annotate('text',x = 0, y = 0, label = "bar(x)", parse=T)

would work.

like image 112
mnel Avatar answered Sep 14 '25 06:09

mnel