使用
我得到了这个情节。 感谢@kdauria对这个问题的回答,我根据他的回答将回归方程和R2添加到了下面的图中。
df
和以下代码library(dplyr)
library(ggplot2)
library(devtools)
df <- diamonds %>%
dplyr::filter(cut%in%c("Fair","Ideal")) %>%
dplyr::filter(clarity%in%c("I1" , "SI2" , "SI1" , "VS2" , "VS1", "VVS2")) %>%
dplyr::mutate(new_price = ifelse(cut == "Fair",
price* 0.5,
price * 1.1))
ggplot(df, aes(x= new_price, y= carat, color = cut))+
geom_point(alpha = 0.3)+
facet_wrap(~clarity, scales = "free_y")+
geom_smooth(method = "lm", se = F)
我得到了这个情节。 感谢@kdauria对这个问题的回答,我根据他的回答将回归方程和R2添加到了下面的图中。
source_gist("524eade46135f6348140")
ggplot(df, aes(x= new_price, y= carat, color = cut))+
stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE)+
geom_point(alpha = 0.3)+
facet_wrap(~clarity, scales = "free_y")+
geom_smooth(method = "lm", se = F)
现在,我希望将回归方程和R2的位置调整到每个分面的特定位置(例如,在每个分面的右下角,如“0.2 y和0.8 x”)。我尝试通过vjust
和hjust
来调整位置,但没有成功。非常感谢您提供任何建议。
ggpmisc
包的作者在ggplot2:添加回归线方程和R2图的答案以获取更多详细信息或联系作者。 - Uwe