ggplot2 facet_wrap with mathematical expression

…衆ロ難τιáo~ 提交于 2020-01-13 13:09:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!