R ggplot2 散点图:为偏离(回归)geom_smooth 线的水平添加颜色
R ggplot2 scatterplot: adding color for the level of deviation from (regression) geom_smooth line
我正在尝试使用具有回归线的 ggplot2 创建散点图(两个连续变量)。我的小数据集(年度平均值)的大部分数据点都在回归线上或接近回归线,而一些观察结果则放置得更远一些。是否可以根据观察值与回归线的距离对散点图上的观察值进行颜色编码?
到目前为止,我自己手动为变量创建了颜色值组,但这看起来有点偏颇。如果可能的话,我想要一些自动的东西。
ggplot(data_mean, aes(x= policy1, y= policy2 ))+
geom_point(aes(colour = group), size=4) +geom_text_repel(aes(label=iso),hjust=0, vjust=0) +
geom_smooth(method=lm, se=FALSE, size=0.1) +
scale_color_manual(name = "Country Categories", # or name = element_blank()
values=colors) +
theme(legend.position="bottom",
legend.title=element_blank())
是否可以根据观察值与回归线的距离对散点图上的观察值进行颜色编码?谢谢!
定义哪些是异常值有点困难,这实际上取决于您拥有的数据。您可以尝试类似下面的操作,我在其中计算线性回归的残差,并将 2 * sd(残差)之外的那些定义为异常值。
首先是一些看起来像您的数据的东西,但 policy2 引入了一些错误
set.seed(888)
data_mean=data.frame(policy1=1:20,policy2=1:20 + rnbinom(20,mu=2,size=2))
data_mean$residuals = abs(lm(policy2~policy1,data=data_mean)$residuals)
# here we define the outliers to be those more than 2 standard error of residuals
data_mean$group = data_mean$residuals > 2*sd(data_mean$residuals)
data_mean$iso = letters[1:20]
然后我们绘制:
ggplot(data_mean, aes(x= policy1, y= policy2))+
geom_point(aes(colour = group), size=4) +
geom_text_repel(aes(label=iso),hjust=0, vjust=0) +
geom_smooth(method=lm, se=FALSE, size=0.1) +
theme(legend.position="bottom",
legend.title=element_blank())
一种替代方法实际上是使用连续刻度:
ggplot(data_mean, aes(x= policy1, y= policy2))+
geom_point(aes(colour = residuals), size=4) +
geom_text_repel(aes(label=iso),hjust=0, vjust=0) +
geom_smooth(method=lm, se=FALSE, size=0.1) +
theme(legend.position="bottom",
legend.title=element_blank()) +
scale_color_viridis()
同样,如果您分享一些数据,并详细说明您希望如何根据残差为点着色,那就太好了。
我正在尝试使用具有回归线的 ggplot2 创建散点图(两个连续变量)。我的小数据集(年度平均值)的大部分数据点都在回归线上或接近回归线,而一些观察结果则放置得更远一些。是否可以根据观察值与回归线的距离对散点图上的观察值进行颜色编码?
到目前为止,我自己手动为变量创建了颜色值组,但这看起来有点偏颇。如果可能的话,我想要一些自动的东西。
ggplot(data_mean, aes(x= policy1, y= policy2 ))+
geom_point(aes(colour = group), size=4) +geom_text_repel(aes(label=iso),hjust=0, vjust=0) +
geom_smooth(method=lm, se=FALSE, size=0.1) +
scale_color_manual(name = "Country Categories", # or name = element_blank()
values=colors) +
theme(legend.position="bottom",
legend.title=element_blank())
是否可以根据观察值与回归线的距离对散点图上的观察值进行颜色编码?谢谢!
定义哪些是异常值有点困难,这实际上取决于您拥有的数据。您可以尝试类似下面的操作,我在其中计算线性回归的残差,并将 2 * sd(残差)之外的那些定义为异常值。
首先是一些看起来像您的数据的东西,但 policy2 引入了一些错误
set.seed(888)
data_mean=data.frame(policy1=1:20,policy2=1:20 + rnbinom(20,mu=2,size=2))
data_mean$residuals = abs(lm(policy2~policy1,data=data_mean)$residuals)
# here we define the outliers to be those more than 2 standard error of residuals
data_mean$group = data_mean$residuals > 2*sd(data_mean$residuals)
data_mean$iso = letters[1:20]
然后我们绘制:
ggplot(data_mean, aes(x= policy1, y= policy2))+
geom_point(aes(colour = group), size=4) +
geom_text_repel(aes(label=iso),hjust=0, vjust=0) +
geom_smooth(method=lm, se=FALSE, size=0.1) +
theme(legend.position="bottom",
legend.title=element_blank())
一种替代方法实际上是使用连续刻度:
ggplot(data_mean, aes(x= policy1, y= policy2))+
geom_point(aes(colour = residuals), size=4) +
geom_text_repel(aes(label=iso),hjust=0, vjust=0) +
geom_smooth(method=lm, se=FALSE, size=0.1) +
theme(legend.position="bottom",
legend.title=element_blank()) +
scale_color_viridis()
同样,如果您分享一些数据,并详细说明您希望如何根据残差为点着色,那就太好了。