如何使用ggplotly和subplot在子图中合并图例

时间:2019-09-18 02:52:11

标签: r ggplot2 shiny plotly subplot

我尝试使用ggplotly的subplot函数将两个条形图绘制成一个图集。但是,无论我如何尝试修改布局,即使它们相同,也无法合并图例并仅显示一个图例。

我尝试更改每种布局中的图例设置,但没有运气



output$barPlot1 = renderPlotly({
      subplot(
        ggplotly(
           ggplot(data =  byPcp_monthly(), 
               aes(x = Year.Month, y = Admi.Rate, 
                   group = !!rlang::sym(input$inputG))) +
          geom_line(aes(color= !!rlang::sym(input$inputG) )) +
          geom_point(aes(color=!!rlang::sym(input$inputG))) + 
          ggtitle('Admissions per 1000 per month tracking') +
          theme_classic() +
          theme(plot.title = element_text(face = 'bold', hjust = 0.5), 
                axis.text.x = element_text(angle = 90, face = 'bold'),
                axis.text.y = element_text(face = 'bold')) + 
          xlab('Year - Month') +
          ylab('Admissions per 1000'), 
        legendgroup = ('group1'), showlegned = F
        ), 

         ggplotly(
        ggplot(data =  byPcp_monthly(), 
               aes(x = Year.Month, y = Total.IP, 
                   group = !!rlang::sym(input$inputG))) +
          geom_line(aes(color= !!rlang::sym(input$inputG) )) +
          geom_point(aes(color=!!rlang::sym(input$inputG))) + 
          ggtitle('Admissions per month tracking') +
          theme_classic() +
          theme(plot.title = element_text(face = 'bold', hjust = 0.5), 
                axis.text.x = element_text(angle = 90, face = 'bold'),
                axis.text.y = element_text(face = 'bold')) + 
          xlab('Year - Month') +
          ylab('Total Admissions'), 
        legendgroup = ('group1'), showlegend = F
        ),

        nrows = 2, shareX = T, shareY = T, titleY = T, margin = 0.08) %>% 
        layout(title = 'Admission Tracking',
               xaxis = list(showticklabel = F, showaxis = T),
               legend2 = list(showlegend = F))


    })

0 个答案:

没有答案