在 R 中格式化包含许多子图的绘图

问题描述 投票:0回答:1

请我有以下数据和带有子图的绘图:

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)

我面临的问题是,y 轴上的刻度大小没有考虑我传递的值(本例中为 20)。我找不到任何显示如何修复我的代码的示例。有什么想法吗?谢谢你

r plotly
1个回答
0
投票

问题是

yaxis
没有属性
tick_font
(或
title_font
)。要设置刻度大小,请使用
tickfont = list(size = 20)
。对于轴
title
使用例如
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)

© www.soinside.com 2019 - 2024. All rights reserved.