1

在我的特定问题的上下文中,我无法理解基本图形中的分段功能。

x <- 0:1000
y <- c(0, 40000, 80000) 

现在我想在 y=0 处绘制一条从 0 到 200 的线的图。在 y=40000 处从 200 到 500 的另一行,在 y=80000 处从 500 到 1000 的最后一行。

plot(x,y,type="n")
segments(0,0,200,40000,200,40000,500,8000,1000)
points(0,0,200,40000,200,40000,500,8000,1000)
points(0,0,200,40000,200,40000,500,8000,1000) 

我认为在这里定义确切的细分是错误的。如果 x 在哪里 0:3 我会知道该怎么做。但是在间隔的情况下我该怎么办?

4

2 回答 2

3

您需要提供坐标向量x0y0x1y1它们分别是要从中绘制的 x 和 y 坐标。考虑以下工作示例:

x <- seq(0, 1000, length = 200)
y <- seq(0, 80000, length = 200)
plot(x,y,type="n")

from.x <- c(0, 200, 500)
to.x   <- c(200, 500, 1000)
to.y   <- from.y <- c(0, 40000, 80000) # from and to y coords the same

segments(x0 = from.x, y0 = from.y, x1 = to.x, y1 = to.y)

这会产生以下情节

在此处输入图像描述

于 2014-06-24T21:32:26.823 回答
0

一个快速的ggplot版本:

library(ggplot2)
x <- seq(0, 1000, length = 200)
y <- seq(0, 80000, length = 200)
plot(x,y,type="n")

dta <- data.frame( x= from.x,y=from.y, xend=to.x, yend=to.y )
ggplot( dta, aes( x=x, y=y, xend=xend, yend=yend )) +
  geom_segment()+
  geom_point( shape=16, size=4 ) +
  geom_point( aes( x=xend, y=yend ), shape=1, size=4 ) 
于 2014-06-24T21:43:33.927 回答