简体   繁体   English

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

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

Suppose you have the following sample dataset:假设您有以下示例数据集:

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

The start/stop is the x-axis.开始/停止是 x 轴。 How can I make a plot in R such that between a certain start and stop the y axis takes the constant value in response.如何在 R 中制作 plot 以便在某个开始和停止之间 y 轴采用恒定值作为响应。 ie from time 0 to 1 the y-axis takes the value 3, ... ,from time 4 to 5 the y-axis takes the value 2.即从时间 0 到 1,y 轴取值 3,...,从时间 4 到 5,y 轴取值 2。

Are either of these what you're looking for?这些都是你要找的吗?

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