Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression containing a comma as annotation on a single facet: is it possible?

Tags:

r

ggplot2

I am interested in plotting a customized expression that contains a comma on a single facet of a plot in ggplot2. I know how to plot expressions on a single facet using a new data frame as follows:

fakedata <- data.frame(x = 1:10, y=runif(10), grp=gl(2,5))

ggplot(fakedata, aes(x=x,y,y)) + geom_point() + facet_grid(. ~ grp) +
   geom_text(data=data.frame(x=5,y=0.5,grp=1,lab='a == 5'), aes(label=lab), parse=TRUE)

expression on one facet

I also know how to plot expressions on multiple facets using annotation_custom, which can accommodate an expression with a comma. But this is not possible to plot on a single facet only.

ggplot(fakedata, aes(x=x,y,y)) + geom_point() + facet_grid(. ~ grp) +
   annotation_custom(grobTree(textGrob(expression(paste(a == 5, ', ', b == 6)), x=.5, y=.5)))

expression with a comma on both facets

But I cannot figure out what to do for the expression with a comma on a single facet, since you cannot store previously defined expressions in a data frame. How can I plot the expression with a comma on a single facet?

like image 817
qdread Avatar asked Feb 05 '16 23:02

qdread


1 Answers

From ?plotmath, you can use the list() operator:

‘list(x, y, z)’: comma-separated list

g0 + geom_text(data=data.frame(x=5,y=0.5,grp=1,lab='list(a == 5, b==6)'),
      aes(label=lab), parse=TRUE)
like image 148
Ben Bolker Avatar answered Sep 28 '22 08:09

Ben Bolker