0

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.

Excel example of what I am trying to accomplish with ggplot!

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")
rhinomania
  • 15
  • 4

1 Answers1

1
library(dplyr)
library(ggplot2)

df_all_npo_insurance %>%
  mutate(cohort=factor(cohort, levels=c("NPO","All"))) %>%
  ggplot(aes(x=reorder(insurance, -percentage), y=percentage, 
             fill=cohort, label = paste0(percentage,"%"))) +
  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(),
        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")

Explanations:

reorder(): See this heavily viewed post. The "-" sign tells R to sort descending since the default is ascending.

label=: This tells ggplot that we will be adding text to the plot, usually using geom_text().

paste0(): Very simply function to concatenate vectors. Here we just need to add the % to each value of the percentage vector.

theme(): Most ggplot components (axes, labels, legend titles) are provided by default. If you want to suppress them, or change the defaults, you'll need to say so. element_blank() is ggplot syntax for "", that is, show nothing.


enter image description here

Edward
  • 10,360
  • 2
  • 11
  • 26
  • That is fricking amazing. Thank you!!! I edited my initial post just with some clarifying questions re some lines of your code, so that next time I can do it myself with greater understanding. Thank you! :) – rhinomania Jun 13 '20 at 14:52
  • Supremely helpful. Thank you! – rhinomania Jun 14 '20 at 23:29