代码之家  ›  专栏  ›  技术社区  ›  Bakaburg

带极坐标的条形图,在ggplot中调整为锥形

  •  0
  • Bakaburg  · 技术社区  · 6 年前

    我想用ggplot创建一个得分图,就像你在角色扮演游戏中经常看到的那样,有正的和负的得分。

    为此,我使用 geom_col coord_polar . 问题是,由于整个绘图空间坐标系都变形了,所以这些条不是矩形而是梯形。

    enter image description here

    是否可以对此进行调整以获得适当的均匀条(并在绘图中心添加一些空间以避免重叠)?

    额外要求:根据x轴上的值的数量,是否可以使用多边形网格来代替圆形网格?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Z.Lin    6 年前

    可以使用线段而不是条。厚度将取决于您选择的大小,而不是坐标系:

    # sample dataset
    set.seed(333)
    n.dim = 9
    df <- data.frame(
      x = paste("attribute", seq(1, n.dim), sep = "."),
      score = rnorm(n.dim)
    )
    
    ggplot(df,
           aes(x = x, y = score, 
               xend = x, yend = 0,
               col = score >= 0)) +
      geom_segment(size = 15) +                 # adjust size for different bar thickness
      scale_y_continuous(expand = c(0.1, 0)) +  # increase space at plot centre
      coord_polar() +
      theme_minimal() +
      theme(legend.position = "none",
            axis.title = element_blank(),
            axis.text.y = element_blank(),
            panel.grid.major.x = element_blank())
    

    plot

    plot