I am trying to draw two points in polar coordinates (r, theta), where r is a distance from the center, and theta the angle.
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.
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
:
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. 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.
In the polar coordinate system, the ordered pair will now be (r, θ). The ordered pair specifies a point's location based on the value of r and the angle, θ, from the polar axis. The value of r can be positive, negative, or zero.
As (-3,3) lies in I Quadrant, θ=34π(r,θ)⟹(32, ,43π)or,(32 ,135°)
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. What ggplot2 does with coord_polar() is just to deform the whole cartesian plane following polar coordinates. 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With