Как установить одинаковые цвета для групп на всех графиках в блестящем приложении? - PullRequest
0 голосов
/ 22 февраля 2020

Я использую библиотеку plotly, чтобы сделать кучу визуализаций внутри моего блестящего приложения, я думаю, это может быть такое простое кодирование, но я много пробовал и до сих пор не могу этого сделать. Я добавлю 3 графика на моем примере, но их много, и я просто хочу, чтобы у всего приложения были одинаковые цвета в каждой топи c. Моя главная проблема заключается в том, что мне нужно сохранить его обобщенным c, потому что количество групп / тем будет варьироваться в зависимости от набора данных, который я должен проанализировать, между 3 и 15. Код ниже настолько прост, насколько я мог бы сделать, потому что topic_color исходит из вектора длины 15, и он приспособлен для того, чтобы сохранять только один из них для каждой топи c в этом наборе данных и отбрасывать остальные.

# COLOR SETTING FOR ALL VISUALIZATIONS

ui <- fluidPage( 
  navbarPage("Analysis",
             tabPanel("Home",
                      mainPanel(
                        plotlyOutput("scape"),
                        plotlyOutput("per_topic_timeline"),
                        plotlyOutput("donut")
                      )
             )
  )
)

  server <- function(input, output, session) {

    # GRAPH 1
    output$scape <- renderPlotly({

      structure(list(topic = c("Sports", "Growing plants", "Reading", 
                               "Story telling", "Gymnastics", "Cooking classes", "Education", 
                               "Arts", "Arcade", "Acting", "Population growth", "Movies", "Science"
      ), n = c(42L, 36L, 56L, 82L, 94L, 24L, 23L, 46L, 61L, 43L, 48L, 
               52L, 23L), topic_color = c("antiquewhite3", "aquamarine3", "azure3", 
                                          "bisque2", "coral", "cadetblue3", "chocolate", "coral4", "darkgoldenrod1", 
                                          "darkseagreen", "gold", "lightpink2", "olivedrab")), row.names = c(NA, 
                                                                                                             -13L), class = "data.frame") %>% 
        plot_ly(labels = ~topic, 
                values = ~n, 
                parents = ~NA, 
                type = 'treemap',
                colors = ~topic_color,
                hovertemplate = "Topic: %{label}<br>Count: %{value}<extra></extra>") %>%
        config(displayModeBar = FALSE) %>%
        layout(title = "Scape")
    })

    # GRAPH 2

    output$per_topic_timeline <- renderPlotly({

      structure(list(Topic = c("Sports", "Sports", "Sports", "Sports", 
                               "Sports", "Sports", "Sports", "Sports", "Growing plants", "Growing plants", 
                               "Growing plants", "Growing plants", "Growing plants", "Growing plants", 
                               "Reading", "Reading", "Reading", "Reading", "Reading", "Reading", 
                               "Story telling", "Story telling", "Story telling", "Story telling", 
                               "Story telling", "Story telling", "Gymnastics", "Gymnastics", 
                               "Gymnastics", "Gymnastics", "Gymnastics", "Gymnastics", "Gymnastics", 
                               "Gymnastics", "Cooking classes", "Cooking classes", "Cooking classes", 
                               "Cooking classes", "Education", "Education", "Education", "Education", 
                               "Education", "Education", "Education", "Education", "Arts", "Arts", 
                               "Arts", "Arts", "Arts", "Arts", "Arcade", "Arcade", "Arcade", 
                               "Arcade", "Arcade", "Arcade", "Arcade", "Arcade", "Acting", "Acting", 
                               "Acting", "Acting", "Acting", "Acting", "Population growth", 
                               "Population growth", "Population growth", "Population growth", 
                               "Population growth", "Population growth", "Population growth", 
                               "Population growth", "Movies", "Movies", "Movies", "Movies", 
                               "Movies", "Movies", "Movies", "Science", "Science", "Science", 
                               "Science", "Science", "Science"), Year = c(2012, 2013, 2014, 
                                                                          2015, 2016, 2017, 2018, 2019, 2014, 2015, 2016, 2017, 2018, 2019, 
                                                                          2014, 2015, 2016, 2017, 2018, 2019, 2014, 2015, 2016, 2017, 2018, 
                                                                          2019, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2014, 2017, 
                                                                          2018, 2019, 2011, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2013, 
                                                                          2015, 2016, 2017, 2018, 2019, 2012, 2013, 2014, 2015, 2016, 2017, 
                                                                          2018, 2019, 2013, 2014, 2016, 2017, 2018, 2019, 2012, 2013, 2014, 
                                                                          2015, 2016, 2017, 2018, 2019, 2012, 2014, 2015, 2016, 2017, 2018, 
                                                                          2019, 2014, 2015, 2016, 2017, 2018, 2019), Count = c(1L, 1L, 
                                                                                                                               1L, 4L, 2L, 10L, 11L, 12L, 1L, 1L, 2L, 7L, 10L, 15L, 4L, 2L, 
                                                                                                                               2L, 5L, 22L, 21L, 1L, 10L, 20L, 21L, 17L, 13L, 3L, 2L, 3L, 4L, 
                                                                                                                               6L, 18L, 31L, 27L, 1L, 1L, 10L, 12L, 1L, 1L, 1L, 1L, 2L, 4L, 
                                                                                                                               3L, 10L, 1L, 6L, 7L, 4L, 14L, 14L, 1L, 1L, 2L, 1L, 8L, 16L, 16L, 
                                                                                                                               16L, 2L, 3L, 5L, 9L, 7L, 17L, 2L, 2L, 3L, 2L, 3L, 4L, 18L, 14L, 
                                                                                                                               1L, 1L, 3L, 3L, 6L, 21L, 17L, 1L, 1L, 1L, 2L, 7L, 11L), topic_color = c("antiquewhite3", 
                                                                                                                                                                                                       "antiquewhite3", "antiquewhite3", "antiquewhite3", "antiquewhite3", 
                                                                                                                                                                                                       "antiquewhite3", "antiquewhite3", "antiquewhite3", "aquamarine3", 
                                                                                                                                                                                                       "aquamarine3", "aquamarine3", "aquamarine3", "aquamarine3", "aquamarine3", 
                                                                                                                                                                                                       "azure3", "azure3", "azure3", "azure3", "azure3", "azure3", "bisque2", 
                                                                                                                                                                                                       "bisque2", "bisque2", "bisque2", "bisque2", "bisque2", "coral", 
                                                                                                                                                                                                       "coral", "coral", "coral", "coral", "coral", "coral", "coral", 
                                                                                                                                                                                                       "cadetblue3", "cadetblue3", "cadetblue3", "cadetblue3", "chocolate", 
                                                                                                                                                                                                       "chocolate", "chocolate", "chocolate", "chocolate", "chocolate", 
                                                                                                                                                                                                       "chocolate", "chocolate", "coral4", "coral4", "coral4", "coral4", 
                                                                                                                                                                                                       "coral4", "coral4", "darkgoldenrod1", "darkgoldenrod1", "darkgoldenrod1", 
                                                                                                                                                                                                       "darkgoldenrod1", "darkgoldenrod1", "darkgoldenrod1", "darkgoldenrod1", 
                                                                                                                                                                                                       "darkgoldenrod1", "darkseagreen", "darkseagreen", "darkseagreen", 
                                                                                                                                                                                                       "darkseagreen", "darkseagreen", "darkseagreen", "gold", "gold", 
                                                                                                                                                                                                       "gold", "gold", "gold", "gold", "gold", "gold", "lightpink2", 
                                                                                                                                                                                                       "lightpink2", "lightpink2", "lightpink2", "lightpink2", "lightpink2", 
                                                                                                                                                                                                       "lightpink2", "olivedrab", "olivedrab", "olivedrab", "olivedrab", 
                                                                                                                                                                                                       "olivedrab", "olivedrab")), row.names = c(NA, -87L), class = "data.frame") %>%
        complete(Topic, Year = unique(Year), fill = list(Count = 0)) %>% 
        plot_ly(x = ~Year, y = ~Count, color = ~Topic, colors = ~topic_color, type = 'scatter', mode = 'lines+markers') %>%
        config(displayModeBar = FALSE) %>%
        layout(title = "Topics timeline") 

    })

    # GRAPH 3

    output$donut <- renderPlotly({
      plot_ly(structure(list(topic = c("Sports", "Growing plants", "Reading", 
                                       "Story telling", "Gymnastics", "Cooking classes", "Education", 
                                       "Arts", "Arcade", "Acting", "Population growth", "Movies", "Science"
      ), n = c(42L, 36L, 56L, 82L, 94L, 24L, 23L, 46L, 61L, 43L, 48L, 
               52L, 23L), topic_color = c("antiquewhite3", "aquamarine3", "azure3", 
                                          "bisque2", "coral", "cadetblue3", "chocolate", "coral4", "darkgoldenrod1", 
                                          "darkseagreen", "gold", "lightpink2", "olivedrab")), row.names = c(NA, 
                                                                                                             -13L), class = "data.frame"),

      labels = ~topic, 
      values = ~n,
      marker = list(color = ~topic_color)) %>% 
        add_pie(hole = 0.5) %>%
        layout(colorway = ~topic_color) %>%
        config(displayModeBar = FALSE)

    })

  }

  shinyApp(ui,server)

Многие вопросы используют add_trace, но я не действительно понимаю, если есть способ и как сделать его обобщенным c, я бы добавил, чтобы добавить след для каждого topi c, и это не то, что я ищу. Другие используют add_markers со списком, но не работает на моем третьем графике. Единственный, который, кажется, работает хорошо, это второй, но иногда он оставляет Acting без отображаемого цвета, это похоже на призрак, который появляется только в Легенде. phantom

...