I am trying to make a barplot with positive and negative values using two tables. The tables look like this:
> dput(up.counts)
structure(list(deg.phase = c("Early", "Early", "Early", "Early",
"Early", "Late", "Late", "Late", "Late", "Late", "Both"), contrast.names = structure(1:11, .Label = c("1 dpi",
"7 dpi", "Post-Oviposition", "Within 14 dpi", "Early", "22 dpi",
"Longer", "Adapted R", "Adapted S", "Late", "Nymph"), class = "factor"),
degs = c(92L, 95L, 4210L, 2262L, 511L, 1798L, 124L, 163L,
120L, 553L, 2230L), reg = c("UP", "UP", "UP", "UP", "UP",
"UP", "UP", "UP", "UP", "UP", "UP")), row.names = c(NA, -11L
), class = "data.frame")
> dput(dn.counts)
structure(list(deg.phase = c("Early", "Early", "Early", "Early",
"Early", "Late", "Late", "Late", "Late", "Late", "Both"), contrast.names = structure(1:11, .Label = c("1 dpi",
"7 dpi", "Post-Oviposition", "Within 14 dpi", "Early", "22 dpi",
"Longer", "Adapted R", "Adapted S", "Late", "Nymph"), class = "factor"),
degs = c(-101, -95, -4363, -2262, -503, -2758, -125, -233,
-183, -608, -2294), reg = c("DN", "DN", "DN", "DN", "DN",
"DN", "DN", "DN", "DN", "DN", "DN")), row.names = c(NA, -11L
), class = "data.frame")
>up.counts
deg.phase contrast.names degs reg
1 Early 1 dpi 92 UP
2 Early 7 dpi 95 UP
3 Early Post-Oviposition 4210 UP
4 Early Within 14 dpi 2262 UP
5 Early Early 511 UP
6 Late 22 dpi 1798 UP
7 Late Longer 124 UP
8 Late Adapted R 163 UP
9 Late Adapted S 120 UP
10 Late Late 553 UP
11 Both Nymph 2230 UP
>dn.counts
deg.phase contrast.names degs reg
1 Early 1 dpi 101 DN
2 Early 7 dpi 95 DN
3 Early Post-Oviposition 4363 DN
4 Early Within 14 dpi 2262 DN
5 Early Early 503 DN
6 Late 22 dpi 2758 DN
7 Late Longer 125 DN
8 Late Adapted R 233 DN
9 Late Adapted S 183 DN
10 Late Late 608 DN
11 Both Nymph 2294 DN
I use the following code to make the barplots, however, I end up losing the oviposition bar (particularly the positive value one) each time I make it.
dn.counts$degs = dn.counts$degs*-1
up.counts$contrast.names = as.character(up.counts$contrast.names)
up.counts$contrast.names = factor(up.counts$contrast.names, levels = unique(up.counts$contrast.names))
dn.counts$contrast.names = as.character(dn.counts$contrast.names)
dn.counts$contrast.names = factor(dn.counts$contrast.names, levels = unique(dn.counts$contrast.names))
breaksup = levels(up.counts$contrast.names)
breaksdn = levels(dn.counts$contrast.names)
Plot Code:
cdeg1 = ggplot() +
geom_bar(data = up.counts, aes(x = contrast.names, y = degs, fill = deg.phase), stat = "identity", width = 1) +
new_scale_fill() +
geom_bar(data = dn.counts, aes(x = contrast.names, y = degs, fill = deg.phase), stat ="identity", width = 1) +
theme(legend.box = "horizontal", legend.margin = margin(0,0,0,-0.6, "cm"), legend.box.margin = margin(0,1,0,1, "cm"), legend.title = element_text(size = 14), legend.text = element_text(size = 12)) + scale_y_continuous(limits = c(-5000,4000), breaks = ybreaks, labels = abs(ybreaks)) +
cdeg1
Not sure how to resolve the issue. I would also like to bunch the bars by the deg.phase
column and make positive values and negative values particular colors. Any help would be appreciated. Thanks.