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

问题描述 投票:0回答:1

我在 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”或“组_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())

但是,我想根据 y 轴上的基因名称更改面板的背景颜色(每个基因具有不同的颜色或相邻基因没有相同的颜色)。由于我的 y 轴标签是离散的,我不确定如何实现这一点。任何帮助,将不胜感激。谢谢!

我已经尝试过:

在 R 中的 ggplot2 中更改白天和夜晚的背景颜色

r ggplot2 plot background background-color
1个回答
0
投票

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()

© www.soinside.com 2019 - 2024. All rights reserved.