There are 4 groups of 3 bars on my horizontal bar chart. I would like the top two groups (ie top 6 bars) to have the colour wheat 1 as their background and the bottom two groups (ie the bottom 6 bars) to have the background colour wheat 2. Please look at the link below to see what I have so far.
I have four regions of space Far left space (LS), central left space (LS-C), central right space (RS-C) and far right space (RS). I have three conditions A, B, C. Combined this means I have 12 conditions
My data resembles this 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)
here is the code I have so far
# 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)
You can use geom_rect
to color desired regions
PS: your as.factor(condition)
& as.factor(region)
were not needed as you didn't assign them back to 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)
Alternatively, annotate("rect", ...)
can also get the job done
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)
Created on 2018-06-15 by the reprex package (v0.2.0).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.