用ggplot stat_smooth绘制多项式回归线 [英] plot polynomial regression line with ggplot stat_smooth

查看:71
本文介绍了用ggplot stat_smooth绘制多项式回归线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot:stat_smooth创建具有二阶多项式回归线的散点图.这是代码:

I'm trying to create a scatter plot with second degree polynomial regression line using ggplot:stat_smooth. Here are the codes:

df.car_spec_data <- read.csv(url("http://www.sharpsightlabs.com/
wp- content/uploads/2015/01/auto-snout_car-specifications_COMBINED.txt"))

df.car_spec_data$year <- as.character(df.car_spec_data$year)

df.car_spec_data %>% group_by(year) %>%
summarise(maxspeed=max(top_speed_mph,
na.rm=T)) %>%  ggplot(aes(x=year, y=maxspeed,
group=1))+geom_point(color='red',   alpha=0.3,
size=3)+stat_smooth(method='lm', y~poly(x,2))

我收到以下错误消息:

Error: Mapping must be created by `aes()` or `aes_()`

非常感谢.

推荐答案

此方法适用(适用于mtcars数据集):

This works (for mtcars dataset):

df.car_spec_data <- mtcars
df.car_spec_data %>% group_by(cyl) %>%
  summarise(maxmpg=max(mpg, na.rm=T)) %>%  
    ggplot(aes(x=cyl, y=maxmpg, group=1)) + 
    geom_point(color='red',   alpha=0.3,size=3)+
    stat_smooth(method='lm', formula = y~poly(x,2))

这篇关于用ggplot stat_smooth绘制多项式回归线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆