Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2中的几何图形平滑不工作/显示_R_Ggplot2_Linear Regression - Fatal编程技术网

R ggplot2中的几何图形平滑不工作/显示

R ggplot2中的几何图形平滑不工作/显示,r,ggplot2,linear-regression,R,Ggplot2,Linear Regression,我试图在图表中添加一条线性回归线,但当它运行时,它不会显示出来。下面的代码被简化了。每天通常有多个点。除此之外,这张图表还不错 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'

我试图在图表中添加一条线性回归线,但当它运行时,它不会显示出来。下面的代码被简化了。每天通常有多个点。除此之外,这张图表还不错

    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

b当前您的日期是一个因素,因为您是以字符向量的形式输入的。请参见
课程(b$天)

一旦更改为日期,线性回归将正常运行

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
b$Day
#示例数据

d您需要将day设置为一个数值变量,如
…ggplot(b,aes(x=as.numeric(day))…
非常感谢。我想我遗漏了一些简单的东西。救生员!谢谢。
# sample data
d <- data.frame(expand.grid(x=letters[1:4], g=factor(1:2)), y=rnorm(8))   
# Try the below three different scripts to draw.
ggplot(d, aes(x=x, y=y, colour=g)) + geom_line() + geom_point()
ggplot(d, aes(x=x, y=y, colour=g, group=g))+ geom_line() + geom_point()
ggplot(d, aes(x=x, y=y, colour=g, group=1)) + geom_line() + geom_point()