Search code examples
rggplot2ggplotly

Plot subtitle is lost when plot is diplayed with ggplotly()


I create the ggplot below with title and subitle but when I diplay it using ggplotly() the subtitle is lost.

library(ggplot2)
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_boxplot()
library(plotly)
p <- p + labs(title = "Effect of Vitamin C on Tooth Growth",
              subtitle = "Plot of length by dose",
              caption = "Data source: ToothGrowth")
ggplotly(p)

Solution

  • library(ggplot2)
    p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
      geom_boxplot()
    library(plotly)
    p <- p + labs(title = "Effect of Vitamin C on Tooth Growth",
                  subtitle = "Plot of length by dose",
                  caption = "Data source: ToothGrowth")
    ggplotly(p)%>% 
      layout(title = list(text = paste0('Effect of Vitamin C on Tooth Growth"',
                                        '<br>',
                                        '<sup>',
                                        'Plot of length by dose','</sup>')))
    

    This works let me know if it fixes your issue