1
votes

Hi i'm using ggplotly to add interactivity with a plot i'm making with ggplot2 for a shiny dashboard. the plot functions correctly, it just looks goofy because it's not consistently using my theme i normally use ( theme_jacob() ). i'm aware there's a lot of hiccups when applying plotly functions onto a ggplot plot but i can't find any resources online for a fix for my specific issue. i've pasted my code below.

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 = ""))))

below is an example of what the plot looks like, it's distorted but it's the correct size in my shiny dashboard. you can see it's using my theme everywhere except the actual plot where it turns into a default white background and idk how to fix that. if anyone has any advice i'd appreciate it !

enter image description here

1

1 Answers

3
votes

You need the statement panel.background = element_rect(fill = "floralwhite") in your theme_jacob(). That should work.