R geom_平滑在aes中写什么

R geom_平滑在aes中写什么,r,ggplot2,regression,R,Ggplot2,Regression,我与R和ggplot一起工作。我已经为4个不同的data.frames绘制了点。现在我想为这个点集画4条回归线 我以前的代码: ggplot() + ggtitle("title")+ xlab("date") + ylab("value") + geom_point(data=toplot$1, aes(x=date, y=x, color='1'), size = 4, shape=1) + geom_point(data=toplot$2

我与R和ggplot一起工作。我已经为4个不同的data.frames绘制了点。现在我想为这个点集画4条回归线

我以前的代码:

ggplot() + 
    ggtitle("title")+
    xlab("date") +
    ylab("value") +
    geom_point(data=toplot$1, aes(x=date, y=x, color='1'), size = 4, shape=1) +     
    geom_point(data=toplot$2, aes(x=date, y=x, color='2'), size = 4, shape=2)+      
    geom_point(data=toplot$3, aes(x=date, y=x, color='3'), size = 4, shape=3)+      
    geom_point(data=toplot$4, aes(x=date, y=x, color='4'), size = 4, shape=4)+          
    scale_colour_manual(name = "legend", values = c('1'='green', "2"="red", "3"="blue", "4"="brown"))
当我为第一个data.frame添加一行时

geom_smooth(data=toplot$1, formula=date~x,method=lm, color='1',aes(x=date, y=x)) 
我收到一条信息:

每组只有一个唯一的x值。可能需要aes(组=1)

如果我添加行:

geom_smooth(data=toplot$1, formula=date~x,method=lm, color='1',aes(group=1)) 
我收到另一条消息:

stat_smooth需要以下缺失的美学:x,y

也许你知道,我需要写的aes参数(没有任何aes它也不起作用)


谢谢。

平滑函数(在本例中为“lm”)使用aes中定义的变量,因此您可以使用如下内容

toplot1 <- data.frame(date=seq(Sys.Date()-30,Sys.Date(),1), x= 1:31+ rnorm(31, 0, 2))
toplot2 <- data.frame(date=seq(Sys.Date()-30,Sys.Date(),1), x= 1:31+ 5 + rnorm(31, 0, 2))
 sp <- ggplot() + 
     ggtitle("title")+
     xlab("date") +
     ylab("value") +
     geom_point(data=toplot1, aes(x=date, y=x, color='1'), size = 4, shape=1) +     
     geom_point(data=toplot2, aes(x=date, y=x, color='2'), size = 4, shape=2)+      
     scale_colour_manual(name = "legend", values = c ('1'='green', "2"="red", "3"="blue", "4"="brown"))
 sp <- sp  +
      geom_smooth(data=toplot1, aes(x=date, y=x, color="1"), formula = y~x, method="lm") +
      geom_smooth(data=toplot2, aes(x=date, y=x, color="1"), formula = y~x, method="lm")
plot(sp)

toplot1简短的答案可能是:

geom_smooth(data=toplot$1, formula=y~x,method=lm, color='1',aes(x = date, y = x,group=1))
(无论在其他层中如何称呼它们,
geom_smooth
中的公式始终是“通用”的,因为您引用了响应并与x和y共变。)

然而,这:

geom_point(data=toplot$1, aes(x=date, y=x, color='1'), size = 4, shape=1) +     
geom_point(data=toplot$2, aes(x=date, y=x, color='2'), size = 4, shape=2)+      
geom_point(data=toplot$3, aes(x=date, y=x, color='3'), size = 4, shape=3)+      
geom_point(data=toplot$4, aes(x=date, y=x, color='4'), size = 4, shape=4)
这不是正确的方式。基本上,只要你发现自己一遍又一遍地重复一个
geom
,这就是你没有正确使用ggplot的好迹象

相反,通常使用
rbind
将所有四个数据帧合并为一个数据帧,然后创建第三个变量
grp
,其值为1-4,以标记每个部分。然后你只需要做一层:

geom_point(data = full_data,aes(x = date,y = x, color = grp, shape = grp), size = 4)

简短的答案是aes(x=date,y=x,color='1')
,正确的答案是您应该学会使用
ggplot2
。有关如何使用分组的示例,请参见下文

# prepare data
toplot2 <- do.call(rbind, toplot)
toplot2[, "group"] <- factor(rep(1:length(toplot), times=sapply(toplot, nrow)))
# ggplot command
ggplot(toplot2, aes(x=date, y=x, color=group, shape=group)) +
  geom_point(size=4) +
  geom_smooth(method=lm) +
  ggtitle("title")+
  xlab("date") +
  ylab("value") +
  scale_colour_manual(name = "legend", values = c('1'='green', '2'='red', '3'='blue', '4'='brown')) +
  scale_shape_manual(name = "legend", values = c('1'=1, '2'=2, '3'=3, '4'=4))
#准备数据

toplot2我的脚本运行时,只添加组=1 x,y轴

graf_disp <- ggplot(dados, aes(x=Treatments, y=Survival, group=1)) +
  geom_point(aes(col=Treatments)) +
  geom_smooth(method=lm, se=F) +
  labs(subtitle = "Weeds Survival Percentage",
       y = "Weeds Survival (%)", x = "Treatments")

plot(graf_disp)

graf_disp当只有一列数据时,我看不出您是如何设法使
aes(x=date,y=x)
工作的。更常见的方法是将所有数据堆叠在一列中,并为组创建一个指示符列,这样您只需使用两个几何图形一次:
ggplot(data=blah,aes(x=blah,y=blah,group=factor(group_id)))+geom_point()+geom_smooth()
I有两列数据:date和x(表示值)。不在同一数据列中。您似乎使用了从
toplot$1到4
定义的四列数据。我不太明白toplot$1是如何包含两列数据的。我有4个data.frames。在上面的代码中,您可以看到4个data.frames的4组点:toplot$1、toplot$2、toplot$3、toplot$4。每个data.frame包含两列:date和x(表示值)。我已经在同一张图片上得到了4组点,我想得到它们的4条回归线。我需要在geom_smooth的aes中写什么?
toplot$1
在base R中调用data.frame中的一列
toplot
。我仍然不知道您的数据结构,因此很难知道
geom_smooth
的正确语法应该是什么。对于初学者来说,
geom_smooth
不需要公式参数,因为它是
stat_smooth
的包装函数,并根据您的
aes
动态生成
公式。如果可以的话,做一个
dput(head(toplot,10))
geom_平滑(data=toplot$1,formula=y~x,method=lm,color=1',aes(group=1))
不起作用。答案是:每组只有一个唯一的x值。也许你想要aes(group=1)@user2957954我已经给了你尽可能多的帮助,没有重复的例子。谢谢,看起来不错。但是对于这个带有geom_平滑函数的代码,我仍然收到一条消息:每组只有一个唯一的x值。也许你想要aes(组=1)。这意味着什么?在stackoverflow上发布是一种学习方法