我刚刚开始使用R作为我的图表,而我想要实现的图表是一个堆栈条形图,其中折线图应该有一个辅助轴 . 我已经能够为该线添加辅助轴 . 然而,折线图似乎不遵循辅助轴,而是使用第一个 .

如果有人能指出我错过的东西,我真的很感激 .

这就是我的图表的样子 .

我的数据看起来像这样:

date       Combine_Shift    Shift_Type    Prod
2018-04-02    536            PM_Shift     1088
2018-04-02    563            AM_Shift     343
2018-04-03    665            PM_Shift     1124
2018-04-03    665            AM_Shift     444
2018-04-04    658            PM_Shift     1004
2018-04-04    658            AM_Shift     493
2018-04-05    529            PM_Shift     855
2018-04-05    529            AM_Shift     1088
2018-04-06    630            PM_Shift     959
2018-04-06    630            AM_Shift     1088
2018-04-07    926            PM_Shift     1475
2018-04-07    926            AM_Shift     716
2018-04-09    701            PM_Shift     825
2018-04-09    701            AM_Shift     595
2018-04-10    605            PM_Shift     648
2018-04-10    605            AM_Shift     581

这就是我的代码在图表中的样子 .

## Convert my table to xts
ProdDB <- xts((ProdData)[,-1,0], order.by=as.Date((ProdData)[,1], "%Y-%m-%d"))

plot_Productivity <- function(date){
  answ2<-ProdDB[date]
  answ1<-fortify(answ2)

ggplot(data=answ1) + 
  theme(legend.direction = "horizontal",legend.position = "top",axis.text.x = element_text(vjust = .5),plot.title = element_text(hjust = 0.5, size=11,face="bold"),axis.text = element_text(size=8)) + 
  labs(title = "Productivity", x = " ", y = " ", fill = " ")  +
  scale_fill_manual(values = ProductivityPalette) + 
  guides(fill=guide_legend(nrow=1,byrow=TRUE)) + theme(legend.text=element_text(size=7)) +
  geom_bar(aes(y = as.numeric(as.character(Prod)) , x =Index , fill = Shift_Type), stat="identity") + 
  geom_line(aes(y = as.numeric(as.character(Combine_Shift)) , x =Index))+
  scale_y_continuous(sec.axis= sec_axis(~./2))
}