簡體   English   中英

Plot 一步(分段常數) function in R

[英]Plot a step (piecewise constant) function in R

假設您有以下示例數據集:

rm(list=ls()); set.seed(1)
start<-c(0:4); stop<-c(1:5); Response<- round(10*runif(5) )
Dat<-data.frame(start,stop,Response)
Dat
  start stop Response
1     0    1 3
2     1    2 4
3     2    3 6
4     3    4 9
5     4    5 2

開始/停止是 x 軸。 如何在 R 中制作 plot 以便在某個開始和停止之間 y 軸采用恆定值作為響應。 即從時間 0 到 1,y 軸取值 3,...,從時間 4 到 5,y 軸取值 2。

這些都是你要找的嗎?

library(ggplot2)

Dat %>% 
    ggplot(aes(start, Response)) +
    geom_step()

Dat %>% 
    ggplot() +
    geom_segment(aes(x = start, xend = stop, y = Response, yend = Response)) 

暫無
暫無

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

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