将简单的lm趋势线添加到ggplot boxplot [英] Adding a simple lm trend line to a ggplot boxplot

查看:583
本文介绍了将简单的lm趋势线添加到ggplot boxplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用标准R图形将线性模型趋势线添加到boxplot时,我使用:

$ p $ boxplot(iris [,2 ] iris [,1],col =LightBlue,main =Quartile1(Rare))
modelQ1 <-lm(iris [,2]〜iris [,1])$ ​​b $ b abline然而,当在ggplot2中使用它时:

$ b $

$ p

$ (虹膜,aes(因子(虹膜[,1]),虹膜[,2]))
a + geom_boxplot( )+
geom_smooth(method =lm,se = FALSE,color =black,formula = iris [,2]〜iris [,1])$ ​​b $ b

我收到以下错误:

  geom_smooth:Only每组有一个独特的x值。也许你想要aes(group = 1)? 

此行不会出现在我的情节中。



在这两种情况下使用的模型都是相同的。如果任何人都能指出我出错的地方,那会很好。



编辑:以虹膜数据集为例。

这个错误信息几乎是不言自明的:将 aes(group = 1)添加到 geom_smooth


$ b $

  ggplot(iris,aes(factor(Sepal.Length),Sepal.Width))+ 
geom_boxplot()+
geom_smooth(method =lm,se = FALSE,color =black,aes(group = 1))


When adding a linear model trend line to a boxplot using standard R graphics I use:

boxplot(iris[,2]~iris[,1],col="LightBlue",main="Quartile1 (Rare)")
modelQ1<-lm(iris[,2]~iris[,1])
abline(modelQ1,lwd=2)

However, when using this in ggplot2:

a <- ggplot(iris,aes(factor(iris[,1]),iris[,2]))
a + geom_boxplot() +
geom_smooth(method = "lm", se=FALSE, color="black", formula=iris[,2]~iris[,1])

I get the following error:

geom_smooth: Only one unique x value each group.Maybe you want aes(group = 1)?

And the line does not appear on my plot.

The models used in both of these scenarios are identical. If anyone could point out where I'm going wrong, that would be great.

EDIT: Used the iris dataset as an example.

解决方案

The error message is pretty much self-explanatory: Add aes(group=1) to geom_smooth:

ggplot(iris, aes(factor(Sepal.Length), Sepal.Width)) +
  geom_boxplot() +
  geom_smooth(method = "lm", se=FALSE, color="black", aes(group=1))

这篇关于将简单的lm趋势线添加到ggplot boxplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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