Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formatting a plotly plot with many subplots in R

please I have the following data and plot with subplots:

library(plotly)

# Create example data
set.seed(123)
df <- data.frame(
  DATE = seq(as.Date("2021-01-01"), as.Date("2021-01-10"), by = "day"),
  FEATURE1 = rnorm(10),
  FEATURE2 = rnorm(10),
  FEATURE3 = rnorm(10),
  FEATURE4 = rnorm(10),
  FEATURE5 = rnorm(10),
  FEATURE6 = rnorm(10),
  FEATURE7 = rnorm(10),
  FEATURE8 = rnorm(10),
  FEATURE9 = rnorm(10),
  FEATURE10 = rnorm(10)
)

# Reshape data from wide to long format
df_long <- tidyr::pivot_longer(df, cols = -DATE, names_to = "Feature", values_to = "Value")

plots <- lapply(unique(df_long$Feature)[-1], function(feature) {
    plot_ly(df_long[df_long$Feature == feature, ], x = ~BUSINESS_DATE, y = ~Value, type = "scatter", mode = "lines",
            line = list(color = "blue"), showlegend = FALSE, text = paste0("Feature: ", feature)) %>%
      layout(yaxis = list(  title = list(text = feature),
                            title_font = list(size=20),
                            tick_font = list(size = 20)
                          ),
             annotations = list(
               list(x = 0.5, y = 1, text = feature, showarrow = FALSE, xref = "paper", yref = "paper",
                    font = list(size = 6))
             )
             )
  })
  subplot(plots, nrows = 5)

The issue I face is that I the tick size on the y-axis doesn’t take into account the value I’m passing (20 in this example). I can’t find any example that shows how to fix my code. Any thoughts? Thank you

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The issue is that yaxis has no attribute tick_font (or title_font). To set the tick size use tickfont = list(size = 20). For the axis title use e.g. title = list(font = list(size = 20))

library(plotly, warn = FALSE)
#> Loading required package: ggplot2

plots <- lapply(unique(df_long$Feature)[-1], function(feature) {
  plot_ly(df_long[df_long$Feature == feature, ],
    x = ~BUSINESS_DATE, y = ~Value, type = "scatter", mode = "lines",
    line = list(color = "blue"), showlegend = FALSE,
    text = paste0("Feature: ", feature)
  ) %>%
    layout(
      yaxis = list(
        title = list(
          text = feature,
          font = list(size = 20)
        ),
        tickfont = list(size = 20)
      ),
      annotations = list(
        list(
          x = 0.5, y = 1,
          text = feature,
          showarrow = FALSE,
          xref = "paper",
          yref = "paper",
          font = list(size = 6)
        )
      )
    )
})
subplot(plots, nrows = 5)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading