本文介绍了如何绘制一列与R中的其余部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中[,1]是时间,然后是接下来的14个数值。我想在一张图上分散绘制所有幅度与时间的关系图,其中每个不同的列都是网格的(相互重叠)

我想使用raw数据,使这些图和来使他们分开,但只想做这个过程一次。

数据集称为A,唯一的自变量是时间(第一列)

  df  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])

我尝试了@VlooO提到的代码,但它使图形难以辨认,每个图形都有自己的坐标轴。我所有的图表都可以在相同的坐标轴上,按照标题分开。



在看ggplots时,我认为这将是我想要的完美程序。 > ggplot(data = df.melt,aes(x = time,y = ???))

由于我想引用每个不同的列,所以我很困惑我的y应该是什么。



感谢R社区

解决方案

t提供了一个可重复的例子,你既不显示你曾经尝试过的:


  1. 使用 list.files 循环上一步的结果并读取您的数据
  2. li>
  3. 使用 melt 从 reshape2 和长度可变的时间作为ID。使用 ggplot2 来使用变量作为 aes color / group。

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



I have a data set where the [,1] is time and then the next 14 are magnitudes. 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)

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. 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.

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

解决方案

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
  2. Use lapply to loop over the result of the previous step and read your data
  3. Put your data in the long format using melt from reshape2 and the variable time as id.
  4. Use ggplot2 to plot using the variable as aes color/group.

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

这篇关于如何绘制一列与R中的其余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:11