ggplotly 主题与 ggplot2 不一致

ggplotly theme inconsistency with ggplot2

你好,我正在使用 ggplotly 来添加与我正在使用 ggplot2 制作的图表的交互性,以获得闪亮的仪表板。情节正常运行,它看起来很愚蠢,因为它没有始终如一地使用我通常使用的主题( theme_jacob() )。我知道将 plotly 函数应用于 ggplot 图时会出现很多问题,但我无法在网上找到任何资源来解决我的特定问题。我在下面粘贴了我的代码。

theme_jacob <- function () { 
  theme_minimal(base_size=10, base_family="Gill Sans MT") %+replace% 
    theme(
      panel.grid.minor = element_blank(),
      plot.background = element_rect(fill = 'floralwhite', color = 'floralwhite')
    )
}

  p <- df %>%
    ggplot(aes(valence, energy, color = album_name, text = paste(track_name, '<br>', album_name, '<br>',
                                                                 'Positivity ', round(valence, 2), '<br>',
                                                                 'Energy ', round(energy, 2)))) +
    geom_point(size = 2, alpha = 0.8) +
    geom_hline(yintercept = 0.5) +
    geom_vline(xintercept = 0.5) +
    scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, .1)) +
    scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, .1)) +
    annotate('text', x = 0.99, y = 1, label = 'Happy / Upbeat') +
    annotate('text', x = 0.99, y = 0, label = 'Mellow / Peaceful') +
    annotate('text', x = 0.01, y = 0, label = 'Sad / Slow') +
    annotate('text', x = 0.05, y = 1, label = 'Aggressive / Fast Paced') +
    labs(x = "Positivity",
         y = "Energy",
         color = 'Album',
         title = paste(df$artist_name, ' Song Distribution', sep = ""),
         caption = paste('Data collected via spotifyr Package on ', mdyDate, sep = "")) +
    theme_jacob() +
    theme(legend.position = 'top')
  ggplotly(p, tooltip = c('text')) %>%
    layout(title = list(text = paste0(paste(df$artist_name, ' Song Distribution', sep = ""))))

下面是该图的示例,它被扭曲了,但在我闪亮的仪表板中它的大小是正确的。你可以看到它到处都在使用我的主题,除了实际的情节,它变成了默认的白色背景,我不知道如何解决这个问题。如果有人有任何建议,我将不胜感激!

您的 theme_jacob() 中需要语句 panel.background = element_rect(fill = "floralwhite")。那应该可以。