控制单个中断/标签 Facet Grid / ggplot2

Posted

技术标签:

【中文标题】控制单个中断/标签 Facet Grid / ggplot2【英文标题】:Control Individual Breaks/Labels Facet Grid / ggplot2 【发布时间】:2021-12-23 21:58:18 【问题描述】:

我有下面的 ggplot2 代码,它产生了附加的情节。我想知道是否可以控制单个 y 轴中断/标签。例如,我希望将它们的 Mean Depth 设为“1X”、“2X”、“3X”和“4X”,以及 Retained Reads 的百分比喜欢有“10%”、“20%”、“30%”和“40%”。我想我需要为每个单独的方面使用类似 scale_y_continuous 的东西,但我还没有找到这样做的方法。

非常感谢,乔治。

 ggplot() +
  geom_violin(data = fulldfUp, aes(x = Species, y = Value),
              fill = "#ffffff", colour = "#000000", show.legend = FALSE, alpha = .9, size = .3, width = .7) +
  stat_summary(data = fulldfUp, aes(x = Species, y = Value),  
               fun = mean, geom = "point", shape = 21, size = 3.5, alpha = .9, colour = "#000000", fill = "#fc3232") +
  facet_grid(Estimate ~. , scales = "free", labeller = labeller(Estimate = ylabels)) +
  scale_fill_manual(values = c("#fbb4ae", "#b3cde3", "#ccebc5", "#decbe4", "#fed9a6")) +
  scale_colour_manual(values = c("#fbb4ae", "#b3cde3", "#ccebc5", "#decbe4", "#fed9a6")) +
  theme(panel.background = element_rect(fill = "#ffffff"),
        panel.grid.major.x = element_line(color = "#ededed", linetype = "dashed", size = .00005),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank(), 
        panel.border = element_blank(),
        axis.line = element_line(colour = "#000000", size = .3),
        axis.title = element_blank(),
        axis.text.x = element_text(colour = "#000000", size = 20, face = "bold", angle = 45, vjust = 1, hjust = 1),
        axis.text.y = element_text(color = "#000000", size = 20),
        axis.ticks.x = element_line(color = "#000000", size = .3),
        axis.ticks.y = element_line(color = "#000000", size = .3),
        strip.background.y = element_rect(colour = "#000000", fill = "#d6d6d6", size = 0.3),
        strip.text = element_text(colour = "#000000", size = 20, face = "bold"),
        legend.position = "top",
        legend.margin = margin(t = 0, b = 0, r = 0, l = 0),
        legend.box.margin = margin(t = 10, b = 20, r = 0, l = 0),
        legend.key = element_rect(fill = NA),
        legend.background = element_blank())

【问题讨论】:

【参考方案1】:

我需要创建自己的示例数据,因为它没有提供。

您可以对每组的数据进行标准化以绘制百分比:

library(tidyverse)
set.seed(1337)

# simulate some aboundance data
data <-
  list(
    Species_a = rpois(100, 0.2),
    Species_b = rpois(100, 0.1),
    Species_c = rpois(100, 0.3),
    Species_d = rpois(100, 0.8),
    Species_e = rpois(100, 0.7)
  ) %>%
  enframe() %>%
  unnest(value) %>%
  mutate(group = ifelse(name %in% c("Species_d", "Species_e"), "B", "A"))
data
#> # A tibble: 500 x 3
#>    name      value group
#>    <chr>     <int> <chr>
#>  1 Species_a     0 A    
#>  2 Species_a     0 A    
#>  3 Species_a     0 A    
#>  4 Species_a     0 A    
#>  5 Species_a     0 A    
#>  6 Species_a     0 A    
#>  7 Species_a     1 A    
#>  8 Species_a     0 A    
#>  9 Species_a     0 A    
#> 10 Species_a     0 A    
#> # … with 490 more rows


data %>%
  group_by(group) %>%
  mutate(value = value / sum(value) * 100) %>%
  ggplot(aes(name, value)) +
  geom_violin() +
  facet_wrap(~group, scales = "free_x") +
  scale_y_continuous(labels = scales::percent_format(scale = 1)) +
  labs(y = "value (Percentage)")

由reprex package (v2.0.1) 于 2021 年 11 月 11 日创建

要计算平均值,您需要创建另一个图,因为平均值是与百分比不同的轴,并且facet_wrap 仅适用于相同比例类型的轴:

data %>%
  group_by(group) %>%
  summarise(value = mean(value)) %>%
  ggplot(aes(group, value)) +
  geom_col()

【讨论】:

非常感谢您的回答,但这并不是我真正想要的。我希望能够控制单个中断/标签,而无需更改情节结构......更重要的是,无需创建额外的情节。

以上是关于控制单个中断/标签 Facet Grid / ggplot2的主要内容,如果未能解决你的问题,请参考以下文章

使用 facet_grid 在 ggplot 中自动设置数据代表中断

摆脱那些灰色盒子上的 facet_grid 标签?

在 ggplot2 facet_grid 中旋转切换的构面标签

如何在 R 中自定义 Facet_Grid 字体标签

facet_grid label_both 将标签文本动态构面变量包装在闪亮图中

R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?