ggplot2中的geom_smooth无法正常工作/显示 [英] geom_smooth in ggplot2 not working/showing up

查看:523
本文介绍了ggplot2中的geom_smooth无法正常工作/显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向图形添加线性回归线,但是当它运行时,它没有显示.下面的代码已简化.通常每天都有多个积分.除此之外,该图还不错.

I am trying to add a linear regression line to my graph, but when it's run, it's not showing up. The code below is simplified. There are usually multiple points on each day. The graph comes out fine other than that.

    b<-data.frame(day=c('05/22','05/23','05/24','05/25','05/26','05/27','05/28','05/29','05/30','05/31','06/01','06/02','06/03','06/04','06/05','06/06','06/07','06/08','06/09','06/10','06/11','06/12','06/13','06/14','06/15','06/16','06/17','06/18','06/19','06/20','06/21','06/22','06/23','06/24','06/25'),
                  temp=c(10.1,8.7,11.4,11.4,11.6,10.7,9.6,11.0,10.0,10.7,9.5,10.3,8.4,9.0,10.3,11.3,12.7,14.5,12.5,13.2,16.5,19.1,14.6,14.0,15.3,13.0,10.1,8.4,4.6,4.3,4.7,2.7,1.6,1.8,1.9))


gg2 <- ggplot(b, aes(x=day, y=temp, color=temp)) +
  geom_point(stat='identity', position='identity', aes(colour=temp),size=3)


gg2<- gg2 + geom_smooth(method='lm') + scale_colour_gradient(low='yellow', high='#de2d26') 

gg2 <-gg2 + labs(title=filenames[s], x='Date', y='Temperture (Celsius)') + theme(axis.text.x=element_text(angle=-45, vjust=0.5))


gg2

这可能真的很简单,但我似乎无法弄清楚.或者这是我在x轴上使用日期的事实,但是我没有收到任何错误.如果是由于日期,我不确定如何处理.谢谢.

It's probably something really simple, but I can't seem to figure it out. Or it's the fact I am using a date for the x-axis, but I'm not receiving any errors. If it is due to the date, I'm not sure how to approach it. Thanks.

推荐答案

由于您是作为字符向量输入的,因此当前的日期是一个重要因素.参见class(b$day).

Currently your date is a factor since you entered in in as a character vector. See class(b$day).

将其更改为日期后,线性回归将运行良好.

Once you change it to a date, the linear regression will run fine.

b$Day <- as.Date(b$day, format='%m/%d')
# If dates are from 2015, 
# b$Day <- as.Date(b$day, format='%m/%d') - 366
# check with head(b$Day)

gg2 <- ggplot(b, aes(x=Day, y=temp, color=temp)) +
  geom_point(stat='identity', position='identity', aes(colour=temp),size=3)

gg2<- gg2 + geom_smooth(method='lm') + 
  scale_colour_gradient(low='yellow', high='#de2d26') 

gg2 <-gg2 + labs(title=filenames[s], x='Date', y='Temperture (Celsius)') + 
  theme(axis.text.x=element_text(angle=-45, vjust=0.5))

g2

这篇关于ggplot2中的geom_smooth无法正常工作/显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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