Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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_Graph_Plot_Line - Fatal编程技术网

如何在R中绘制绘图线?

如何在R中绘制绘图线?,r,graph,plot,line,R,Graph,Plot,Line,我需要从存储在文本文件中的数据中绘制线条。 到目前为止,我只能在一个图形上画点,我想他们作为线图 代码如下: pupil_data <- read.table("C:/a1t_left_test.dat", header=T, sep="\t") max_y <- max(pupil_data$PupilLeft) plot(NA,NA,xlim=c(0,length(pupil_data$PupilLeft)), ylim=c(2,max_y)); for (i in 1

我需要从存储在文本文件中的数据中绘制线条。 到目前为止,我只能在一个图形上画点,我想他们作为线图

代码如下:

pupil_data <- read.table("C:/a1t_left_test.dat", header=T, sep="\t") 

max_y <- max(pupil_data$PupilLeft)

plot(NA,NA,xlim=c(0,length(pupil_data$PupilLeft)), ylim=c(2,max_y)); 

for (i in 1:(length(pupil_data$PupilLeft) - 1)) 
{
    points(i, y = pupil_data$PupilLeft[i], type = "o", col = "red", cex = 0.5, lwd = 2.0)
}
从数据中绘制线条

以下是文件中的数据:

PupilLeft  
3.553479    
3.539469    
3.527239    
3.613131    
3.649437    
3.632779    
3.614373    
3.605981    
3.595985    
3.630766    
3.590724    
3.626535    
3.62386 
3.619688    
3.595711    
3.627841    
3.623596    
3.650569    
3.64876 

默认情况下,R将绘制单个向量作为y坐标,并使用序列作为x坐标。因此,要绘制所需的绘图,只需:

plot(pupil_data$PupilLeft, type = "o")
您尚未提供任何示例数据,但您可以通过内置iris数据集看到这一点:

plot(iris[,1], type = "o")
这实际上将点绘制为直线。如果您实际上得到的是没有直线的点,那么您需要提供一个使用数据的工作示例来找出原因

编辑:

由于循环,您的原始代码无法工作。实际上,您要求R绘制一条线,在每次通过循环时将单个点连接到自身。下一次通过循环时,R不知道还有其他要连接的点;如果这样做,将破坏点的预期用途,即向现有绘图添加点/线

当然,将一个点连接到它自身的线实际上没有意义,因此它没有被绘制,或者被绘制得太小以至于看不见,同样的结果

您的示例最容易在没有循环的情况下完成:

PupilLeft <- c(3.553479 ,3.539469 ,3.527239 ,3.613131 ,3.649437 ,3.632779 ,3.614373
               ,3.605981 ,3.595985 ,3.630766 ,3.590724 ,3.626535 ,3.62386 ,3.619688
               ,3.595711 ,3.627841 ,3.623596 ,3.650569 ,3.64876)

plot(PupilLeft, type = 'o')
如果您确实需要使用循环,那么编码将变得更加复杂。一种方法是使用关闭:

makeaddpoint <- function(firstpoint){
  ## firstpoint is the y value of the first point in the series

  lastpt <- firstpoint
  lastptind <- 1

  addpoint <- function(nextpt, ...){
    pts <- rbind(c(lastptind, lastpt), c(lastptind + 1, nextpt))
    points(pts, ... )
    lastpt <<- nextpt
    lastptind <<- lastptind + 1
  }

  return(addpoint)

}

myaddpoint <- makeaddpoint(PupilLeft[1])

plot(NA,NA,xlim=c(0,length(PupilLeft)), ylim=c(2,max(PupilLeft)))

for (i in 2:(length(PupilLeft))) 
{
    myaddpoint(PupilLeft[i], type = "o")
}
然后,您可以在for循环中封装myaddpoint调用,使用您需要的任何测试来决定是否实际绘制该点。makeaddpoint返回的函数将为您跟踪绘图索引


这是Lisp类语言的常规编程。如果您感到困惑,您可以不使用闭包来执行此操作,但您需要处理递增索引并在循环中“手动”存储上一个点值。

默认情况下,R将绘制单个向量作为y坐标,并使用序列作为x坐标。因此,要绘制所需的绘图,只需:

plot(pupil_data$PupilLeft, type = "o")
您尚未提供任何示例数据,但您可以通过内置iris数据集看到这一点:

plot(iris[,1], type = "o")
这实际上将点绘制为直线。如果您实际上得到的是没有直线的点,那么您需要提供一个使用数据的工作示例来找出原因

编辑:

由于循环,您的原始代码无法工作。实际上,您要求R绘制一条线,在每次通过循环时将单个点连接到自身。下一次通过循环时,R不知道还有其他要连接的点;如果这样做,将破坏点的预期用途,即向现有绘图添加点/线

当然,将一个点连接到它自身的线实际上没有意义,因此它没有被绘制,或者被绘制得太小以至于看不见,同样的结果

您的示例最容易在没有循环的情况下完成:

PupilLeft <- c(3.553479 ,3.539469 ,3.527239 ,3.613131 ,3.649437 ,3.632779 ,3.614373
               ,3.605981 ,3.595985 ,3.630766 ,3.590724 ,3.626535 ,3.62386 ,3.619688
               ,3.595711 ,3.627841 ,3.623596 ,3.650569 ,3.64876)

plot(PupilLeft, type = 'o')
如果您确实需要使用循环,那么编码将变得更加复杂。一种方法是使用关闭:

makeaddpoint <- function(firstpoint){
  ## firstpoint is the y value of the first point in the series

  lastpt <- firstpoint
  lastptind <- 1

  addpoint <- function(nextpt, ...){
    pts <- rbind(c(lastptind, lastpt), c(lastptind + 1, nextpt))
    points(pts, ... )
    lastpt <<- nextpt
    lastptind <<- lastptind + 1
  }

  return(addpoint)

}

myaddpoint <- makeaddpoint(PupilLeft[1])

plot(NA,NA,xlim=c(0,length(PupilLeft)), ylim=c(2,max(PupilLeft)))

for (i in 2:(length(PupilLeft))) 
{
    myaddpoint(PupilLeft[i], type = "o")
}
然后,您可以在for循环中封装myaddpoint调用,使用您需要的任何测试来决定是否实际绘制该点。makeaddpoint返回的函数将为您跟踪绘图索引


这是Lisp类语言的常规编程。如果您感到困惑,您可以不使用闭包来完成此操作,但您需要处理增加索引并在循环中“手动”存储上一个点值。

有经验的R编码器强烈反对在不真正需要时使用for循环。这是一个无循环使用名为segments的向量化函数的示例,该函数采用4个向量作为参数:x0、y0、x1、y1

npups <-length(pupil_data$PupilLeft)
segments(1:(npups-1), pupil_data$PupilLeft[-npups],  # the starting points
           2:npups, pupil_data$PupilLeft[-1] )        # the ending points

有经验的程序员强烈反对在不需要时使用for循环。这是一个无循环使用名为segments的向量化函数的示例,该函数采用4个向量作为参数:x0、y0、x1、y1

npups <-length(pupil_data$PupilLeft)
segments(1:(npups-1), pupil_data$PupilLeft[-npups],  # the starting points
           2:npups, pupil_data$PupilLeft[-1] )        # the ending points

如果你想画线,为什么要使用点函数?在绘图函数中添加一个type='l',然后使用线。数据:PupilLeft3.553479 3.539469 3.527239 3.61313.649437 3.632779 3.614373 3.6059981 3.595985 3.630766 3.590724 3.626535 3.62386 3.619688 3.595711 3.627841 3.623596 3.650569 3.64876我尝试了这两种方法当我使用“线”时,解决方案没有结果。“plot”fcn中的“type='l”在之后使用行时也会发生同样的情况。@user1221812:是您的循环弄脏了代码。请参阅我修改后的答案。如果要绘制线,为什么要使用点函数?在绘图函数中添加一个type='l',然后使用线。数据:PupilLeft3.553479 3.539469 3.527239 3.613131 3.649437 3.632779 3.614373 3.605981 3.595985 3.630766 3.590724 3.626535 3.62386 3.619688 3.595711 3.627841 3.623596 3.650693.64876我尝试了这两种解决方案,但当我使用“线路”时,没有结果。同样的事情也发生在'type='l'中
e“plot”fcn,同时在后面使用行。@user1221812:是你的循环弄脏了你的代码。请看我修改后的答案。我必须逐个处理数据,因此我必须使用for循环。所以我没有数据向量,而是像瞳孔数据$PupilLeft[I]那样提取的单个点。我在报告中提供了数据OP@user1221812在R中,通过循环点并依次绘制每个点来画一条线根本不是这样做的。首先,这将是非常低效的。如果你需要处理数据,先这样做,然后像泰勒演示的那样画出来。这与我无关。我只是想要一个线条图,其中线条连接我用瞳孔数据$PupilLeft[I]提取的点。所以我有一个点,我需要用一条线连接到另一个点,并在循环中重复这个过程。我必须逐个处理数据,因此我必须使用for循环。所以我没有数据向量,而是像瞳孔数据$PupilLeft[I]那样提取的单个点。我在报告中提供了数据OP@user1221812在R中,通过循环点并依次绘制每个点来画一条线根本不是这样做的。首先,这将是非常低效的。如果你需要处理数据,先这样做,然后像泰勒演示的那样画出来。这与我无关。我只是想要一个线条图,其中线条连接我用瞳孔数据$PupilLeft[I]提取的点。所以我有一个点,我需要用一条线连接到另一个点,然后在循环中重复这个过程。