如何在情节垂直线段?

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

我有一个情节(X,Y),我想垂直线添加在x = 2仅从Y = 1〜4。我想使用的线()函数,但我无法限制的y范围。

什么是简单的方法来做到这一点?

r
3个回答
2
投票

下面是使用plotlines的一个简单的例子。若要从(2, 1)再接(2, 4),你必须提供的X坐标和Y坐标(2, 2)(1, 4)

plot(1:5)
lines(c(2, 2), c(1, 4))

enter image description here


0
投票

ggplot2提供了一个非常简单的解决办法呢!

library(ggplot2)
# Create some dummy data
data.frame(X = rpois(n = 10, lambda = 3), 
           Y = rpois(n = 10, lambda = 2)) %>% 
# Pipe to ggplot
    ggplot(aes(X, Y)) + 
    geom_point() + 
    geom_segment(aes(x = 1, xend = 1, y = 1, yend = 4), color = "red")

在美学打电话geom_segment()您可以选择您的x和y参数的起点和终点。然后,可以容易地通过简单地添加+ geom_segment(aes(...))上述代码的末尾添加多个段。


0
投票

为了完整起见,还存在R A基的图形功能,将执行以下操作:段(X0,Y0,X1,Y1):

plot(1:5)
segments(2,1,2,4)
© www.soinside.com 2019 - 2024. All rights reserved.