仅绘制水平线的阶跃函数

问题描述 投票:3回答:2

我使用ggplot2使用geom_step()绘制阶跃函数。我现在需要的是摆脱vertical行。至少在数学上,这应该是一个非常普遍的问题...

docs没有提及这种可能性。

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

TL; DR:

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

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

r ggplot2
2个回答
7
投票

通读本示例。您可能想要删除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

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9ZM1doei5wbmcifQ==” alt =“从ggsave输出”>


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.