ggplot2 facet_wrap 带数学表达式
ggplot2 facet_wrap with mathematical expression
使用 facet_wrap(),我想用数学表达式标记每个单独的图:
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
我想要 \bar(x) 而不是 xbar。我试过 expression(),但我得到:"cannot coerce class ""expression"" 到 data.frame".
正在使用
geom_text(data = mean.df, parse = TRUE,
aes(x = x, y = y, label = paste("bar(x) ==", round(x, 1))))
有帮助。
使用 facet_wrap(),我想用数学表达式标记每个单独的图:
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
我想要 \bar(x) 而不是 xbar。我试过 expression(),但我得到:"cannot coerce class ""expression"" 到 data.frame".
正在使用
geom_text(data = mean.df, parse = TRUE,
aes(x = x, y = y, label = paste("bar(x) ==", round(x, 1))))
有帮助。