用ggplot2添加回归线 [英] adding regression line per group with ggplot2

查看:199
本文介绍了用ggplot2添加回归线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下图表:

I do the following graph:

> ddd
      UV.NF TRIS            volvol
2  145.1923   31  500 µl / 625  µl
3  116.3462   50  500 µl / 625  µl
4  127.1635   60  500 µl / 625  µl
5  125.9615   69  500 µl / 625  µl
6  162.0192   30    1 ml / 625  µl
7  166.8269   50    1 ml / 625  µl
8  176.4423   60    1 ml / 625  µl
9  171.6346   70    1 ml / 625  µl
19 292.3077   31 500 µl / 2500  µl
20 321.1538   50 500 µl / 2500  µl
21 225.0000   60 500 µl / 2500  µl
22 263.4615   69 500 µl / 2500  µl
23 301.9231   30   1 ml / 2500  µl
24 350.0000   50   1 ml / 2500  µl
25 282.6923   60   1 ml / 2500  µl
26 282.6923   70   1 ml / 2500  µl
35 133.6207   31  500 µl / 625  µl

ggplot() +  
    geom_point(aes(y = log(UV.NF), x = TRIS, colour=ddd[,"volvol"], shape=ddd[,"volvol"]), 
        data=ddd) + 
    labs(colour = "volvol", shape="volvol") + xlab("TRIS (mM)") + 
    guides(colour = guide_legend(title="Vol. lyo. / Vol. reconst."), 
        shape=guide_legend(title="Vol. lyo. / Vol. reconst.")) +
    scale_shape_manual(values = c(19,19,3,3)) + scale_colour_manual(values = c(2,4,2,4))

我想添加回归线 lm(我对 geom_smooth()做了很多尝试,但没有成功。 p>

I want to add the regression line lm(y~x) for each of the four groups appearing in the legend. I have done many attempts with geom_smooth() but without success.

推荐答案

我不太确定这是否是您想要的,但您是否尝试过以下方法?

I'm not quite sure whether that's what you want, but have you tried the following?

ggplot(ddd,aes(y = log(UV.NF), x =TRIS,colour=volvol,shape=volvol)) +
geom_point() + geom_smooth(method="lm", fill=NA)

这给了我下面的图与您的数据:

This gives me the following plot with your data:

有还有一些关于 geom_smooth ,尽管它采用更复杂(但灵活)的方式,但它完成了您所需要的功能。

There's also some documentation for geom_smooth that does pretty much what you'd like, albeit in a more complicated (yet flexible) manner.

这篇关于用ggplot2添加回归线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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