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

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

问题描述

我有以下数据框:

  ABCD 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

我需要在同一个图中绘制所有这些列(on 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 =bl ack)

但它只是绘制第一个(Xax和A)

解决方案

最简单的方法就是将您的数据转换为高格式。

  s < -  
ABCG 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)

#同一图上的所有内容
ggplot(d,aes(Xax,value,col = variable))+
geom_point()+
stat_smooth()

#单独地块
ggplot(d,aes(Xax,value))+
geom_point( )+
stat_smooth()+
facet_wrap(〜variable)


I have the following data frame:

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

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.

I tried this:

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")

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

解决方案

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天全站免登陆