如何环绕ggplot2中的极坐标限制? [英] How to wrap around the polar coordinate limits in ggplot2?

查看:227
本文介绍了如何环绕ggplot2中的极坐标限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个圆形空间,其中0度和360度角相等.我想在此空间中绘制矩形,以便矩形可以越过该值.但是,我在使用ggplot2时遇到了麻烦.

I have a circular space where angles 0 and 360 are equivalent. I want to plot rectangles in this space such that rectangles can cross this value. However, I am having trouble with ggplot2.

base <- ggplot() +
  scale_x_continuous(breaks = seq(45, 360, 45), limits = c(0, 360)) +
  scale_y_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
  coord_polar(theta = "x", start = 1.5 * pi, direction = -1)

1.尝试使用超过xlim的值进行绘制:

base + geom_rect(aes(xmin = 340, xmax = 380, ymin = 0.4, ymax = 0.6), 
  color = "darkblue", fill = "steelblue")
#> Warning message:
#> Removed 1 rows containing missing values (geom_rect). 

xlim之外的所有值都将被删除,因此不起作用.

All values outside of xlim are removed so this does not work.

2.尝试使用重新缩放的值进行绘制

base + geom_rect(aes(xmin = 340, xmax = 380 %% 360, ymin = 0.4, ymax = 0.6), 
  color = "darkblue", fill = "steelblue")

这至少会产生一个图,但与我想要的相反.而不是从340到380 CCW,这是340到20 CW.

This at least produces a plot, but plots the opposite of what I want. Instead of going from 340 to 380 CCW, this plots 340 to 20 CW.

3.尝试将其绘制为两个相连的元素

  base + geom_rect(aes(xmin = c(350, 0), xmax = c(360, 10), ymin = 0.4, ymax = 0.6), 
    color = "darkblue", fill = "steelblue")

这显示了我想要的矩形,但是由于笔划线的夹角为0/360,并且现在我必须将每个矩形表示为两个矩形,因此这不能令人满意.

This shows the rectangle where I want it, but this is not satisfying as a solution because of the stroke lines at angle 0/360 and because I now have to represent each rectangle as two rectangles.

4.尝试1使用缩放而不是裁剪

ggplot() +
  scale_x_continuous(breaks = seq(45, 360, 45)) +
  scale_y_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
  coord_cartesian(xlim = c(0, 360)) +
  coord_polar(theta = "x", start = 1.5 * pi, direction = -1) +
  geom_rect(aes(xmin = 340, xmax = 380, ymin = 0.4, ymax = 0.6), 
    color = "darkblue", fill = "steelblue")

这似乎失去了缩放和限制.

This seems to lose the zooming and the limits.

5.尝试2使用缩放而不是裁剪

ggplot() +
  scale_x_continuous(breaks = seq(45, 360, 45)) +
  scale_y_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
  coord_polar(theta = "x", start = 1.5 * pi, direction = -1) +
  coord_cartesian(xlim = c(0, 360)) +
  geom_rect(aes(xmin = 340, xmax = 380, ymin = 0.4, ymax = 0.6), 
    color = "darkblue", fill = "steelblue")

这样可以正确完成缩放,但是会覆盖极坐标系.

This accomplishes the zooming correctly, but overwrites the polar coordinate system.

如果任何人都可以为这个问题提供解决方案或想法,我将不胜感激.同样,我正在寻找看起来像#3但没有内部笔触并且不需要使用两个矩形的东西.

If anyone can provide a solution or ideas for this problem, I would really appreciate it. Again, I am looking for something that looks like #3 but without the inner stroke and without needing to use two rectangles.

问题是相关的,也未得到答复.

This question is related and also unanswered.

推荐答案

基础坐标系是极坐标是否至关重要? ggforce包中的geom_arc_bar()的行为与您期望的一样,因此可以使用它以任意角度绘制圆弧.但是,下面有一个笛卡尔坐标系,因此,如果需要,您可能必须自己绘制坐标线.

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.

这篇关于如何环绕ggplot2中的极坐标限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆