Patchwork and plotly

Somsak Chanaim, member2, member 3

2023-03-15

What is the patchwork package

The goal of patchwork package is to make it ridiculously simple to combine separate ggplots into the same graphic.

library(patchwork)

The patchwork logo

The ggplot graph from qplot function

use data mtcars

data("mtcars")
plot.1 <- qplot(data = mtcars, x= mpg, geom = "histogram")

plot.2 <- qplot(data = mtcars, x= disp, geom = "density")

plot.3 <- qplot(data = mtcars, x= mpg, y =disp, 
                geom = c("point","smooth"))

plot.4 <- qplot(data = mtcars, x= gear, y =disp, 
                group = gear, geom = "boxplot")
plot.1
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

plot.2

plot.3
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

plot.4

Adding plots to the patchwork

At this point, it shouldn’t come as a surprise that you can use \(+\) to add plots together to form a patchwork.

plot.1+plot.2

Stacking and packing

For the special case of putting plots besides each other or on top of each other patchwork provides / operators.

plot.1 / plot.2 

For up to 3 plots

We can apply | will behave just as + but using | will communicate the intend of the layout better.

plot.4/ (plot.1+plot.2)

or

(plot.1+plot.2)/ plot.4

plot.12 <- plot.1+plot.2
plot.12
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

plot.12/plot.4
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

plot.1/plot.2/plot.3/plot.4
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Nesting the left-hand side As plots will always be added to the patchwork on the left-hand side, it is not possible to nest the left-hand side beside the right-hand side with the standard operators shown above.

plot.5 <- (plot.1/plot.2)
plot.4 + plot.5

plot.4 + (plot.1/plot.2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

plot.1+theme_minimal()+ggtitle("histogram")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(data = mtcars)+
aes(x = mpg, y = disp)+
geom_point()

ggplot(mtcars) +
  aes(x = mpg, y = disp) +
  geom_point(shape = "circle", size = 1.5, color = "#112446") 

Modifying theme

Often, especially when it comes to theming, you want to modify everything at once. patchwork provides two additional operators that facilitates this. & will add the element to all subplots in the patchwork, and * will add the element to all the subplots in the current nesting level.

(plot.4+theme_light()) + (plot.1+theme_light())/(plot.2+theme_light())
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

by patchwork

(plot.4 + plot.5) & theme_light()

(plot.4 + plot.5) & theme_dark()

library(ggthemes)
(plot.1/(plot.2+plot.3)) & theme_economist()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

(plot.4 + plot.5)*theme_minimal()

(plot.4+theme_minimal()) + plot.5

(plot.1/(plot.2+plot.3))*theme_economist()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

plot.1/( (plot.2+plot.3)&theme_economist() )
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

plot.1+( plot.5&theme_economist() )
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Adding Annotation and Style

Titles, subtitles and captions

One of the most needed things is to add descriptive text to your plot ensemble. To achieve this, you simply add it to your patchwork using plot_annotation()

plot.6 <- (plot.4 + plot.5)*theme_minimal()+
   plot_annotation(
                  title = "Box plot, histogram, and density plot.",
               subtitle = "created by patchwork",
                caption = "mtcars dataset")

plot.6

ggplot()+theme_minimal()

(plot.1+qplot())/(qplot()+plot.4)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

(plot.1+(qplot()+theme_minimal()) )/
   ( (qplot()+theme_minimal())+plot.4)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Plotly package

What is Plotly?

Plotly is an open-source data visualization library for Python and R written in JavaScript, making graphs inherently interactive. They can be modified tremendously and are easily exported as images, HTML, etc. for seamless integration into a number of applications.

library(plotly)

The plotly logo

How to use plotly

step 1: Created a ggplot object.

step 2:put ggplot object in ggplotly() function

ggplotly(plot.3)

View multiple plots in a single view with plotly by the subplot() function

subplot(plot.1,plot.2,plot.3,plot.4, nrows =2)
subplot(plot.1,plot.2,plot.3,plot.4, nrows =1)
subplot(plot.1,qplot(),qplot(),plot.3, nrows =2)

Warning

  • plotly is not work with ggplot object from patchwork.

  • plotly is not work for pdf.