1

我正在创建一个闪亮的 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)  })

图表上的相同值(即不会修复)具有不同的颜色。 在此处输入图像描述

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

4

1 回答 1

0

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

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)
于 2017-11-21T15:17:00.403 回答