【发布时间】:2021-10-17 23:33:54
【问题描述】:
我正在使用 R 编程语言。我使用内置的“mtcars”数据集制作了以下图表:
library(ggplot2)
a = ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point() + ggtitle("mtcars: wt vs mpg")
问题:现在,我正在尝试自定义标题,但我希望标题“包含变量引用”,例如:
b = ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point() + ggtitle("mtcars: wt vs mpg - average mpg = mean(mtcars$mpg)")
但是这个命令实际上只是打印我写的代码:
我知道“mean”命令自己运行:
mean(mtcars$mpg)
[1] 20.09062
但是有人可以帮我更改此代码吗:
ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point() + ggtitle("mtcars: wt vs mpg - average mpg = mean(mtcars$mpg)")
所以它会产生这样的东西(注意:这里,我手动写了“平均值”):
谢谢
【问题讨论】:
标签: r ggplot2 data-visualization scatter-plot