我想使用ggplot2将背景分为两种颜色作为水平条形图

时间:2018-06-15 13:15:39

标签: r ggplot2

我的水平条形图上有4组3个条形图。我想前两组(即前六条)将颜色小麦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)

Please take a look at my graph so far

enter image description here

1 个答案:

答案 0 :(得分:0)

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

P.S:你的as.factor(condition)&amp;您不需要as.factor(region),因为您没有将它们分配回condition&amp; 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 package(v0.2.0)创建于2018-06-15。