簡體   English   中英

帶間隔的R段功能

[英]R segment function with intervals

我在遇到特定問題時無法理解基本圖形中的細分功能。

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處,我將知道該怎么做。 但是在間隔情況下我該怎么辦?

您需要提供坐標x0y0以及x1y1 向量 ,它們分別是從x和y繪制的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)

這將產生以下情節

在此處輸入圖片說明

快速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 ) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM