首页 > 解决方案 > geom_smooth() 创建多条线,可能是美学问题

问题描述

(尝试寻找其他问题,找不到好的问题。)

我正在尝试创建一条 geom_smooth() 线,但我不断收到多个。这是我的代码:

plot1 <-
 ggplot(gapminder) +
 aes(y = lifeExp, x = gdpPercap, shape = continent, color = year) +
 labs(y = "Life Expectancy") + guides(color = "none") +
 geom_point() +
 scale_x_log10()

plot1 + geom_smooth()

我相信这与我将美学放在哪里或我如何自行格式化它们有关。

任何帮助表示赞赏。

哦,澄清一下,每个大陆都有不同的线路。我只想要一条整体线。

谢谢你。

标签: r

解决方案


我想到了。我只需要将形状和颜色的美学直接移入geom_point(),但不理会轴。

这是正确的代码:

plot1 <-
  ggplot(gapminder, mapping = aes(y = lifeExp, x = gdpPercap)) +
  labs(y = "Life Expectancy") +
  geom_point(mapping = aes(shape = continent, color = year)) +
  guides(color = "none") +
  scale_x_log10()

plot1 +
  geom_smooth()

谢谢你。


推荐阅读