如何在R中绘制一个开放的多边形

时间:2017-08-09 17:35:20

标签: r polygons

如何使用R创建开放多边形,如下面这两个示例: Two open polygons

2 个答案:

答案 0 :(得分:4)

对于您的用途有点模糊,但geom_pathggplot2是一种相当简单的方法。

首先创建一个数据框,xy是所有顶点的笛卡尔坐标:

df <- data.frame(plot = c(rep(1, 3), rep(2, 4)),
                 x = c(1, 0, 2, 0.5, 0, 2, 2.5),
                 y = c(1, 0, 0, 1, 0, 0, 1))

现在情节:

library(ggplot2)

ggplot(data = df, aes(x = x, y = y, group = 1)) +
  geom_path(size = 1) + 
  facet_grid(. ~ plot) +
  theme_bw()

enter image description here

如果您想要更灵活地进行自定义,我建议您创建一个单独的,更详细的问题。

答案 1 :(得分:2)

如果您不想使用ggplot,您可以使用原生plot函数执行与@DaveGruenewald类似的操作。

只是做:

plot(c(0.5, 0, 2), c(1, 0, 0), type='l')
plot(c(0.5, 0, 2, 2.5), c(1, 0, 0, 1), type='l')

获得类似于@DaveGruenewald的情节。