Search code examples
rplotlyr-plotly

Setting titles in Plotly


I work with Plotly and I want to put four charts in the same picture. Below you can see data

library(dplyr)
library(plotly)

df<-data.frame(
  Years = rep(2010:2020,len=22),
  Tax = rep(c("PIT","CIT","VAT"),22),
  Revenues = rep(c(200,400,100),22)
)



df$Pct<-prop.table(df$Revenues)


RevenueCompositionPCT <- plot_ly(df, x = ~Years, y = ~Pct,
                                 type = 'bar',
                                 marker = list(color = ~Revenues), name = ~Tax) %>%
  layout(title = "AA",
         yaxis = list(title = 'Percentage %'), barmode = 'stack')
RevenueCompositionPCT <- RevenueCompositionPCT %>% layout(legend = list(orientation = 'h'))           
RevenueCompositionPCT

Now I want to put four charts with separate titles and also titles on the y-axis on the same pictures. I tried with the code below but is not work properly.

p<-subplot(
  style(RevenueCompositionPCT, showlegend = FALSE,title=TRUE,titleY = TRUE),
  style(RevenueCompositionPCT, showlegend = FALSE,title=TRUE,titleY = TRUE),
  style(RevenueCompositionPCT, showlegend = FALSE,title=TRUE,titleY = TRUE),
  style(RevenueCompositionPCT, showlegend = TRUE,title=TRUE,titleX = TRUE, ),
  nrows =2,margin = 0.05) 
p

So can anybody help me how to solve this problem?

enter image description here


Solution

  • I think it's easier with the combineWidgets function of the manipulateWidget package:

    library(manipulateWidget)
    combineWidgets(p1, p2, p3, p4, nrow = 2)
    

    where p1, p2, p3, p4 are your plotly plots.