【问题标题】:Ggplot2: coord_polar() with geom_col()ggplot2:coord_polar() 和 geom_col()
【发布时间】:2019-05-15 09:54:01
【问题描述】:

我在使用coord_polar()geom_col() 时遇到问题。我的度数范围从 0 到 0, 20, 40... 340。如果我用coord_polar() 绘制它们,我有两个问题:

  • 值 0 和 340 相互接触,并且与其他列的间隙不同
  • “x 轴”略微偏移,因此 0 不指向“北”

查看这个最小的例子。



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轴范围,坐标系的旋转是正确的,但是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 创建

【问题讨论】:

    标签: r ggplot2 polar-coordinates


    【解决方案1】:

    由于每个条形占据的空间是 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)
    

    【讨论】:

      【解决方案2】:

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

      代码:

      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(10,30,50,...) 处,而不是在 c(0,20,40,...) 处绘制 geom_cols。我在 c(5, 15, 25,...) 处绘制 geom_labels。

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

      我得到这张图:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-06
        • 2020-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多