Combining Plotly and ggplot2 charts with Patchwork in one Facet

37 Views Asked by At

I want to combine two charts prepared with Plotly and ggplot2 into one PDF. Below, you can see the code for the preparation of the charts:

library(ggplot2)
library(plotly)
library(dplyr)
library(patchwork)

## 1.Plotly

dt=data.frame(
  types= rep("stories",10),
  kind= c('kind_1','kind_2','kind_3','kind_1','kind_2','kind_3','kind_1','kind_2','kind_3','kind_1'),
  values=seq(1:10))

Treemap_plotly<-plot_ly(data = dt,
                 type= "treemap",
                 values= ~values,
                 labels= ~kind,
                 parents=  ~types,
                 domain= list(column=0),
                 name = " ",
                 textinfo="label+value+percent parent")%>%
            layout(title="Treemap")


## 2.ggplot2

df1 <- data.frame(dose=c("D0.5", "D1", "D2"),
                 len=c(4.2, 10, 29.5))


barchart_ggplot2<-ggplot(data=df1, aes(x=dose, y=len)) +
          geom_bar(stat="identity", fill="steelblue")+
          geom_text(aes(label=len), vjust=-0.3, size=3.5)+
          theme_minimal()

Now, I want to combine them into one facet plot using the patchwork library. Below, you can see the command:

# Combine both

(Treemap_plotly/barchart_ggplot2)

Unfortunately, this does not work. Below is my desired graph:

enter image description here

enter image description here

1

There are 1 best solutions below

0
M-- On

patchwork does not work with plotly objects. It's possible to create the treemap in ggplot2 but it involves some more work or using packages like treemapify.

We could save the treemap graph as an image and then use gridExtra to put the plots side by side:

library(dplyr)
library(plotly)
library(ggplot2)
library(magick)
library(grid)
library(gridExtra)

Treemap_plotly_file <- tempfile(fileext = ".png")
#export is soft deprecated, but for now it's the best option
suppressWarnings(invisible(export(Treemap_plotly, file = Treemap_plotly_file))) 
my_img <- image_read(Treemap_plotly_file)

barchart_ggplot2<- barchart_ggplot2 + theme(plot.margin = margin(1,0.5,1,0, "cm"))

grid.arrange(rasterGrob(my_img), barchart_ggplot2, widths = c(0.6, 0.4))

But this will look janky depending on the size of the plots and whatnot. So, I'd suggest creating both plots with plotly and then use subplot.

library(dplyr)
library(plotly)

Treemap_plotly <- plot_ly(data = dt, type= "treemap", 
                        values= ~values, labels= ~kind, parents=  ~types,
                        domain= list(column=0), name = " ",
                        textinfo="label+value+percent parent") %>% 
  layout(title = list(text = "Treemap", x = 0.1))

barchart_plotly <- df1 %>% 
                    mutate(clr = "#619CFF") %>% 
                    plot_ly(data = , x = ~dose, y = ~len, 
                           color = ~clr, colors = "#619CFF", 
                           type = "bar", showlegend = F,
                           text = ~sprintf("<b>%s</b>", len), 
                           textfont = list(color = "black"),
                           textposition='outside')

subplot(Treemap_plotly, barchart_plotly, widths = c(0.6, 0.3))

Created on 2024-03-26 with reprex v2.0.2