问题
Using facet_wrap(), I want to label each individual plot with a mathematical expression:
library(ggplot2)
x <- rnorm(100, 50, 10)
y <- rnorm(100, 50, 10)
grp <- rep(c("a", "b", "c", "d"), each=25)
test.df <- data.frame(grp, x, y)
mean.df <- aggregate(test.df[c("x", "y")], test.df["grp"], mean)
p <- ggplot(test.df) +
geom_point(aes(x=x, y=y, col=grp)) +
facet_wrap(~ grp) +
geom_text(data=mean.df, aes(x=x, y=y, label=paste("xbar=", round(x,1))))
p
I want \bar(x) instead of xbar. I tried expression(), but I get: "cannot coerce class ""expression"" to a data.frame".
回答1:
Using
geom_text(data = mean.df, parse = TRUE,
aes(x = x, y = y, label = paste("bar(x) ==", round(x, 1))))
helps.
来源:https://stackoverflow.com/questions/35044906/ggplot2-facet-wrap-with-mathematical-expression