Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

r - Plotting a step function with only horizontal lines

I use ggplot2 to plot a step function using geom_step(). What I need now is to get rid of the vertical lines. This should be a quite common problem in math at least...

The docs do not mention such possibility .

Is there a hidden argument somewhere, or do I need to convert the data somehow so I can print individual lines for each datapoint?

TL;DR:

have ggplot(data,aes(x,y))+geom_step()

want ggplot(data,aes(x,y))+geom_step(lines=horizontal)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Read through this example. You may want to remove the vline and play with the various parameters -- see http://docs.ggplot2.org/current/.

library(ggplot2)
df <- data.frame(x=seq(0, 10), y=cumsum(rnorm(11)))
df$xend <- c(df$x[2:nrow(df)], NA)
df$yend <- df$y
p <- (ggplot(df, aes(x=x, y=y, xend=xend, yend=yend)) +
      geom_vline(aes(xintercept=x), linetype=2, color="grey") +
      geom_point() +  # Solid points to left
      geom_point(aes(x=xend, y=y), shape=1) +  # Open points to right
      geom_segment())  # Horizontal line segments
p

output from ggsave


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...