Ggplot2: coord_polar() 和 geom_col()

Ggplot2: coord_polar() with geom_col()

我在将 coord_polar()geom_col() 一起使用时遇到问题。我的度值范围从 0 到 <360。假设有 20 步,所以 0, 20, 40... 340。如果我用 coord_polar() 绘制它们,我有两个问题:

查看这个最小示例。



suppressWarnings(library(ggplot2))


df <- data.frame(x = seq(0,359,20),y = 1)

ninety = c(0,90,180,270)

p <- ggplot(df, aes(x,y)) +
  geom_col(colour = "black",fill = "grey") +
  geom_label(aes(label = x)) + 
  scale_x_continuous(breaks = ninety) +
  geom_vline(xintercept = ninety, colour = "red") +
  coord_polar()

p

如果我设置了x轴范围,坐标系的旋转是正确的,但由于缺少space.

,0处的列消失了

p+scale_x_continuous(breaks = c(0,90,180,270),limits = c(0,360))
#> Scale for 'x' is already present. Adding another scale for 'x', which
#> will replace the existing scale.
#> Warning: Removed 1 rows containing missing values (geom_col).

reprex package (v0.2.1)

于 2019-05-15 创建

我让它更接近你想要的,但它有点乱,所以我不知道它是否是一个很好的解决方案。

代码:

df <- data.frame(x = seq(0,359,20),y = 1)

ggplot(df, aes(x+10,y, hjust=1)) +
  geom_col(colour = "black",fill = "grey") +
  geom_label(aes(x=x+5,label = x)) + 
  scale_x_continuous(breaks = c(0,90,180,270),limits = c(0,360)) +
  coord_polar() 

我现在不是在 c(0,20,40,...) 处绘制 geom_cols,而是在 c(10,30,50,...) 处绘制它们。我正在绘制 geom_labels 在 c(5, 15, 25,...).

图表底部的标签定位仍然不完美,因为 180deg 不是南方。

我得到这张图:

由于每个条形占据的 space 是 20 度,您可以在比例和坐标上将事物移动一半:

ggplot(df, aes(x,y)) +
  geom_col(colour = "black",fill = "grey") +
  geom_label(aes(label = x)) + 
  scale_x_continuous(breaks = ninety,
                     limits = c(-10, 350)) + # shift limits by 10 degrees
  geom_vline(xintercept = ninety, colour = "red") +
  coord_polar(start = -10/360*2*pi) # offset by 10 degrees (converted to radians)