I spent a TON of time trying to make this graph w ggplot2, and eventually gave up and made it in excel b/c I had to give the presentation, but would LOVE to do it with ggplot2. Below have an example below of how I made it in excel.
There are 4 categorical variables, and I have the percentage of which they contributed to two cohorts (NPO, ALL).
I created a dataframe and was able to plot them, to some degree, but have not been able to label the columns with their respective percentages, nor change the order of each of the x-variable (the order should be as it is in the excel chart I made).
Greatly appreciate any insight, have been tearing my hair out and bummed I resorted to excel.
Below my attempts
df_all_npo_insurance <- data.frame(insurance=c("Medicaid", "Medicaid",
"Blue Cross", "Blue Cross",
"Managed Care", "Managed Care",
"Other", "Other"),
cohort=c("NPO", "All",
"NPO", "All",
"NPO", "All",
"NPO", "All"),
percentage=c(70.1, 43.6, 17.4, 34.8, 8.5, 14.3, 4.0, 7.3))
ggplot(df_all_npo_insurance, aes(x=insurance, y=percentage, fill=cohort)) +
geom_col(position="dodge", width =0.8, colour="black", size=.8)
EDIT 6/13/20- asking some clarifying questions re your code just for my learning so that I can do this on my own next time!
library(dplyr)
library(ggplot2)
df_all_npo_insurance %>%
mutate(cohort=factor(cohort, levels=c("NPO","All"))) %>%
ggplot(aes(x=reorder(insurance, -percentage), y=percentage, # What does the "reorder" do here, particularly the "-percentage"?
fill=cohort, label = paste0(percentage,"%"))) + # What does the "paste0" do here?
geom_col(position="dodge", width = 0.8, size = 0.8) +
scale_y_continuous(lim=c(0,71)) +
geom_text(position = position_dodge(width = 0.8), vjust = -0.5, size = 2) +
scale_fill_manual(values=c("blue","darkorange"), labels=c("NPO Violators","All Cases")) +
theme_classic() +
theme(axis.line.y=element_blank(), # I am familiar with the "theme" call, but would you mind telling me a little about what you did here with the "axis.text.y=element_blank()" and the other ones here?
axis.text.y=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
legend.position = "bottom",
legend.title = element_blank(),
plot.title=element_text(size=10, hjust=0.5)) +
ggtitle("NPO Violators vs All Cases by Insurance")