rm(list = ls())
library(ggplot2)

d = data.frame(x= seq(1,8,1), y=c(1,2,3,5,6,7,9,11))
d$label <- rep(LETTERS[1:2],4)
d
##   x  y label
## 1 1  1     A
## 2 2  2     B
## 3 3  3     A
## 4 4  5     B
## 5 5  6     A
## 6 6  7     B
## 7 7  9     A
## 8 8 11     B
 p1 <- ggplot(data=d, aes(x=x, y=y, color= label)) +
  geom_step() 
 p1

 p2 <- p1 + geom_step(direction="vh", linetype=3,size = 1) +
  geom_point(color="red")
p2

p3 <- p2 + scale_x_continuous(expand = c(0.01, 0.01), limits = c(1,8),breaks = 1:8) +
    scale_y_continuous(expand = c(0.01, 0.01), limits = c(1,11),breaks = 1:11)
p3 

p4 <- p3 + theme(legend.key = element_rect(colour = NA, fill = NA),
          #plot.title = element_text(colour="black", size=13, 
          #                         face="plain",family="sans",
          #                         hjust = 0.05),
          #plot.margin=unit(c(0.5,0,0.5,0),"cm"),
          legend.position= "top",
          legend.direction= "horizontal",
          legend.title = element_text(colour="black", size=13, 
                                      face="plain",family="sans"),
          legend.text = element_text(colour="black", size=13, 
                                     face="plain",family="sans"),
          legend.background = element_blank(),
          legend.box.background =  element_blank(),
          #legend.key.size = unit(1, "cm"),
          #legend.key.width = unit(0.4, "cm"),
          #legend.key.height=unit(1, "cm"),
          #axis.line = element_blank(),
          #plot.background = element_blank(),
          panel.background = element_blank(),
          panel.border = element_rect(colour = NA, fill= NA, size=1),
          panel.grid = element_blank(),
          #panel.grid = element_blank(),
          axis.title.y = element_text(color="black", size=13, face="plain",
                                      family="sans",angle = 90,hjust = 0.5),
          #axis.ticks = element_blank(),
          axis.text.x = element_text(color="black", size=13, face="plain",
                                     family="sans",angle = 45,hjust = 1, vjust = 1),
          axis.text.y = element_text(color="black", size=13, face="plain",family="sans",
                                     angle = 0,hjust = 0.5),
          #strip.text = element_blank(),
          axis.ticks =  element_line(size= 0.5),
          axis.ticks.length = unit(3, "pt"),
          axis.line = element_line(size = 0.5, colour = "black"),
          aspect.ratio = 1) 
p4  

ggsave(paste0(Sys.Date(),"-geom_step.tiff"), 
       plot = last_plot(), device = NULL,
       scale = 1, width = 13, height = 13, units ="cm",dpi = 300, 
       limitsize = TRUE,compression = "lzw")
 
 
ggplot(data=d, aes(x=x, y=y, color= label)) + 
  geom_line(arrow = arrow(angle = 15, ends = "both", type = "closed"))

##############################
seq(1, 9, by = 2)  
## [1] 1 3 5 7 9
replicate(2, "my_string")
## [1] "my_string" "my_string"
rep("my_string", 2)
## [1] "my_string" "my_string"
##############################
comb = data.frame(time=rexp(20),n1=rep(1:10,each=2),n2=seq(from=2, to=11.5,by=0.5))
comb
##         time n1   n2
## 1  1.3317874  1  2.0
## 2  0.6893640  1  2.5
## 3  1.3059099  2  3.0
## 4  0.5975933  2  3.5
## 5  0.1097735  3  4.0
## 6  0.2109028  3  4.5
## 7  0.1346740  4  5.0
## 8  0.1834042  4  5.5
## 9  1.3104451  5  6.0
## 10 2.7753348  5  6.5
## 11 0.1630040  6  7.0
## 12 0.2208917  6  7.5
## 13 4.2922101  7  8.0
## 14 0.1773071  7  8.5
## 15 0.7008004  8  9.0
## 16 2.6620596  8  9.5
## 17 1.6444611  9 10.0
## 18 2.2306011  9 10.5
## 19 0.1260962 10 11.0
## 20 1.2866077 10 11.5
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
comb$ctime <- cumsum(comb$time)
ggplot(comb) + 
geom_rect(aes(xmin = ctime, xmax = lead(ctime), 
                ymin = n1, ymax = n2), 
            fill = "blue", alpha = 0.4) +
  geom_step(aes(x=ctime, y=n1))+ 
  geom_step(aes(x=ctime, y=n2)) 
## Warning: Removed 1 rows containing missing values (geom_rect).

##ref https://stackoverflow.com/questions/43587411/area-between-step-wise-functions-in-r