如何在R中基于ggplot2中的离散y轴标签更改面板的背景颜色?

jq6vz3qz  于 2024-01-03  发布在  其他
关注(0)|答案(2)|浏览(119)

在R中有一个名为plot_df的嵌套框架,其结构如下:

# Import the library
library(ggplot2)

# Create a sample data
set.seed(123)
plot_df <- data.frame(gene = rep(paste0("ENSG", 1:10, ".17"), each = 2),
                      mean = rnorm(20, 0.5, 0.1),
                      sd = rnorm(20, 0.02, 0.01),
                      group = rep(c("group_a", "group_b"), 10))

字符串
我尝试创建一个带有点和误差条的ggplot,其中y轴表示基因名称(作为离散标签),x轴表示平均值,颜色表示组(“group_a”或“group_b”)。下面是我使用的代码:

ggplot(plot_df, aes(y=gene)) + 
    geom_point(aes(x=mean, color = group), position = position_dodge(width=0.9)) + 
    geom_errorbar(aes(xmin=mean-sd, xmax=mean+sd, color = group), width=.2,
                 position=position_dodge(.9)) + 
    geom_vline(aes(xintercept=0.5), linetype=2, color = 'gray') + 
    theme_bw() + 
    theme(panel.background = element_blank(),
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank())


x1c 0d1x的数据
然而,我想根据y轴上的基因名称改变面板的背景颜色(每个基因有不同的颜色或相邻基因没有相同的颜色)。由于我的y轴标签是离散的,我不知道如何实现这一点。任何帮助都将不胜感激。谢谢!
我试过:
Change background colour between day and night in ggplot2 in R

drkbr07n

drkbr07n1#

下面是一个使用geom_tile()的解决方案,
1.我们为背景颜色创建一个数据框
1.当我们使用geom_tile()时,我们将设置x美学,以覆盖图的整个宽度,并相应地调整每个图块的宽度。

  1. geom_tile()使用x = sum(x_limits) / 2将切片定位在图的中心,使用width = diff(x_limits)确保每个切片跨越图的整个宽度
library(ggplot2)
library(dplyr)

set.seed(123)
plot_df <- data.frame(
  gene = rep(paste0("ENSG", 1:10, ".17"), each = 2),
  mean = rnorm(20, 0.5, 0.1),
  sd = rnorm(20, 0.02, 0.01),
  group = rep(c("group_a", "group_b"), 10)
)

# df for background colors
bg_df <- plot_df %>%
  distinct(gene) %>%
  arrange(gene) %>% 
  mutate(color = ifelse(row_number() %% 2 == 1, "gold", "lightgrey"))

# x axis limts based on data 
x_limits <- range(plot_df$mean - plot_df$sd, plot_df$mean + plot_df$sd, na.rm = TRUE)

# the plot
ggplot(plot_df, aes(y = gene, x = mean)) +
  geom_tile(data = bg_df, aes(y = gene, x = sum(x_limits) / 2, width = diff(x_limits), fill = color), 
            alpha = 0.3, inherit.aes = FALSE) +
  geom_point(aes(color = group), position = position_dodge(width = 0.9)) +
  geom_errorbar(aes(xmin = mean - sd, xmax = mean + sd, color = group), 
                width = 0.2, position = position_dodge(.9)) +
  scale_color_manual(values = c("steelblue3", "red3"))+
  geom_vline(xintercept = 0.5, linetype = 2, color = 'gray80') +
  theme_bw() +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.position = "right") +
  scale_fill_identity()

字符串


的数据

oug3syen

oug3syen2#

另一种选择是使用geom_rect,将离散的gene s转换为数字,并为ymin/ymax添加-/+.5。此外,将xmin/max设置为-/+Inf可以填充整个面板,其中我还将y尺度的扩展减少到.5
请注意,我们必须显式调用scale_y_discrete()来强制离散缩放,因为我添加了geom_rect作为第一层(否则会出现错误,因为ggplot2将默认为连续缩放)。此外,我为rect使用单独的矩形,以避免为每个group添加矩形,而不是每个gene只添加一个矩形。

library(ggplot2)
library(dplyr)

set.seed(123)
plot_df <- data.frame(
  gene = rep(paste0("ENSG", 1:10, ".17"), each = 2),
  mean = rnorm(20, 0.5, 0.1),
  sd = rnorm(20, 0.02, 0.01),
  group = rep(c("group_a", "group_b"), 10)
)

rect_df <- plot_df |>
  distinct(gene) |> 
  mutate(
    ymin = as.numeric(factor(gene)) - .5,
    ymax = as.numeric(factor(gene)) + .5,
    fill = factor(as.numeric(factor(gene)) %% 2)
  )

# the plot
ggplot(plot_df, aes(y = gene)) +
  geom_rect(
    data = rect_df,
    aes(ymin = ymin, ymax = ymax, fill = fill, xmin = -Inf, xmax = Inf),
    alpha = .3
  ) +
  geom_point(aes(x = mean, color = group),
    position = position_dodge(width = 0.9)
  ) +
  geom_errorbar(aes(xmin = mean - sd, xmax = mean + sd, color = group),
    width = .2,
    position = position_dodge(.9)
  ) +
  geom_vline(aes(xintercept = 0.5), linetype = 2, color = "gray") +
  scale_y_discrete(expand = c(0, .5)) +
  scale_fill_manual(values = c("grey90", "grey60"), guide = "none") +
  theme_bw() +
  theme(
    panel.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )

字符串


的数据

相关问题