简体   繁体   English

在R中绘制多条迹线

[英]Plot multiple traces in R

I started learning R for data analysis and, most importantly, for data visualisation. 我开始学习R用于数据分析,最重要的是,用于数据可视化。

Since I am still in the switching process, I am trying to reproduce the activities I was doing with Graphpad Prism or Origin Pro in R. In most of the cases everything was smooth, but I could not find a smart solution for plotting multiple y columns in a single graph. 由于我还处于切换过程中,我正在尝试重现我在R中用Graphpad Prism或Origin Pro进行的活动。在大多数情况下,一切都很顺利,但我找不到用于绘制多个y列的智能解决方案在一个图表中。

What I usually get from the softwares I use for data visualisations look like this: 我通常从用于数据可视化的软件中获得的内容如下所示:

在此输入图像描述

Each single black trace is a measurement, and I would like to obtain the same plot in R. In Prism or Origin, this will take a single copy-paste in a XY graph. 每个黑色迹线都是一个测量值,我想在R中获得相同的图。在Prism或Origin中,这将在XY图形中进行单个复制粘贴。

I exported the matrix of data (one X, which indicates the time, and multiple Y values, which are the traces you see in the image). 我导出了数据矩阵(一个X表示时间,多个Y值,这是您在图像中看到的痕迹)。

I imported my data in R with the following commands: 我使用以下命令在R中导入了我的数据:

library(ggplot2) #loaded ggplot2

Data <- read.csv("Directory/File.txt", header=F, sep="") #imported data
DF <- data.frame(Data) #transformed data into data frame

If I plot my data now, I obtain a series of columns, where the first one (called V1) is the X axis and all the others (V2 to V140) are the traces I want to put on the same graph. 如果我现在绘制我的数据,我会获得一系列列,其中第一列(称为V1)是X轴,所有其他列(V2到V140)是我想要放在同一图表上的轨迹。

To plot the data, I tried different solutions: 为了绘制数据,我尝试了不同的解决方案:

ggplot(data=DF, aes(x=DF$V1, y=DF[V2:V140]))+geom_line()+theme_bw() #did not work

plot(DF, xy.coords(x=DF$V1, y=DF$V2:V140)) #gives me an error

plot(DF, xy.coords(x=V1, y=c(V2:V10))) #gives me an error

I tried the matplot , without success, following the EZH guide: 我按照EZH指南尝试了matplot ,但没有成功:

The code I used is the following: matplot(x=DF$V1, type="l", lty = 2:100) 我使用的代码如下: matplot(x=DF$V1, type="l", lty = 2:100)

The only solution I found would be to individually plot a command for each single column, but it is a crazy solution. 我发现的唯一解决方案是为每个单独的列单独绘制一个命令,但这是一个疯狂的解决方案。 The number of columns varies among my data, and manually enter commands for 140 columns is insane. 列数因我的数据而异,手动输入140列的命令是疯狂的。

What would you suggest? 你会建议什么?

Thank you in advance. 先感谢您。

Here there are also some data attached. 这里还附有一​​些数据。 Data: single X, multiple Y 数据:单X,多Y

I tried using the matplot(). 我尝试使用matplot()。 I used a very sample data which has no trend at all. 我使用了一个没有趋势的非常样本数据。 so th eoutput from my code shall look terrible, but my main focus is on the code. 所以我的代码输出看起来很糟糕,但我主要关注的是代码。 Since you have already tried matplot() ,just recheck with below solution if you had done it right! 既然你已经尝试了matplot(),那么只要你做得对,就用下面的解决方案重新检查!

set.seed(100)
df = matrix(sample(1:685765,50000,replace = T),ncol = 100)
colnames(df)=c("x",paste0("y", 1:99))
dt=as.data.frame(df)
matplot(dt[["x"]], y = dt[,c(paste0("y",1:99))], type = "l")

If you want to plot in base R , you have to make a plot and add lines one at a time, however that isn't hard to do. 如果你想在基数R绘图,你必须制作一个图并一次添加一行,但这并不难。

we start by making some sample data. 我们首先制作一些样本数据。 Since the data in the link seemed to all be on the same scale, I will assume your data frame only has y values and the x value is stored separately. 由于链接中的数据似乎都在相同的比例上,我假设您的数据框只有y值,x值是分开存储的。

plotData <- as.data.frame(matrix(sort(rnorm(500)),ncol = 5))
xval <- sort(sample(200, 100))

Now we can initialize a plot with the first column. 现在我们可以用第一列初始化一个图。

plot(xval, plotData[[1]], type = "l", 
     ylim = c(min(plotData), max(plotData)))
  • type = "l" makes a line plot instead of a scatter plot type = "l"创建一个线图而不是散点图
  • ylim = c(min(plotData), max(plotData)) makes sure the y-axis will fit all the data. ylim = c(min(plotData), max(plotData))确保y轴适合所有数据。

Now we can add the rest of the values. 现在我们可以添加其余的值。

apply(plotData[-1], 2, lines, x = xval)

  • plotData[-1] removes the column we already plotted, plotData[-1]删除我们已绘制的列,
  • apply function with 2 as the second parameter means we want to execute a function on every column, apply函数2作为第二个参数意味着我们想要在每一列上执行一个函数,
  • lines defines the function we are apply ing to the columns. lines定义了我们apply列的函数。 lines adds a new line to the current plot. lines为当前绘图添加了一个新行。
  • x = xval passes an extra parameter ( x ) to the lines function. x = xval将额外参数( x )传递给lines函数。

结果情节

if you wat to plot the data using ggplot2, the data should be transformed to long format; 如果您使用ggplot2绘制数据,则应将数据转换为长格式;

library(ggplot2)
library(reshape2)

dat <- read.delim('AP.txt', header = F)

# plotting only first 9 traces
# my rstudio will crach if I plot the full data;
df <- melt(dat[1:10], id.vars = 'V1')

ggplot(df, aes(x = V1, y = value, color = variable)) + geom_line()

# if you want all traces to be in same colour, you can use
ggplot(df, aes(x = V1, y = value, group = variable)) + geom_line()

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

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