如何在不同的图表上获得相同的颜色(plot_ly,ggplotly)

时间:2017-11-20 14:27:08

标签: r shiny plotly ggplotly

我正在创建一个R闪亮的仪表板,我创建了两个图表:

output$plotDefPriorBar <- renderPlotly({
ggplotly(
  ggplot(data = dataDefPrior_barchart(), aes(x = TaskDate, y = DefectCount, fill = TaskPriority)) +        theme(axis.ticks.x=element_blank(),axis.text.x=element_text(angle=90,hjust=1,size = rel(0.7)) ) +
    geom_bar(stat = "identity") +
    xlab("Date") + ylab("Total defects")
) })
output$plotDefPrior <- renderPlotly({
plot_ly(dataInteractivePlotDefPrior(),
        values = counts,
        labels = task_priority,
        marker = list(colors = brewer.pal(12, "Set3")),
        type = 'pie',
        showlegend = TRUE) })

,其中

dataDefPrior <- reactive({
data() %>%
  select(-c(DefectType, TaskResolution)) %>%
  dcast(EpicName ~ TaskPriority) %>%
  arrange(EpicName) %>%
  janitor::adorn_totals(which = c("row", "col")) %>%
  arrange(desc(Total)) })

dataDefPrior_barchart <- reactive({
data() %>%
  select(TaskCreateDate,TaskPriority) %>%
  mutate(CreateDate = as.Date(TaskCreateDate)) %>%
  mutate(TaskDate = format(as.Date(CreateDate),"%Y-%m"))%>%
  select(-c(TaskCreateDate,CreateDate)) %>%
  group_by(TaskDate,TaskPriority) %>%
  summarize(DefectCount = n()) %>%
  arrange(TaskDate)  })

图表上的相同值(即不会修复)具有不同的颜色。 enter image description here

如何解决?颜色应该在图表上保持一致。

1 个答案:

答案 0 :(得分:0)

ggplot2并使用不同的配色方案。您应该为plot_lyggplot提供颜色映射,如下所示:

cols <- brewer.pal(12, "Set3")
g <- ggplot(data = dataDefPrior_barchart(), aes(x = TaskDate, y = DefectCount, fill = TaskPriority)) +
    geom_bar(stat = "identity") +
    xlab("Date") + ylab("Total defects") + scale_fill_manual(values=cols)

## For plotly
plot_ly(dataInteractivePlotDefPrior(),
        values = counts,
        labels = task_priority,
        marker = list(colors = cols),
        type = 'pie',
        showlegend = TRUE)