堆积的geom_col图上的叠加密度曲线? [包括代码和图像]

问题描述

# 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,l = 0)))

我目前有一个“直方图”,它是通过使用geom_col进行覆盖的条形图并使用填充使其看起来像数据被覆盖而创建的。 查看下面的当前图片

enter image description here

我想覆盖一个额外的层,该层将是类似于此图像的具有低alpha值的密度线。

enter image description here

我必须使用geom_col来模拟geom_histogram,因为geom_histogram错误地计算了我的度量。这是我的数据示例以及覆盖的内容

enter image description here

dist_plot_prom_delv <-  ggplot(union_df_prom_delv,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),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)

解决方法

添加透明区域的命令:

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