【问题标题】:有没有一种方法可以在 ggplot2 中设置圆环图的 theta 轴刻度?
【发布时间】:2022-01-21 21:21:03
【问题描述】:

我想在 ggplot2 poar 图中显示 theta 轴的刻度线。但是,theta 轴中的axis.ticks.y 和axis.ticks.y 都不适用于theta 轴。任何帮助将不胜感激,谢谢

library(ggplot2)
df <- data.frame(
    start = c(0, 121, 241),
    end = c(120, 240, 359),
    group = letters[1:3]
)
# a example circular ring plot
base <- ggplot(df, aes(ymax = end, ymin = start, 
                       xmin = 0.8, xmax = 1, 
                       fill = group)) +
    geom_rect() +
    coord_polar(theta = "y") +
    xlim(c(0, 1))
base

# the tick of y axis can be changed
base + theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())

# set the tick of x axis not worked for the theta axis
base + theme(axis.ticks.x = element_line(color = "black", size = 2))

感谢@Vishal A.,Controlling ticks and odd text in a pie chart generated from a factor variable in ggplot2 的回答使用了panel.grid.major.y。但是,它将添加主要网格而不是像下面这样的刻度:

base + theme(panel.grid.major.y = element_line(colour = "black"))

reprex package (v2.0.1) 于 2021 年 12 月 20 日创建

【问题讨论】:

标签: r ggplot2


【解决方案1】:

您需要使用panel.grid.minor.y 而不是axis.ticks.y 才能更改刻度。 您的代码将如下所示:

base + theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())
base + theme(panel.grid.minor.y = element_line(color = "black", size = 1))

输出将如下所示:

【讨论】:

    【解决方案2】:

    我看到两个选项。您可以使用面板网格,但您需要隐藏它们。此解决方案的有用性取决于您预期的情节背景。我用的是白色的,当然这可以定制。

    第二个选项是使用注释来伪造刻度,例如,使用符号“|”。

    下面的代码中还有更小的 cmets。

    library(tidyverse)
    df <- data.frame(
      start = c(0, 121, 241),
      end = c(120, 240, 359),
      group = letters[1:3]
    )
    
    ggplot(df) +
      ## annotate with a rectangle, effectively covering your central hole
      annotate(geom = "rect", xmin = 0, xmax = 1, ymin = min(df$start), ymax = max(df$end),
               fill = "white") +
      ## move aes to the geom_layer 
      geom_rect(aes(ymax = end, ymin = start, 
                    xmin = 0.8, xmax = 1, 
                    fill = group)) +
      coord_polar(theta = "y") +
      xlim(c(0, 1)) +
      theme(panel.grid.major.y = element_line(colour = "black"))
    

      
    ## Option 2 - fake the ticks
    ## the position along the theta axis is defined by y
    ## you need to change the angle of your fake ticks according to the angle. 
    df_annot <- 
      data.frame(y = seq(0,300,100), x = Inf, angle = 360-seq(0,300,100))
    
    ggplot(df) +
      ## annotate with text, along your y
      ## by placing it beneath your geom_rect layer it will automatically be covered 
      geom_text(data = df_annot, aes(x, y, label = "|", angle = angle)) +
      ## move aes to the geom_layer 
      geom_rect(aes(ymax = end, ymin = start, 
                    xmin = 0.8, xmax = 1, 
                    fill = group)) +
      coord_polar(theta = "y") +
      xlim(c(0, 1)) 
    

    reprex package (v2.0.1) 于 2021 年 12 月 21 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 2014-11-26
      • 1970-01-01
      • 2022-11-14
      • 2019-07-14
      • 2021-04-15
      • 2020-05-14
      相关资源
      最近更新 更多