繁体   English   中英

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

[英]How to get the same colors on different charts (plot_ly, 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)  })

图表上的相同值(即“无法修复”)具有不同的颜色。 在此处输入图片说明

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

ggplot2和plotly使用不同的配色方案。 您应该提供对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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM