简体   繁体   English

如何在R中绘制一列与其余列

[英]How to plot one column vs the rest in R

I have a data set where the [,1] is time and then the next 14 are magnitudes. 我有一个数据集,其中[,1]是时间,然后下一个14是幅度。 I would like to scatter plot all the magnitudes vs time on one graph, where each different column is gridded (layered on top of one another) 我想在一张图上散布所有幅度与时间的关系图,其中每个不同的列都被网格化(彼此叠加)

I want to use the raw data to make these graphs and came make them separately but would like to only have to do this process once. 我想使用原始数据制作这些图形,并分别制作它们,但只需要执行一次此过程。

data set called A, the only independent variable is time (the first column) 称为A的数据集,唯一的独立变量是时间(第一列)

df<-data.frame(time=A[,1],V11=A[,2],V08=A[,3],
 V21=A[,4],V04=A[,5],V22=A[,6],V23=A[,7],
 V24=A[,8],V25=A[,9],V07=A[,10],xxx=A[,11],
 V26=A[,12],PV2=A[,13],V27=A[,14],V28=A[,15],
 NV1=A[,16])

I tried the code mentioned by @VlooO but it scrunched the graphs making them too hard to decipher and each had its own axes. 我尝试了@VlooO提到的代码,但是它压缩了图形,使它们难以解读,并且每个图形都有自己的轴。 All my graphs can be on the same axes just separated by their headings. 我所有的图形都可以在同一根轴上,只是标题分开。

When looking at the ggplots I Think that would be a perfect program for what I want. 在查看ggplots时,我认为这将是我想要的完美程序。

ggplot(data=df.melt,aes(x=time,y=???))

I confused what my y should be since I want to reference each different column. 由于我要引用每个不同的列,我感到困惑。

Thanks R community 谢谢R社区

Hope i understand you correctly: 希望我能正确理解你:

df<-data.frame(time=rnorm(10),A=rnorm(10),B=rnorm(10),C=rnorm(10))

par(mfrow=c(length(df)-1,1))
sapply(2:length(df), function(x){
  plot(df[,c(1,x)])
})

The result would be 结果将是

绘图

here some hints since you don't provide a reproducible example , neither you show what you have tried : 这里有一些提示,因为您没有提供可复制的示例,也没有显示您尝试过的方法:

  1. Use list.files to go through all your documents 使用list.files浏览所有文档
  2. Use lapply to loop over the result of the previous step and read your data 使用lapply遍历上一步的结果并读取数据
  3. Put your data in the long format using melt from reshape2 and the variable time as id. 把你的数据在使用长格式meltreshape2和可变时间为ID。
  4. Use ggplot2 to plot using the variable as aes color/group. 使用ggplot2将变量用作aes颜色/组进行绘图。

     library(ggplot2) library(reshape2) invisible(lapply(list.files(pattern=...),{ dt = read.table(x) dt.l = melt(dt,id.vars='time') print(ggplot(dt.l)+geom_line(aes(x=time,y=value,color=variable)) })) 

如果不需要ggplot2,则可以使用基本图形的matplot函数在一个命令中执行所需的操作。

SOLUTION: After looking through a bunch more problems and playing around a bit more with ggplot2 I found a code that works pretty great. 解决方案:在研究了更多问题并使用ggplot2进行了更多尝试之后,我发现了一个可以正常工作的代码。 After I made my data frame (stated above), here is what i did 制作完数据框(如上所述)后,这就是我所做的

> df.m<- melt(df,"time")
ggplot(df.m, aes(time, value, colour = variable)) + geom_line() +
+ facet_wrap(~ variable, ncol = 2)

I would post the image but I don't have enough reputation points yet. 我会发布图片,但是我的信誉点还不够。

I still don't really understand why "value" is placed into the y position in aes(time, value,...) If anyone could provided an explanation that would be greatly appreciated. 我仍然不太明白为什么aes(time,value,...)中的“ value”放在y位置,如果有人可以提供一个解释,将不胜感激。 My last question is if anyones knows how to make the subgraphs titles smaller. 我的最后一个问题是,是否有人知道如何缩小子图标题。

Can I use cex.lab=, cex.main= in ggplot2? 我可以在ggplot2中使用cex.lab =,cex.main =吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM