I am attempting to flip a plot in order to have the categorical variables on the y-axis, but my error bars are not flipping. Hopefully the image below does a better job than I am of explaining the issue:
I have used this method before to successfully flip ggplots with error bars, so I am not sure why it is not working this time around. Here is the code used to generate this plot:
avg_emit_consume %>%
group_by(country, type) %>%
pivot_wider(names_from = type, values_from = value) %>%
mutate(ratio = emissions / consumption) %>%
drop_na(ratio) %>%
ungroup() %>%
lm(ratio ~ food_category, data = .) %>%
tidy(conf.int = TRUE) %>%
mutate(term = ifelse(term == "(Intercept)", "Beef", term),
term = str_remove(term, "food_category"),
estimate = ifelse(term != "Beef",
estimate + estimate[term == "Beef"], estimate),
conf.low = ifelse(term != "Beef",
conf.low + estimate[term == "Beef"], conf.low),
conf.high = ifelse(term != "Beef",
conf.high + estimate[term == "Beef"], conf.high),
term = term %>%
as_factor() %>%
fct_reorder(estimate)) %>%
ggplot(aes(term, estimate)) +
geom_point() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high)) +
coord_flip(expand = TRUE) +
labs(title = "Emissions to Consumption Ratio for \nEach Food Category",
subtitle = "Kilograms of CO2 per Kilogram Consumed") +
ylab("Kilograms of CO2 Emissions of per Kilogram Consumed") +
xlab("Food Category") +
theme_classic()