来自单独数据帧

时间:2017-12-11 16:12:00

标签: r ggplot2

我有这两个数据帧:

#This is the df i want to plot. I replaced the values with dummy data.
MonthClosed <- c("Nov 2016", "Dec 2017","Jan 2017","Feb 2017","Mar 2017","Apr 2017")
SumRedress <- data.frame(MonthClosed)
SumRedress$MonthClosed <- factor(SumRedress$MonthClosed, , levels = c("Nov 2016", "Dec 2017","Jan 2017","Feb 2017","Mar 2017","Apr 2017"))
SumRedress$ProductBC <- c("BC1","BC2","BC3","BC1","BC2","BC2")
SumRedress$AreaClosed <- c("Area1","Area2","Area1","Area1","Area1","Area2")
SumRedress$Redress.Type <- c("refund","refund","interest","apology","apology","compensation")
SumRedress$Allocation.Category <- c(1,2,1,2,1,2)
SumRedress$RedressPaid <- c(0.0, 300.0, 12.99, 10.0, 120.0, 13.98)



#second df from which i want the data labels
MonthCreated <- c("Nov 2016", "Dec 2017","Jan 2017","Feb 2017","Mar 2017","Apr 2017")
PCTClosed <- data.frame(MonthCreated)
PCTClosed$MonthCreated <- factor(PCTClosed$MonthCreated, levels = c("Nov 2016", "Dec 2017","Jan 2017","Feb 2017","Mar 2017","Apr 2017"))
PCTClosed$PCTClosed <- c("34.6","64.3","23.5","76.3","12.4","65.4")

第二个df的MonthCreated列仅包含第一个df中存在的月数。

我使用下面的代码制作了图表。

我想将来自PCTClosed $ PCTClosed的值放在x轴值与PCTClosed $ MonthCreated值匹配的每个柱上(最好是每个面)。

我使用geom_text函数搞砸了但似乎无法解决这个问题。

Plot1 <- ggplot(SumRedress, aes(x=MonthClosed, y=RedressPaid, group = AreaClosed, fill = AreaClosed))+
  facet_grid(ProductBC ~.)+
  geom_bar(stat="summary", fun.y="sum")+
  #geom_text()+
  scale_y_continuous(breaks = seq(0,200000, by = 50000))+
  labs(title="Monthly Redress", x="Month Closed", y="redress") +
  theme_classic() +
  #scale_fill_manual(values = colpal3) +
  theme(axis.text.x=element_text(angle=35,hjust=1,vjust=0.5), axis.title.x = element_blank(), plot.margin = unit(c(0.2,0.2,1,0.1), "cm"))
Plot1

I'm after something like this. Essientially wherever there's a bar I want a label next to it with the PCTClosed value of the same x value

0 个答案:

没有答案