0

I have converted the basic ggplot into plotly graph but unable to move legend title.

  1. Basic ggplot:
cases_ts_India <- df_stack %>% 
  filter(Country.Region == "India") %>% 
  
  ggplot(aes(x = Date, y = Cases_count, col = Cases_type)) +
  geom_path()

cases_ts_India

enter image description here

  1. plotly graph:

enter image description here

  1. Moved the legend but unable to move legend title and its showing starting part of the title at the old location:
ggplotly(cases_ts_India) %>% 
  layout(legend = list(orientation = "h", y =-0.2))

enter image description here

I have tried moving title but it didn't work with below lines of code:

ggplotly(cases_ts_India) %>% 
  layout(legend = list(orientation = "h", x =0.2, y = -0.2),
         legend.title = list(orientation = "h", x =0.2, y = -0.2))

Updating Attempt:

cases_ts_India <- cases_ts_India + 
  theme( legend.title = element_blank())
                  
ggplotly(cases_ts_India) %>% 
  layout(title = "India Cases types over the year ",
         legend = list(orientation = "h", x =0.2, y = -0.2)) %>% 
  add_annotations( text="Cases_type",
                  x=0.1, xanchor="left",
                  y=-0.2, yanchor="bottom",    # Same y as legend below
                  legendtitle=TRUE, showarrow=FALSE )

enter image description here

ViSa
  • 1,563
  • 8
  • 30
  • 1
    Have you tried https://stackoverflow.com/questions/38034308/legend-title-in-plotly ? – Ronak Shah Dec 27 '20 at 13:52
  • 1
    @RonakShah thanks for sharing the link. I have tried a modification of that but it didnt work. Updating the Original post with the code & result – ViSa Dec 27 '20 at 14:22
  • 2
    Please try solutions [here](https://stackoverflow.com/questions/40001518/add-title-to-the-plotly-legend) – YBS Dec 27 '20 at 14:25
  • thanks @YBS, was able to run it successfully with this !! – ViSa Dec 27 '20 at 14:44

0 Answers0