首页 > 解决方案 > geom_smooth 与 R 中的字符串

问题描述

我有一个变量要用于 ggplots 中的 y 轴,它必须作为它的“”名称传递(出于类似原因,我可以在那里使用一些变量并希望改变 y 轴标签来反映它)。

但我也想在图表上绘制一条平滑线。

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)

Error: stat_smooth requires the following missing aesthetics: y. 有什么办法吗?

标签: rggplot2

解决方案


你在找help('get')吗?它会

返回命名对象的值

并将其用作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)

在此处输入图像描述


推荐阅读