在 R 中的同一张图上绘制多列 [英] Plot multiple columns on the same graph in R

查看:25
本文介绍了在 R 中的同一张图上绘制多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

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),还需要为每个变量绘制回归线一个人.

I need to plot all these columns in the same plot(on the x-axis I want the variable Xax and the y-axis the variables A,B,C and D) and also to draw the regression line for each variable alone.

我试过了:

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)

But it's only plotting the first one(Xax and A)

推荐答案

最简单的方法是将数据转换为tall"格式.

The easiest is to convert your data to a "tall" format.

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)

这篇关于在 R 中的同一张图上绘制多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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