简体   繁体   English

r geom多边形坐标

[英]r geom polygon coordinates

What is the correct way to enter coordinates with geom_polygon? 用geom_polygon输入坐标的正确方法是什么?

In this plot I would like to draw 2 rectangles. 在此绘图中,我想绘制2个矩形。

One going from .5 to 1.5 on the x axis and 148 to 161 on the y axis. 一个在x轴上从0.5到1.5,在y轴上从148到161。

The other going from 1.5 to 2.5 on the x axis and 339 to 352 on the y axis. 另一个在x轴上从1.5到2.5,在y轴上从339到352。

The coordinates in the polygon() below work but I'd like to confirm how the coordinates must be entered. 下面的polygon()中的坐标可以工作,但是我想确认必须如何输入坐标。 Below the coordinates are entered with the bottom line of each rectangle first 148 148 339 339 are entered and then the top line of each rectangle are entered: 161 161 352 352. Is that how the coordinates must be entered - bottom line first then top line? 在坐标下方输入每个矩形的底线,首先输入148 148 339 339,然后输入每个矩形的顶线:161 161 352352。这就是必须输入坐标的方式-首先输入底线,然后输入顶线?

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, 2.5, 2.5, 1.5, 1.5, 0.5),
y= c(148, 148, 339, 339, 352, 352, 161, 161),
col = "blue", border = NA)

When I enter all 4 coordinates for each rectangle for the first rectangle first and then all 4 coordinates for the second rectangle the plot is wrong: 当我先为第一个矩形的每个矩形输入所有4个坐标,然后为第二个矩形的所有4个坐标输入时,绘图错误:

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

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

Thank you. 谢谢。

This is a base plot question rather than ggplot2 这是基本plot问题,而不是ggplot2

polygon is trying to to draw a single polygon rather than the two you want. polygon试图绘制一个多边形,而不是您想要的两个。 It is also assuming that the points are in order, and that the last point is connected to the first point 还假设这些点是有序的,并且最后一个点连接到第一个点

So your second example might work better if you separated the rectangles and reordered the points, perhaps trying 因此,如果您分离矩形并重新排列点,则第二个示例可能会更好地工作,也许尝试

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)

so rather than 所以而不是

在此处输入图片说明

you would get 你会得到

在此处输入图片说明

which is what I assume you want 这就是我想你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM