仅用水平线绘制阶梯函数 [英] Plotting a step function with only horizontal lines

查看:73
本文介绍了仅用水平线绘制阶梯函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

想要 ggplot(data,aes(x,y))+ geom_step(lines = horizo​​ntal)

推荐答案

通读此示例.您可能要删除vline并使用各种参数-请参阅 http://docs.ggplot2.org/当前/.

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

这篇关于仅用水平线绘制阶梯函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆