Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Subplot changing appearance of plots

I want to create a table of interactive plots in Shiny. The table has a variable number of rows, depending on the data.
My approach is to first create the plots (using ggplot2), and then to use subplot to create the rows and columns.
However, there are some problems with the layout.
For example, the height of first and last rows is higher than the height of rows in the middle.
Furthermore, even if I use theme_void() in the ggplot2 plots, the grids are shown.
I have narrowed the problem down to an example :

library(tidyverse)
library(plotly)
library(gridExtra)

Creating a simple data frame

x <- seq(1:3)
xmin <- min(x)
xmax <- max(x) + 1

function to create the ggplot2-plot

plot_x <- function(x) {
df <- data.frame(x = x, y = 1)
p <- ggplot(df, aes(xmin = x, xmax = x + 1, ymin = 1, ymax = 2)) +
geom_rect() +
coord_cartesian(xlim = c(xmin, xmax), ylim = c(1, 2)) +
theme_void()

p
}

create a list of ggplot2 plots

plots <- lapply(x, plot_x)

the desired output, although not interactive

grid.arrange(grobs = plots, ncols = 1)

applying subplot

(py <- subplot(plots, nrows = length(x), which_layout = “merge”, shareY = TRUE, shareX = TRUE))

applying subplot with layout

py %>%
layout(margin = margin(l = 0, r = 0, t = 0, b = 0),
xaxis = list(
visible = FALSE
),
yaxis = list(
visible = FALSE,
autorange = FALSE,
fixedrange = TRUE,
showgrid = FALSE
)
)

In this code a first create a list of simple plots.
I use grid.arrange to create a static plot containing the subplots. I want no grid lines, nor axes.

There are two problems when I apply the list of plots to the subplot function using

(py <- subplot(plots, nrows = length(x), which_layout = “merge”, shareY = TRUE, shareX = TRUE))

The rectangles for the first and last row are higher than for the middle row. This becomes clearer as you increase the number of plots in the sequence for variable x.
The grid lines are shown.

In a second trial, I try to add a layout where I hide the grid lines for the yaxis, but this only works on the first of the subplots.

Does anyone have any advice on solving this issue ?

Thank you

Bart

Hello,

I have the same problem with the height of plots : the first and last one are higher !
So I will be very glad t ofind an answer !
AS for your question about layout, well each plot should hold their own layout ! Otherwise, your layout applies to only one of the plots, as you have realised.
In your plot_x function, wrap your ggplot in a ggplotly, and then add the layout expression :

plot_x <- function(x) {
df <- data.frame(x = x, y = 1)
p <- ggplotly(
ggplot(df, aes(xmin = x, xmax = x + 1, ymin = 1, ymax = 2)) +
geom_rect() +
coord_cartesian(xlim = c(xmin, xmax), ylim = c(1, 2)) +
theme_void()
) %>%
layout(margin = margin(l = 0, r = 0, t = 0, b = 0),
xaxis = list(
visible = FALSE
),
yaxis = list(
visible = FALSE,
autorange = FALSE,
fixedrange = TRUE,
showgrid = FALSE
)
)
p
}

Regards,
Parisa