首页 > 解决方案 > 我想使用 ggplot2 将我的背景分成两种颜色的水平条形图

问题描述

我的水平条形图上有 4 组 3 个条形图。我希望前两组(即前 6 条)的背景颜色为小麦 1,底部两组(即底部的 6 条)的背景颜色为小麦 2。请看下面的链接到目前为止我所拥有的。

我有四个空间区域:最左空间(LS)、中左空间(LS-C)、中右空间(RS-C)和最右空间(RS)。我有 A、B、C 三个条件。这意味着我有 12 个条件

我的数据类似于fakedata

condition <- c( "LS-A", "LS-B","LS-C", "LS-C-A", "LS-C-B", "LS-C-C", 
            "RS-C-A", "RS-C-B", "RS-C-C", "RS-A", "RS-B", "RS-C")
as.factor(condition )

condition <- factor(condition, 
                    levels = c("LS-A","LS-B","LS-C","LS-C-A","LS-C-B","LS-C-C", "RS-C-A","RS-C-B","RS-C-C", "RS-A","RS-B", "RS-C"))
#put them in the order I Want to appear on the graph

region <- c("Far-right", "Far-right", "Far-right", "RC", "RC", "RC", "LC", "LC", "LC", "Far-left", "Far-left", "Far-left")
as.factor(region)
region <-factor(region, 
                    levels = c("Far-right", "RC", "LC", "Far-left"))

mean <- c(-2, -1, 2, -3, 4, 2, -4, 2, 4, 2, 4, 1)

fakedata <- data.frame(condition, region, mean)

这是我到目前为止的代码

# 1: horizontal barplot
p <- ggplot(fakedata,  aes(x=region, y = mean, fill= condition )) +
geom_bar(width=.6, position = position_dodge(width=0.6), stat = "identity", color = "black", size = 0.3) +  
 coord_flip(ylim = c(-5, 5)) 

p <- p + scale_fill_manual(values=c("yellow", "green1", "royalblue", "yellow", "green", "blue", "yellow", "green", "blue","yellow", "green", "blue"))
p <- p + theme(legend.position = "none") 

p <- p + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                       panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), legend.position = "none")

p <- p + theme (panel.background = element_rect(linetype= "solid", color = "black", fill=NA, size = 0.5))
p <- p + geom_abline(mapping = NULL, intercept = 0, slope= 0, color = "black", size= 0.25, show.legend = NA)

请看我到目前为止的图表

在此处输入图像描述

标签: rggplot2

解决方案


您可以使用geom_rect为所需区域着色

PS:不需要您的as.factor(condition)&as.factor(region)因为您没有将它们分配回condition&region

library(ggplot2)

condition <- c(
  "LS-A", "LS-B", "LS-C", "LS-C-A", "LS-C-B", "LS-C-C",
  "RS-C-A", "RS-C-B", "RS-C-C", "RS-A", "RS-B", "RS-C"
)
condition <- factor(condition,
  levels = c(
    "LS-A", "LS-B", "LS-C", "LS-C-A", "LS-C-B", "LS-C-C",
    "RS-C-A", "RS-C-B", "RS-C-C", "RS-A", "RS-B", "RS-C"
  )
)
# put them in the order I want to appear on the graph

region <- c(
  "Far-right", "Far-right", "Far-right", "RC", "RC", "RC", "LC", "LC",
  "LC", "Far-left", "Far-left", "Far-left"
)
region <- factor(region, levels = c("Far-right", "RC", "LC", "Far-left"))

mean <- c(-2, -1, 2, -3, 4, 2, -4, 2, 4, 2, 4, 1)

fakedata <- data.frame(condition, region, mean)

ggplot(fakedata, aes(x = region, y = mean, fill = condition)) +
  # plot geom_rect first so it will stay in the background
  # lower region: Far-right and RC are factor level 1 & 2
  geom_rect(
        fill = "wheat3",
        xmin = 0,
        xmax = 2.5,
        ymin = -5, 
        ymax = 5) +   
  # upper region: Far-left and LC are factor level 3 & 4
  geom_rect(
          fill = "wheat1",
          xmin = 2.5,
          xmax = 5.0,
          ymin = -5,
          ymax = 5) +
  geom_col(width = .6, position = position_dodge(width = 0.6), 
           color = "black", size = 0.3) +
  coord_flip(ylim = c(-5, 5)) +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_manual(values = c("yellow", "green1", "royalblue", 
                              "yellow", "green", "blue", 
                              "yellow", "green", "blue", 
                              "yellow", "green", "blue")) +
  theme_bw() + 
  # any modified theme needs to be after theme_bw()
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"), 
        legend.position = "none") +
  theme(panel.background = element_rect(linetype = "solid", 
                                       color = "black", 
                                       fill = NA, 
                                       size = 0.5)) +
  geom_abline(mapping = NULL, intercept = 0, slope = 0, 
             color = "black", size = 0.25, show.legend = NA)

或者,annotate("rect", ...)也可以完成工作

ggplot(fakedata, aes(x = region, y = mean)) +
  geom_col(aes(fill = condition), 
           width = .6, 
           position = position_dodge(width = 0.6), 
           size = 0.3) +
  annotate("rect",
           alpha = 0.4,
           fill = "wheat3",
           xmin = 0, xmax = 2.5,
           ymin = -5, ymax = 5) +
  annotate("rect",
           alpha = 0.4,
           fill = "wheat1",
           xmin = 2.5, xmax = 5.0,
           ymin = -5, ymax = 5) +
  coord_flip(ylim = c(-5, 5)) +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_manual(values = c("yellow", "green1", "royalblue", 
                               "yellow", "green", "blue", 
                               "yellow", "green", "blue", 
                               "yellow", "green", "blue")) +
  theme_bw() +
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"), 
        legend.position = "none") +
  theme(panel.background = element_rect(linetype = "solid", 
                                        color = "black", 
                                        fill = NA, 
                                        size = 0.5)) +
  geom_abline(mapping = NULL, intercept = 0, slope = 0, 
              color = "black", size = 0.25, show.legend = NA)   

reprex 包(v0.2.0) 于 2018 年 6 月 15 日创建。


推荐阅读