How to wrap around the polar coordinate limits in ggplot2?

我怕爱的太早我们不能终老 提交于 2019-12-05 02:47:50

Is it critical that the underlying coordinate system is polar? geom_arc_bar() from the ggforce package behaves as you would expect, so you can use it to draw arcs in arbitrary angles. But you have a cartesian coordinate system underneath, so you may have to draw the coordinate lines yourself if you need them.

library(ggforce)
library(dplyr)

data_deg <- data.frame(xmin = 340,
                   xmax = 380,
                   ymin = 0.4,
                   ymax = 0.6)

offset = 90 # by how much are angles offset
dir = 1 # should we go counterclockwise (1) or clockwise (-1)

# convert angles from degrees into radians, apply offset and direction
data_rad <- mutate(data_deg,
               xmin = dir*2*pi*(xmin + offset)/360,
               xmax = dir*2*pi*(xmax + offset)/360)

ggplot(data_rad) + geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = ymin, r = ymax,
                                start = xmin, end = xmax),
                            color = "darkblue", fill = "steelblue") +
  scale_x_continuous(limits = c(-1, 1)) +
  scale_y_continuous(limits = c(-1, 1)) +
  coord_fixed()

This doesn't solve the other issue you linked to, but in general you will probably find that converting coordinates from polar to Euclidean yourself gives you much more flexibility to get the plot looking the way you want it to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!