简体   繁体   English

在 R ggplot2 中向图形添加趋势线时出现问题

[英]Problem adding a trendline to a graph in R ggplot2

I tried to use geom_smooth(method = "lm") and it doesn't work...我尝试使用geom_smooth(method = "lm")但它不起作用......

percentage.no.work <- cleanData %>% group_by(AREA) %>%
  summarise(percentage = mean(ESTIMATED.CITY.UNEMPLOYMENT))

ggplot() +
  geom_point(data=percentage.no.work, aes(x=AREA, y=percentage), alpha=0.6, color="purple", size=2) +
  geom_smooth(method = "lm") +
  theme_minimal() + ggtitle("Percentage Estimated City Unemployment") + 
  ylab("Percentage")

You need to provide the aesthetics for the geom_smooth as well.您还需要为geom_smooth提供美感。 Either by including it in the ggplot() or in the geom_smooth() :通过将其包含在ggplot()geom_smooth()中:

ggplot() +
  geom_point(data=percentage.no.work, aes(x=AREA, y=percentage), alpha=0.6, color="purple", size=2) +
  geom_smooth(aes(x=AREA, y=percentage), method = "lm") +
  theme_minimal() + ggtitle("Percentage Estimated City Unemployment") + 
  ylab("Percentage")

You can avoid repeating section of the code putting it in the ggplot()您可以避免将代码的重复部分放入ggplot()

ggplot(data=percentage.no.work, aes(x=AREA, y=percentage)) +
  geom_point(alpha=0.6, color="purple", size=2) +
  geom_smooth(method = "lm") +
  theme_minimal() + ggtitle("Percentage Estimated City Unemployment") + 
  ylab("Percentage")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM