r geom多边形坐标

时间:2019-02-24 18:54:56

标签: r ggplot2

使用geom_polygon输入坐标的正确方法是什么?

在此绘图中,我想绘制2个矩形。

x轴从0.5到1.5,y轴从148到161。

另一个在x轴上从1.5到2.5,在y轴上从339到352。

下面的polygon()中的坐标有效,但是我想确认必须输入的坐标。在坐标下方输入每个矩形的底线,首先输入148 148 339 339,然后输入每个矩形的顶线:161 161 352352。这就是必须输入坐标的方式-首先输入底线,然后输入顶线?

<div class="footer">
   <p>Footer</p>
</div>

当我先为第一个矩形输入每个矩形的所有4个坐标,然后为第二个矩形输入所有4个坐标时,绘图是错误的:

.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: transparent;
  color: white;
  text-align: center;
}

谢谢。

1 个答案:

答案 0 :(得分:1)

这是一个基本的plot问题,而不是ggplot2

polygon试图绘制一个多边形,而不是要绘制的两个多边形。还假设这些点是有序的,并且最后一个点连接到第一个点

因此,如果您分离矩形并重新排列点,那么第二个示例可能会更好地工作,也许尝试

plot(1, type="n", main="test",
     xlim=c(0, 5), xlab="y",
     ylim=c(0, max(0, 400)), ylab="")

polygon(x=c(0.5, 1.5, 1.5, 0.5), y=c(148, 148, 161, 161),
        col = "red", border = NA)
polygon(x=c(1.5, 2.5, 2.5, 1.5), y=c(339, 339, 352, 352),
        col = "red", border = NA)

所以而不是

enter image description here

您会得到

enter image description here

这就是我想你想要的