仅使用水平线绘制阶梯函数

时间:2015-06-09 10:38:49

标签: r ggplot2

我使用 ggplot2 使用geom_step()绘制步进函数。 我现在需要的是摆脱垂直线。 这应该是数学中至少常见的问题......

docs没有提到这种可能性。

某处有隐藏的参数,还是我需要以某种方式转换数据,以便为每个数据点打印单独的行?

TL; DR:

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

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

2 个答案:

答案 0 :(得分:5)

通读这个例子。您可能希望删除vline并使用各种参数 - 请参阅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

答案 1 :(得分:0)

您可以改为使用带有组参数的geom_line。

ggplot(data,aes(x,y,group=as.factor(x)))+geom_line()