我有一个变量想用于 ggplots 中的 y 轴,该变量必须作为其“”名称传递(出于类似原因,我可以在那里使用一些变量并希望更改 y 轴标签来反射(reflect)它)。
但我还想在图表上绘制一条平滑线。
ggplot(data=iris, aes(x=Sepal.Width, col=Species)) +
geom_point(aes_string(y="Petal.Length")) +
geom_smooth(method="lm", formula=y~x, se=F)
给出错误:stat_smooth需要以下缺失的美感:y
。有什么办法可以实现吗?
最佳答案
您在寻找help('get')
吗?将会
Return the Value of a Named Object
并将其用作 y
坐标来绘制数据。
library(ggplot2)
ggplot(data=iris, aes(x=Sepal.Width, y = get("Petal.Length"), color=Species)) +
geom_point() +
geom_smooth(method="lm", formula=y~x, se=F)
关于r - R 中带有字符串的 geom_smooth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62364353/