【问题标题】:duplicating and edit a discrete axis in ggplot2 - 2021在 ggplot2 - 2021 中复制和编辑离散轴
【发布时间】:2021-11-27 01:07:50
【问题描述】:

关于同一主题还有其他几篇文章,但提出的解决方案不适合我的情况。 给定这样的数据框

    ddff <- structure(
  list(
    SampleID = structure(
      20:16,
      .Label = c(
        "S39",
        "S30",
        "S35",
        "S22",
        "S23",
        "S26",
        "S29",
        "S24",
        "S27",
        "S32",
        "S37",
        "S36",
        "S38",
        "S34",
        "S33",
        "S40",
        "S25",
        "S28",
        "S31",
        "S21"
      ),
      class = "factor"
    ),
    Counts = c(12177, 14367, 15118, 15312,
               16622),
    sampleName = structure(
      20:16,
      .Label = c(
        "2Dr",
        "2Es",
        "1Er",
        "1Bs",
        "1Cs",
        "2As",
        "2Ds",
        "1Ds",
        "2Bs",
        "1Br",
        "2Br",
        "2Ar",
        "2Cr",
        "1Dr",
        "1Cr",
        "2Er",
        "1Es",
        "2Cs",
        "1Ar",
        "1As"
      ),
      class = "factor"
    ),
    compartment = c("soil", "root", "soil",
                    "soil", "root")
  ),
  row.names = c(NA, 5L),
  class = "data.frame"
)

还有下面的代码

library(tidyverse)    
ddff %>%
      ggplot(aes(x = Counts, y = SampleID)) +
      geom_point(aes(col = compartment), size = 4, alpha = 0.75) +
      geom_text(
        aes(label = paste0("   ", Counts)),
        size = 3,
        hjust = 0,
        nudge_x = -0.1,
        check_overlap = TRUE,
        color = "blue"
      ) +
      xlim(NA, 33000) +
      theme_light() +
      theme(plot.title = element_text(hjust = 0.5), aspect.ratio = 1) +
      theme(# remove the vertical grid lines
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) +
      labs(title = "Library Size Overview",
           x = "Read Counts",
           color = "Compartment") +
      theme(
        legend.position = c(.95, .95),
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.box.background = element_rect(color = "black", size = 1)
      )

我明白了这个情节 Rplot 根据 ddff 列“sampleName”的内容,我有兴趣在右侧 Y 轴上添加第二个标签。当然,解决方案似乎是 scale_y_discretedup_axis 的使用,但我无法弄清楚...根据最近的 ggplot 演变有什么想法?

【问题讨论】:

  • 不是dup_axis,更是如此sec_axis,这略有不同......但我不认为sec_axis本身可以轻松地处理非连续数据,你需要伪造它.有关某种方法,请参阅stackoverflow.com/q/45361904/3358272

标签: r ggplot2 tidyverse


【解决方案1】:

ggplot2 中的离散刻度不支持二级刻度(请参阅相关的issue)。 ggh4x 包有一个manual axis 可以解决这个限制。 (免责声明:我是 ggh4x 的作者)。

例如,您可以像这样使用它:

library(ggplot2)

# ddff <- structure(...) # omitted for brevity

ggplot(ddff, aes(x = Counts, y = SampleID)) +
  geom_point(aes(col = compartment), size = 4, alpha = 0.75) +
  geom_text(
    aes(label = paste0("   ", Counts)),
    size = 3,
    hjust = 0,
    nudge_x = -0.1,
    check_overlap = TRUE,
    color = "blue"
  ) +
  xlim(NA, 33000) +
  guides(y.sec = ggh4x::guide_axis_manual(
    breaks = ddff$SampleID, labels = ddff$sampleName
  ))

但是,如果每个 y 轴类别有多个观察值(示例中不是这种情况),您可能需要对轴中的数据进行重复数据删除。

【讨论】:

    【解决方案2】:

    这是对 ... 的欺骗,调整其解决方案只是添加 scale_y_continuous 并设置 y 轴标签。与 teunbrand 的出色回答不同(我是 teunbrand 的软件包/作品的粉丝),这是基础 ggplot2

    (这也改变了顺序,因为我们现在正在沿样本“计数”。)

    ddff %>%
          ggplot(aes(x = Counts, y = seq_along(SampleID))) +
          geom_point(aes(col = compartment), size = 4, alpha = 0.75) +
          geom_text(
            aes(label = paste0("   ", Counts)),
            size = 3,
            hjust = 0,
            nudge_x = -0.1,
            check_overlap = TRUE,
            color = "blue"
          ) +
          xlim(NA, 33000) +
          theme_light() +
          theme(plot.title = element_text(hjust = 0.5), aspect.ratio = 1) +
          theme(# remove the vertical grid lines
            panel.grid.major.x = element_blank(),
            panel.grid.minor.x = element_blank()) +
          labs(title = "Library Size Overview",
               x = "Read Counts",
               y = "SampleID",                        # <-- new
               color = "Compartment") +
          theme(
            legend.position = c(.95, .95),
            legend.justification = c("right", "top"),
            legend.box.just = "right",
            legend.box.background = element_rect(color = "black", size = 1)
          ) +
          # new
          scale_y_continuous(
            breaks = seq_len(nrow(ddff)), labels = ddff$SampleID,
            sec.axis = sec_axis(~., breaks = seq_len(nrow(ddff)),
                                labels = ddff$sampleName)
          )
    

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 2018-01-03
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2022-01-23
      • 2019-03-25
      • 2016-06-05
      相关资源
      最近更新 更多