R 在ggplot2中使用geom_平滑更改绘图图例

R 在ggplot2中使用geom_平滑更改绘图图例,r,ggplot2,R,Ggplot2,我有一个非常简单的问题,我正在努力解决,即在ggplot2中使用geom_smooth更改绘图图例 这是我的密码: p1<- mtcars$group <- factor(mtcars$vs) ggplot(mtcars, aes(x=mpg, y=disp, group=group)) + geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group)

我有一个非常简单的问题,我正在努力解决,即在ggplot2中使用geom_smooth更改绘图图例

这是我的密码:

p1<-    mtcars$group <- factor(mtcars$vs)
    ggplot(mtcars, aes(x=mpg, y=disp, group=group)) +
      geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group), colour="black")

p1

我的错误是使用了
scale\u fill\u discrete
而不是
scale\u linetype\u discrete

如果这样运行代码,会出现错误吗

ggplot(mtcars, aes(x=mpg, y=disp, group=group)) +
    geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group), colour="black")+
    labs(linetype="Legend")+
    scale_linetype_discrete(labels=c("Experiment", "Control"))
我得到这个情节:


使用
scale\u linetype\u discrete()
而不是fill谢谢您的回复,但这会产生错误:错误:尝试应用非函数:编辑:无需更改,重新运行代码,结果正是我想要的。谢谢谢谢,内特,我已经更新了我的代码,看起来和应该更新的一模一样。
ggplot(mtcars, aes(x=mpg, y=disp, group=group)) +
  geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group), colour="black")+
  labs(linetype="Legend")+
  scale_linetype_discrete(labels=c("Experiment", "Control"))
ggplot(mtcars, aes(x=mpg, y=disp, group=group)) +
    geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group), colour="black")+
    labs(linetype="Legend")+
    scale_linetype_discrete(labels=c("Experiment", "Control"))