[英]How to draw a point in polar coordinates with negative r?
I am trying to draw two points in polar coordinates (r, theta), where r is a distance from the center, and theta the angle. 我试图在极坐标(r,theta)中绘制两个点,其中r是距离中心的距离,θ是角度。
The current solution does not work because I don't have a unique "origin" of the axes. 当前的解决方案不起作用,因为我没有轴的唯一“原点”。 When using coord_plane
, the origin of y is the center of the circle, but the origin of x seems to be the center of each radius. 使用coord_plane
,y的原点是圆的中心,但x的原点似乎是每个半径的中心。
What I am trying to do, is to plot in a system where the two points from the below example are symmetric with respect to the origin. 我想要做的是绘制一个系统,其中下面的例子中的两个点相对于原点是对称的。
library(ggplot2)
ggplot(data.frame(r = c(-100, 100) , theta = c(1, 1)),
aes(x = r, y= theta)) +
geom_text(aes(label = paste(round(r, 1),',', round(theta, 1)))) +
coord_polar(theta = 'y',
direction = -1,
start = -pi/2) +
scale_y_continuous(limits = c(0, 2*pi),
breaks = c(0, pi/2, pi, 3*pi/2 ),
labels = c('0', 'pi/2', 'pi', '3/2pi'))
UPDATE: 更新:
While the system that coord_polar
creates is probably not a "straight" polar systems, here is a quote from the grammar of graphics that in part explains in part the behavior of coord_polar
, and the reason why I had to fix the limits of y
: 虽然coord_polar
创建的系统可能不是一个“直的”极坐标系统,但这里引用的是图形语法,部分解释了coord_polar
的行为,以及我必须修正y
的极限的原因:
We could treat polar coordinates as an exception to the way all other scales are handled in this system. 我们可以将极坐标视为该系统中处理所有其他尺度的例外。 That is, we could interpret angular values ab- solutely as radians. 也就是说,我们可以将角度值解释为弧度。 This would make sense if all our graphics were mathemat- ical or engineering applications involving radians. 如果我们所有的图形都是涉及弧度的数学或工程应用,这将是有意义的。 We have chosen not to do this, however, so that we can hide scaling details when doing coordinate con- versions. 但是,我们选择不这样做,这样我们就可以在进行坐标转换时隐藏缩放细节。 This makes it easy, for example, to represent yearly time in polar co- ordinates. 例如,这使得在极坐标中表示年度时间变得容易。 In the polar coordinate conversion, therefore, we align 0 radians with the minimum scale value in data units (degrees, radians, proportions, etc.) and 2S radians with the maximum. 因此,在极坐标转换中,我们将0弧度与数据单位(度,弧度,比例等)中的最小刻度值和最大值的2S弧度对齐。 The cycle parameter, together with min and max parameters in the scale functions allows us to create polar graphs with more than one revolution if we wish. 循环参数以及缩放功能中的最小和最大参数允许我们根据需要创建具有多个旋转的极坐标图。
I don't fully understand what is your ultimate goal, but maybe the problem is that if you want r
to represent distance to the origin, then it cannot be negative. 我不完全明白你的最终目标是什么,但也许问题是,如果你想要r
来表示与原点的距离,那么它就不能是负面的。 What ggplot2 does with coord_polar() is just to deform the whole cartesian plane following polar coordinates. ggplot2对coord_polar()的作用就是在极坐标后变形整个笛卡尔平面。 This results in a "zero" that is actually the lower limit of your "radial" coordinate. 这导致“零”,实际上是“径向”坐标的下限。 You can see it clearly if you manually change its limits: 如果手动更改其限制,您可以清楚地看到它:
library(ggplot2)
ggplot(data.frame(r = c(-100, 100) , theta = c(1, 1)),
aes(x = r, y= theta)) +
geom_text(aes(label = paste(round(r, 1),',', round(theta, 1)))) +
coord_polar(theta = 'y',
direction = -1,
start = -pi/2) +
scale_y_continuous(limits = c(0, 2*pi),
breaks = c(0, pi/2, pi, 3*pi/2 ),
labels = c('0', 'pi/2', 'pi', '3/2pi')) +
scale_x_continuous(limits = c(-200, NA))
I don't know exactly what you mean with "symmetric with respect to the origin" but something this would be ok? 我不确切地知道你对“原点对称”的意思,但是这样可以吗?
library(ggplot2)
ggplot(data.frame(r = c(100, 100) , theta = c(1, 1 + pi)),
aes(x = r, y= theta)) +
geom_text(aes(label = paste(round(r, 1),',', round(theta, 1)))) +
coord_polar(theta = 'y',
direction = -1,
start = -pi/2) +
scale_y_continuous(limits = c(0, 2*pi),
breaks = c(0, pi/2, pi, 3*pi/2 ),
labels = c('0', 'pi/2', 'pi', '3/2pi')) +
scale_x_continuous(limits = c(0, NA))
Created on 2019-07-16 by the reprex package (v0.3.0) 由reprex包创建于2019-07-16(v0.3.0)
Adapting Elio Campitelli's answer as a function, you could use something like: 将Elio Campitelli的答案作为一种功能,你可以使用类似的东西:
plot_polar_signed <- function(r, theta) {
data2 <- data.frame(r2 = abs(r),
theta2 = theta + ifelse(r < 0, pi, 0))
ggplot(data2, aes(x = r2, y = theta2)) +
geom_text(aes(label = paste(round(r, 1),',', round(theta, 1)))) +
coord_polar(theta = 'y',
direction = -1,
start = -pi/2) +
scale_y_continuous(limits = c(0, 2*pi),
breaks = c(0, pi/2, pi, 3*pi/2 ),
labels = c('0', 'pi/2', 'pi', '3/2pi'))
}
plot_polar_signed(r = c(-100, 100), theta = c(1, 1))
Points with r < 0
are not defined in polar coordinates, since r
is the distance from the center. r < 0
点未在极坐标中定义,因为r
是距中心的距离。 Otherwise there would be 2 sets of coordinates for every point with r != 0
even when forcing theta to be in (0, 2pi)
. 否则,即使迫使theta处于(0, 2pi)
,每个点也会有2组坐标,其中r != 0
。
So you're really trying to plot in an extended polar coordinate system where negative r
is allowed and (r, theta)
and (-r, theta + pi)
are the same points. 所以你真的试图在一个扩展的极坐标系中绘制,其中允许负r
并且(r, theta)
和(-r, theta + pi)
是相同的点。
I suggest using a mapping from your system to the canonical polar coordinates. 我建议使用从您的系统到规范极坐标的映射。
canonical <- function(r, theta) {
if (r >= 0) {
c(r = r, theta = theta %% (2*pi))
} else {
c(r = -r, theta = (theta + pi) %% (2*pi))
}
}
Or shorter and vectorised: 或更短和矢量化:
canonical <- function(r, theta) {
list(r = abs(r),
theta = (theta + ifelse(r < 0, pi, 0)) %% (2 * pi))
}
This leaves valid polar coordinates untouched (except bringing theta in (0, 2pi)
if it is not already) and converts points with r < 0
to valid polar coordinates. 这使得有效的极坐标保持不变(除非将θ (0, 2pi)
带入(0, 2pi)
如果尚未),并将r < 0
点转换为有效的极坐标。
canonical(1, 0)
# untouched
canonical(-1, 0)
# becomes (1, pi)
The advantage is that you can use all the standard polar functions from ggplot
after converting. 优点是您可以在转换后使用ggplot
所有标准极性函数。
library(ggplot2)
df <- data.frame(r = c(-100, -50, 0, 50, 100) , theta = rep(1, 5))
df_polar <- as.data.frame(canonical(df$r, df$theta))
ggplot(df_polar,
aes(x = r, y = theta)) +
geom_text(aes(label = paste(round(r, 1),',', round(theta, 1)))) +
coord_polar(theta = 'y') +
scale_y_continuous(limits = c(0, 2*pi),
breaks = c(0, pi/2, pi, 3*pi/2 ),
labels = c('0', 'pi/2', 'pi', '3/2pi'))
Another option in the same vein is to convert from polar to cartesian coordinates to make it easy to draw straight lines. 同样的另一种选择是从极坐标转换为笛卡尔坐标,以便于绘制直线。 The mapping is x = r*cos(theta)
and y=r*sin(theta)
and, interestingly, is valid for negative r as well since cos(theta+pi) = -cos(theta)
and same for sin
. 映射是x = r*cos(theta)
和y=r*sin(theta)
并且有趣的是,对于负r也是有效的,因为cos(theta+pi) = -cos(theta)
和sin
相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.