【问题标题】:How to make ggplot lines run to the edge?如何让ggplot线跑到边缘?
【发布时间】:2016-10-11 18:13:39
【问题描述】:

我有想要用线图显示的数据(随时间变化的深度)。为清楚起见,我想放大一个部分,但仍向用户显示数据继续超出绘图范围。所以我希望线条停在情节的边缘,而不是最后一点。这在基本图形中很简单,但我不能让它在 ggplot 中工作。下面是一个带有 base 的示例:

d <- data.frame(x = 1:10, y = 1:10)
plot(d$x, d$y, xlim = c(2,9))
lines(d$x, d$y)

与 ggplot 类似的方法不起作用;线条停在最后一点。示例:

d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_line() + xlim(2,9)

有没有办法让线条在 ggplot 中运行到情节的边缘?谢谢。

【问题讨论】:

    标签: r plot ggplot2 line-plot


    【解决方案1】:

    试试这个

    d <- data.frame(x = 1:10, y = 1:10)
    ggplot(d, aes(x, y)) + geom_point() + geom_line() + coord_cartesian(xlim = c(0,9))
    

    【讨论】:

      【解决方案2】:

      如果你想要一条直线,abline 是最简单的

      d <- data.frame(x = 1:10, y = 1:10)
      ggplot(d, aes(x, y)) + geom_point() + geom_abline() + xlim(2,9)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多