Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何用每条线之间的特定距离绘制图形_R - Fatal编程技术网

R 如何用每条线之间的特定距离绘制图形

R 如何用每条线之间的特定距离绘制图形,r,R,实际上,我试图绘制一个图形,但它将所有列(线)放置并显示在彼此之间,因此它不具有代表性。我试着制作一个模拟数据,向你们展示我如何绘制它,也向你们展示我想要什么 我不知道如何制作一个像我在下面展示的示例那样的数据,但在这里我做了什么 set.seed(1) M <- matrix(rnorm(20),20,5) x <- as.matrix(sort(runif(20, 5.0, 7.5))) df <- as.data.frame(cbind(x,M)) set.seed(1

实际上,我试图绘制一个图形,但它将所有列(线)放置并显示在彼此之间,因此它不具有代表性。我试着制作一个模拟数据,向你们展示我如何绘制它,也向你们展示我想要什么

我不知道如何制作一个像我在下面展示的示例那样的数据,但在这里我做了什么

set.seed(1)
M <- matrix(rnorm(20),20,5)
x <- as.matrix(sort(runif(20, 5.0, 7.5)))
df <- as.data.frame(cbind(x,M))
set.seed(1)
M此解决方案使用并生成此图:

它使用此函数接受3个参数:

  • df
    :一个
    data.frame
    就像上面的'M'一样
  • x
    :x轴的
    数字向量(或1列
    数据.frame`)
  • cols
    :(可选)要重复的颜色向量。如果缺少,将绘制黑色线
以下是函数:

nik_plot <- function(df, x, cols){
  require(rgl)
  # if a data.frame is
  if (is.data.frame(x) && ncol(x)==1)
    x <- as.numeric(x[, 1])
  # prepare a vector of colors
  if (missing(cols))
    cols <- rep_len("#000000", nrow(df))
  else
    cols <- rep_len(cols, nrow(df))
  # initialize an empty 3D plot
  plot3d(NA, xlim=range(x), ylim=c(1, ncol(df)-1), zlim=range(df), xlab="Mass/Charge (M/Z)", ylab="Time", zlab="Ion Spectra", box=FALSE)
  # draw lines, silently
  silence_please <- sapply(1:ncol(df), function(i) lines3d(x=x, y=i, z=df[, i], col=cols[i])) 
}
nik_plot2D <- function(df, x, cols, gap=10, more_gap_each=1, more_gap_relative=0){
  if (is.data.frame(x) && ncol(x)==1)
    x <- as.numeric(x[, 1])

  # we normalize ( 0 <= signal <= 1)
  df <- df-min(df)
  df <- (df/max(df))
  # we prepare a vector of colors
  if (missing(cols))
    cols <- rep_len("#00000055", nrow(df))
  else
    cols <- rep_len(cols, nrow(df))
  # we prepare gap handling. there is probably more elegant
  gaps <- 1
  for (i in 2:ncol(df))
    gaps[i] <- gaps[i - 1] + 1/gap + ifelse((i %% more_gap_each) == 0, (1/gap)*more_gap_relative, 0)
  # we initialize the plot
  plot(NA, xlim=range(x), ylim=c(min(df), 1+max(gaps)), xlab="Time", ylab="", axes=FALSE, mar=rep(0, 4))
  axis(1)
  # finally, the lines
  silent <- lapply(1:ncol(df), function(i) lines(x, df[, i] + gaps[i], col=cols[i]))
}
可以使用鼠标导航3D窗口

你还需要别的吗


编辑


您可以使用下面的函数构建第二个绘图。你的数据范围如此之大,我认为将每一行向上移动的整个想法,可以防止y轴具有可靠的刻度。这里我已经对所有信号进行了归一化(0对于3D绘图,我更喜欢rgl软件包。这应该接近您想要的解决方案。
每次扫描的颜色每三次就改变一次

library(rgl)
M<-read.table("M.txt", sep="\t", header = TRUE, colClasses = "numeric")
x<-read.table("x.txt", sep="\t", header = TRUE)
n<-ncol(M)
M[M<1]<-1
plot3d(x='', xlim=range(x$Time), ylim=c(1, n), zlim=(range(M)), box=FALSE)
sapply(seq(1,n), function(t){lines3d(x$Time, y=t*10, z=(M[,t])/10000, col=t/3+1)})
title3d(xlab="scan", ylab="time", zlab="intensity")
title3d(main ="Extracted Spectra Subset")
axes3d()
#axis3d(edge="x")
#axis3d(edge="y")
#axis3d(edge="z")
库(rgl)

M正如其他人所指出的,您的数据具有非常大的峰值,并且不清楚是否要允许某些曲线重叠


m请以可复制格式为四列提供您的数据这很简单,但我想您希望将其应用于更多列?@Mol我不知道为什么我的代码无法在您的数据集上工作。抱歉!您确定我们需要3000x34数据集来帮助您吗?@rawr是的,因为示例数据不可用代表性也可以制作如上所述的2D绘图(每列都比另一列有特定的距离)你的意思是这样的:
绘图(x[,1],M[,5],type=“l”,xlab=“x title”,ylab=“y title”)
?是的,但它仅用于一列(配置文件)看看我在问题中粘贴的上图,这是一个全新的绘图(我觉得这是一个更好的答案)。我也更新了我的答案。这看起来很酷,我非常感谢你的回答。我想知道,如果可能的话,是否有可能为每五列或每三列分别添加一种特定的颜色?
nik_plot2D <- function(df, x, cols, gap=10, more_gap_each=1, more_gap_relative=0){
  if (is.data.frame(x) && ncol(x)==1)
    x <- as.numeric(x[, 1])

  # we normalize ( 0 <= signal <= 1)
  df <- df-min(df)
  df <- (df/max(df))
  # we prepare a vector of colors
  if (missing(cols))
    cols <- rep_len("#00000055", nrow(df))
  else
    cols <- rep_len(cols, nrow(df))
  # we prepare gap handling. there is probably more elegant
  gaps <- 1
  for (i in 2:ncol(df))
    gaps[i] <- gaps[i - 1] + 1/gap + ifelse((i %% more_gap_each) == 0, (1/gap)*more_gap_relative, 0)
  # we initialize the plot
  plot(NA, xlim=range(x), ylim=c(min(df), 1+max(gaps)), xlab="Time", ylab="", axes=FALSE, mar=rep(0, 4))
  axis(1)
  # finally, the lines
  silent <- lapply(1:ncol(df), function(i) lines(x, df[, i] + gaps[i], col=cols[i]))
}
nik_plot2D(M, x) # gap=10
nik_plot2D(M, x, 50)
nik_plot2D(M, x, gap=20, cols=1:3)
nik_plot2D(M, x, gap=20, cols=rep(1:3, each=5))
nik_plot2D(M, x, gap=20, cols=terrain.colors(10), more_gap_each = 1, more_gap_relative = 0) # no gap by default
nik_plot2D(M, x, gap=20, cols=terrain.colors(10), more_gap_each = 10, more_gap_relative = 4) # large gaps every 10 lines
nik_plot2D(M, x, gap=20, cols=terrain.colors(10), more_gap_each = 5, more_gap_relative = 2) # small gaps every 5 lines
library(rgl)
M<-read.table("M.txt", sep="\t", header = TRUE, colClasses = "numeric")
x<-read.table("x.txt", sep="\t", header = TRUE)
n<-ncol(M)
M[M<1]<-1
plot3d(x='', xlim=range(x$Time), ylim=c(1, n), zlim=(range(M)), box=FALSE)
sapply(seq(1,n), function(t){lines3d(x$Time, y=t*10, z=(M[,t])/10000, col=t/3+1)})
title3d(xlab="scan", ylab="time", zlab="intensity")
title3d(main ="Extracted Spectra Subset")
axes3d()
#axis3d(edge="x")
#axis3d(edge="y")
#axis3d(edge="z")
m <- read.table("~/Downloads/M.txt", head=T)

fudge <- 0.05
shifty <- function(m, fudge=1){
  shifts <- fudge * max(abs(apply(m, 2, diff))) * seq(0, ncol(m)-1)
  m + matrix(shifts, nrow=nrow(m), ncol=ncol(m), byrow=TRUE)
}
par(mfrow=c(1,2), mar=c(0,0,1,0))
cols <- colorRampPalette(blues9[4:9])(ncol(m))
matplot(shifty(m), t="l", lty=1, bty="n", yaxt="n", xaxt="n", ylab="", col=cols)
title("no overlap")
matplot(shifty(m, 0.05), t="l", lty=1, bty="n", yaxt="n", xaxt="n", ylab="", col=cols)
title("some overlap")
library(outliers)

shifty2 <- function(m, outliers = 10){
  tmp <- m
  for(ii in seq_len(outliers)) tmp <- rm.outlier(tmp, median = TRUE)
  shifts <- max(abs(apply(tmp, 2, diff))) * seq(0, ncol(m)-1)
  m + matrix(shifts, nrow=nrow(m), ncol=ncol(m), byrow=TRUE)
}

matplot(shifty2(m), t="l", lty=1, bty="n", yaxt="n", xaxt="n", ylab="", col=cols)