Renaming legend text in ggplotly

I have integrated my ggplot code into ggplotly but the scale_manual_color() for renaming the legend texts isn't recognised by ggplotly.

ggplotly(
      ggplot(data = national2,aes(x=period, y=pregnancy))+
        geom_line(aes(color=type),size=0.8)+
        scale_color_manual(values=c("red","blue","green"),
                           breaks = c("pregnancy1019","pregnancy1519","pregnancy1014"),
                           labels=c("Teen pregnancy (10-19years)","Teen pregnancy (15-19years)","Teen pregnancy (10-14years)"))+
        labs(
          title = "Teen pregnancy trends from 2016Q1 to 2020Q2",
          x="Yearly quarters",
          y="Teen pregnancies"

        )+
        scale_x_yearqtr(n=10)+
        theme_gray()+
        theme(
          plot.title = element_text(size = 14),
          legend.title = element_blank(),
          legend.text = element_text(size = 12),
          axis.title = element_text(face = "bold",size = 11),
          axis.text = element_text(face = "bold",size = 10)
      )
  )

For full control of building a plotly chart, its best to learn the plotly syntax and do it the plotly way. ggplotly should be thought of as a convenience that often works, but only often.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.