Plotly subplots with shared legend in R

故事扮演 提交于 2021-01-28 09:02:03

问题


I have made twoplots using plotly, which are working fine individually, but when combined using subplot I can't seem to figure out how to combine the legends. I have tried to use showlegend = F in plot_ly in one of the plots, but this just removes it completely - what I want is to control both subplots with the same legend.

My code is as follows:

coronavirus_not_china <- coronavirus %>%
  filter(!(country == "China"))

cases_not_china_plot <- coronavirus_not_china %>% 
  group_by(type, date) %>%
  summarise(total_cases = sum(cases)) %>%
  pivot_wider(names_from = type, values_from = total_cases) %>%
  arrange(date) %>%
  mutate(active = confirmed - death - recovered) %>%
  mutate(active_total = cumsum(active),
                recovered_total = cumsum(recovered),
                death_total = cumsum(death)) %>%
  plot_ly(x = ~ date,
                  y = ~ active_total,
                  name = 'Active', 
                  fillcolor = '#1f77b4',
                  type = 'scatter',
                  mode = 'none', 
                  stackgroup = 'one',
                  showlegend = F) %>%
  add_trace(y = ~ death_total, 
             name = "Death",
             fillcolor = '#E41317') %>%
  add_trace(y = ~recovered_total, 
            name = 'Recovered', 
            fillcolor = 'forestgreen') %>%
  layout(title = "Distribution of Covid19 Cases outside China",
         legend = list(x = 0.1, y = 0.9),
         yaxis = list(title = "Number of Cases", showgrid = T))
coronavirus_china <- coronavirus %>%
  filter((country == "China"))

cases_china_plot <- coronavirus_china %>% 
  group_by(type, date) %>%
  summarise(total_cases = sum(cases)) %>%
  pivot_wider(names_from = type, values_from = total_cases) %>%
  arrange(date) %>%
  mutate(active = confirmed - death - recovered) %>%
  mutate(active_total = cumsum(active),
                recovered_total = cumsum(recovered),
                death_total = cumsum(death)) %>%
  plot_ly(x = ~ date,
                  y = ~ active_total,
                  name = 'Active', 
                  fillcolor = '#1f77b4',
                  type = 'scatter',
                  mode = 'none', 
                  stackgroup = 'one',
                  showlegend = T) %>%
  add_trace(y = ~ death_total, 
             name = "Death",
             fillcolor = '#E41317') %>%
  add_trace(y = ~recovered_total, 
            name = 'Recovered', 
            fillcolor = 'forestgreen') %>%
  layout(title = "Distribution of Covid19 Cases inside China",
         legend = list(x = 0.1, y = 0.9),
         yaxis = list(title = "Number of Cases", showgrid = F))

And I create the subplots as:

subplot(cases_not_china_plot, cases_china_plot, nrows = 2, margin = 0.05, shareX = T) %>%
  layout(title="Coronavirus cases outside China and in China", ylab("Number of cases"))

I am quite new to R, so if there is a smarter way to do what I desire, please let me know. With the above code, my output is:

subplots with plotly

Best regards

来源:https://stackoverflow.com/questions/62472949/plotly-subplots-with-shared-legend-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!