Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

r - adding regression line per group with 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))

graph

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...