5

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?

1 Answer 1

8

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)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.