Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plots seem merged when using subplot in plotly #2377

Open
mayank7j-shell opened this issue Aug 14, 2024 · 1 comment
Open

Plots seem merged when using subplot in plotly #2377

mayank7j-shell opened this issue Aug 14, 2024 · 1 comment

Comments

@mayank7j-shell
Copy link

Description:

When attempting to display multiple histograms using subplot from the plotly package, the plots appear merged together, making them difficult to distinguish. This issue is primarily reproducible when there are large number of visualizations to plot together.

Here is a minimal reprex for the same:

# Load necessary libraries
library(ggplot2)
library(plotly)

# set seed for reproducibility
set.seed(123)

# simulate a dataset
simulateDataset <- function(numRows = 100, numCols = 40) {
  data <- data.frame(matrix(nrow = numRows, ncol = numCols))
  
  for (i in 1:numCols) {
    if (i %% 2 == 0) {
      data[[i]] <- rnorm(numRows, mean = 50, sd = 10)  # Normal distribution
    } else {
      data[[i]] <- rexp(numRows, rate = 0.1)           # Exponential distribution
    }
  }
  
  names(data) <- paste0("Var", 1:numCols)
  return(data)
}

# simulate the data
df <- simulateDataset(numRows = 100, numCols = 60)

# create histograms and convert to plotly
plotList <- lapply(names(df), function(colName) {
  if (is.numeric(df[[colName]])) {
    p <- ggplot(df, aes(x = .data[[colName]])) + 
      geom_histogram(binwidth = 0.5, fill = 'grey', color = 'black') +
      theme_minimal()
    
    ggplotly(p)
  }
})

# define number of rows based on number of plots in each row
numberOfRows <- ceiling(length(plotList) / 3)

# display all plots together using subplot
subplot(plotList, nrows = numberOfRows, margin = 0.05, titleX = TRUE, titleY = TRUE)

Expected Behavior:
Each histogram should be clearly separated from the others, with appropriate spacing and labels visible.

Screenshot

image

@romanzenka
Copy link

You are drawing 20 rows with titles with margin.

20 times 0.05 is 1, so you are asking the library to spend all the vertical space on margin alone.

What should be correct behavior here? I would personally throw an exception "Cannot satisfy requirements" or similar and give up.

Here's what I see when I take the margin=0.05 out:

Image

The titles are still incorrectly positioned, but it gives me some idea about what you are asking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants