Search code examples
rggplot2histogramdensity-plot

Overlay Density Curve on stacked geom_col plot? [code & images included]


# Creating overlayed barchart with ggplot 
dist_plot_prom_delv_Mon <-  ggplot(union_df_prom_delv_Mon,aes(x=Promise,y=Orders,fill=Flag)) + 
        geom_bar(stat="identity",position = "identity", alpha= .50, show.legend = FALSE)+
  theme_economist()+
  scale_x_continuous(breaks=seq(0,30,1))+
  xlab("Promise & Delv Days") +
  ylab("Count of Orders") +
 scale_fill_manual(values = c("sandybrown", "cadetblue4"))+
  ggtitle("E2E Promise & Delv Distribution Comparison 2020 YTD - Monday Orders")+
   theme(plot.title = element_text(size=12), axis.text=element_text(size=11), 
        axis.title=element_text(size=11), strip.text = element_text(size=11),
        axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.title.x = element_text(margin = margin(t = 15, r = 15, b = 0, l = 0)))

I currently have a "histogram" that I created by doing an overlayed bar chart using geom_col and using fill to make it seem like the data is overlayed. See the current image below enter image description here

I want to overlay an additional layer which would be the density line with a low alpha similar to this image. enter image description here

I had to use geom_col to mimic geom_histogram becuase geom_histogram was calculating my measure incorrectly. Here is a sample of my data and what gets overlayed. enter image description here

dist_plot_prom_delv <-  ggplot(union_df_prom_delv,aes(x=Promise,y=Orders,fill=Flag)) + 
        geom_bar(stat="identity",position = "identity", alpha= .50, show.legend = FALSE)+
  theme_economist()+
  scale_x_continuous(breaks=seq(0,30,1))+
         geom_density(fill="darkgrey", alpha=0.3)+
  xlab("Promise & Delv Days") +
  ylab("Count of Orders") +
 scale_fill_manual(values = c("sandybrown", "cadetblue4"))+
  ggtitle("E2E Promise & Delv Distribution Comparison 2020 YTD ")+
   theme(plot.title = element_text(size=12), axis.text=element_text(size=11), 
        axis.title=element_text(size=11), strip.text = element_text(size=11),
        axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.title.x = element_text(margin = margin(t = 15, r = 15, b = 0, l = 0)))




plot_prom2_Mon <- step1_fix %>% 
  rename(Promise = E2E_PROMISE_DAYS) %>% 
  group_by(Promise) %>% 
  filter(ORD_DOW == 'Monday') %>% 
  summarise(Orders = n_distinct(ORD_TRACK_KEY)) %>% 
  mutate(Flag = "Promise")

# Creating Logic to create union for overlayed bar chart 
plot_delv2_Mon <- step1_fix %>% 
  rename(Promise = E2E_BUS_DAYS) %>% 
  group_by(Promise) %>% 
  filter(ORD_DOW == 'Monday') %>% 
  summarise(Orders = n_distinct(ORD_TRACK_KEY)) %>% 
  mutate(Flag = "Delv")

# Creating union to create overlayed barchart
union_df_prom_delv_Mon <- union_all(plot_prom2_Mon, plot_delv2_Mon)

Solution

  • The command for adding a transparent area:

    geom_area(aes(color=Fill), alpha=0.3, position="identity") +
    theme(legend.position = "none") #for deleting the legend