Edit: updated with data below
I am attempting to create a graph that looks like this with treatment averages for males and females mirrored horizontally on the y-axis, but I'm struggling to figure out how ggplot:
I can graph the averages with SE horizontally, but they are not starting from the same axis. The y-axis is labeled in both facets. I want females on the left side and males on the right side, starting from the same y-axis. I made the female averages negative, but that still didn't give the desired result; this is the closest I've gotten so far:
I used the following code to create the above graph:
sex_plot_neg<- ggplot(summary_data_neg, aes(x = Average, y = treatment, fill = sex, color = sex)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8), width = 0.7) +
geom_errorbar(aes(xmin = Average - SE, xmax = Average + SE), colour="black",
position = position_dodge(width = 0.8), width = 0.25) +
facet_wrap(~sex, scales = "free_y") + # Separate male and female
theme_minimal() +
labs(x = "Average Number of Insects ± SEM", y = "Treatment") +
theme(legend.position = "top")+
scale_x_continuous(labels = abs)
Data below, with negative female means:
dput(head(summary_data_neg, 20))
structure(list(treatment = c("ace+verbeone", "AKB", "akb+ace+verb",
"AKB+verbenone", "carveol", "cryptone", "lure", "nopinone", "verbenone",
"verbenone plus", "ace+verbeone", "AKB", "akb+ace+verb", "AKB+verbenone",
"carveol", "cryptone", "lure", "nopinone", "verbenone", "verbenone plus"
), sex = c("female", "female", "female", "female", "female",
"female", "female", "female", "female", "female", "male", "male",
"male", "male", "male", "male", "male", "male", "male", "male"
), Average = c(-7.977777778, -29.84210526, -4.288888889, -9.392156863,
-32.1754386, -36.54385965, -41.65686275, -33.0877193, -15.37623762,
-7.355555556, 6.755555556, 27.40350877, 4.022222222, 7.676470588,
28.40350877, 30.29824561, 39.29411765, 27.78947368, 12.31683168,
5.355555556), SE = c(1.020244024, 8.198538498, 0.570859906, 1.132903598,
5.670578931, 6.984321422, 6.640072589, 5.806523463, 1.732785286,
0.903503593, 0.70076117, 6.283350102, 0.500997882, 0.81829709,
4.680109062, 4.743546263, 4.886730757, 4.433067871, 1.302695659,
0.541374565)), row.names = c(NA, 20L), class = "data.frame")
Any suggestions?
You don't provide data, so I can't answer directly.
Simply make one of the sexes have negative values, and then use the abs
function to label the x-axis:
library(ggplot2)
data.frame(Sex = c("M", "M", "F", "F"),
Treatment = c("A", "B", "A", "B"),
Average = c(1, 3, -2, -4),
SE = 0.5) |>
ggplot(aes(x = Average, y = Treatment, fill = Sex)) +
geom_col() +
geom_errorbar(aes(xmin = Average - SE, xmax = Average + SE), width = 0.2) +
scale_x_continuous(labels = abs) +
theme_minimal()