【问题标题】:Are there easy way to set limit of vertical or horizontal line in ggplot?有没有简单的方法在ggplot中设置垂直或水平线的限制?
【发布时间】:2019-02-24 12:20:15
【问题描述】:

我想绘制geom_vlinegeom_hline的一部分,代码如下:

df <- data.frame(x=1:10,y=1:10)
plt <- ggplot(data=df)+
  geom_point(aes(x=x,y=y))+
  geom_vline(aes(xintercept=x[2]))+
  geom_hline(aes(yintercept=y[2]))

我希望将左侧和下部线显示为交叉点。 但geom_vline中没有xlimylim等参数

【问题讨论】:

  • geom_segment 让您绘制具有 x 和 y 限制的线条
  • 使用geom_segment,您可以将上限设置为Inf,以确保它们延伸到绘图边缘
  • 得到它!非常感谢。

标签: r ggplot2


【解决方案1】:

您可以使用geom_line

library(ggplot2)

df <- data.frame(x=1:10,y=1:10)
plt <- ggplot(data=df)+
  geom_point(aes(x = x, y = y))+
  geom_line(data = data.frame(x = c(2, Inf), y = c(2, 2)), aes(x = x , y = y)) +
  geom_line(data = data.frame(x = c(2, 2), y = c(2, Inf)), aes(x = x, y = y)) 

geom_segment。他们产生了相同的情节。

plt <- ggplot(data=df)+
  geom_point(aes(x = x, y = y))+
  geom_segment(aes(x = 2 , y = 2, xend = Inf, yend = 2)) +
  geom_segment(aes(x = 2 , y = 2, xend = 2, yend = Inf))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    相关资源
    最近更新 更多