如何在R中的绘图上画线?

发布于 2025-01-07 13:46:45 字数 850 浏览 4 评论 0原文

我需要从文本文件中存储的数据中绘制线条。 到目前为止,我只能在图表上绘制点,我希望将它们作为线条(折线图)。

这是代码:

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

请帮我更改这行代码:

points(i, y = pupil_data$PupilLeft[i], type = "o", col = "red")

从数据中绘制线条。

这是文件中的数据:

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 

I need to draw lines from the data stored in a text file.
So far I am able only to draw points on a graph and i would like to have them as lines (line graph).

Here's the code:

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

Please help me change this line of code:

points(i, y = pupil_data$PupilLeft[i], type = "o", col = "red")

to draw lines from the data.

Here is the data in the file:

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2025-01-14 13:46:45

默认情况下,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")
}

然后您可以将 myaddpoint 调用包装在 for 循环中,并进行任何您需要的测试来决定是否实际绘制该点。 makeaddpoint 返回的函数将为您跟踪绘图索引。

这是类 Lisp 语言的正常编程。如果您发现它令人困惑,您可以在不使用闭包的情况下执行此操作,但是您需要处理递增索引并在循环中“手动”存储前一个点值。

By default, R will plot a single vector as the y coordinates, and use a sequence for the x coordinates. So to make the plot you are after, all you need is:

plot(pupil_data$PupilLeft, type = "o")

You haven't provided any example data, but you can see this with the built-in iris data set:

plot(iris[,1], type = "o")

This does in fact plot the points as lines. If you are actually getting points without lines, you'll need to provide a working example with your data to figure out why.

EDIT:

Your original code doesn't work because of the loop. You are in effect asking R to plot a line connecting a single point to itself each time through the loop. The next time through the loop R doesn't know that there are other points that you want connected; if it did, this would break the intended use of points, which is to add points/lines to an existing plot.

Of course, the line connecting a point to itself doesn't really make sense, and so it isn't plotted (or is plotted too small to see, same result).

Your example is most easily done without a loop:

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

If you really do need to use a loop, then the coding becomes more involved. One approach would be to use a closure:

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

You can then wrap the myaddpoint call in the for loop with whatever testing you need to decide whether or not you will actually plot that point. The function returned by makeaddpoint will keep track of the plot indexing for you.

This is normal programming for Lisp-like languages. If you find it confusing you can do this without a closure, but you'll need to handle incrementing the index and storing the previous point value 'manually' in your loop.

不羁少年 2025-01-14 13:46:45

经验丰富的 R 程序员非常厌恶在不需要时使用 for 循环。这是一个无循环使用名为segment的向量化函数的示例,该函数采用 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

There is a strong aversion among experienced R coders to using for-loops when not really needed. This is an example of a loop-less use of a vectorized function named segments that takes 4 vectors as arguments: 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文