我有以下数据框:
A B C D Xax
0.451 0.333 0.034 0.173 0.22
0.491 0.270 0.033 0.207 0.34
0.389 0.249 0.084 0.271 0.54
0.425 0.819 0.077 0.281 0.34
0.457 0.429 0.053 0.386 0.53
0.436 0.524 0.049 0.249 0.12
0.423 0.270 0.093 0.279 0.61
0.463 0.315 0.019 0.204 0.23
我需要在同一个图中绘制所有这些列(在 x 轴上我想要变量 Xax,在 y 轴上想要变量 A、B、C 和 D),并且还要单独为每个变量绘制回归线。
我试过这个:
pl<-ggplot(data=df) + geom_point(aes(x=Xax,y=A,size=10)) +
geom_point(aes(x=Xax,y=B,size=10)) +
geom_point(aes(x=Xax,y=C,size=10)) +
geom_point(aes(x=Xax,y=D,size=10)) +
geom_smooth(method = "lm", se=FALSE, color="black")
但它只是绘制第一个(Xax 和 A)
最简单的方法是将数据转换为“tall”格式。
s <-
"A B C G Xax
0.451 0.333 0.034 0.173 0.22
0.491 0.270 0.033 0.207 0.34
0.389 0.249 0.084 0.271 0.54
0.425 0.819 0.077 0.281 0.34
0.457 0.429 0.053 0.386 0.53
0.436 0.524 0.049 0.249 0.12
0.423 0.270 0.093 0.279 0.61
0.463 0.315 0.019 0.204 0.23
"
d <- read.delim(textConnection(s), sep="")
library(ggplot2)
library(reshape2)
d <- melt(d, id.vars="Xax")
# Everything on the same plot
ggplot(d, aes(Xax,value, col=variable)) +
geom_point() +
stat_smooth()
# Separate plots
ggplot(d, aes(Xax,value)) +
geom_point() +
stat_smooth() +
facet_wrap(~variable)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)