填充选项中指定的ggplot重新排序变量

劳里·赫塔(Lauri Huhta)

我正在创建一个条形图,并在其中使用填充来区分5个不同的变量。这是代码:

data <- suppressMessages(read_csv(csvpath))

# Changing the order of the data to suit better for Cumulative Flow Diagram
cfdLevels <- c("Backlog", "Selected for Development", "In Progress", "Closed", "Done")
data$status <- factor(data$status,levels = cfdLevels)
## Cumulative Flow Diagram ##
cumplot_pre <- ggplot(data, aes(x = currentdate, fill = status))
cumplot_pre + geom_bar(aes(y = ..count.., text = paste('Date: ', as.Date(currentdate), '\n',
                                                      'Status: ', status)))+
              ggtitle("Cumulative Flow Diagram")+
              theme(axis.title.x = element_blank())+
              ylab("Count of Tickets")+
              scale_fill_manual(values = c("#2eb9d1", "#7f1dcf", "#fc3903", "#a6948f", "#fc9d03"))

这是图形: 在此处输入图片说明

我想要实现的是,fill-option中指定的状态变量的顺序是我试图在代码行5中重新排序(以data $ status开头),但是我只实现了如何重新排序它出现在图例中。

如何在图形本身中指定这些变量的顺序?

编辑以包含dput:

structure(list(currentdate = structure(c(18392, 18392, 18392, 
18392, 18392, 18392, 18392, 18392, 18392, 18392, 18392, 18392, 
18392, 18392, 18392, 18392, 18392, 18392, 18392, 18392, 18392, 
18392, 18392, 18392, 18392, 18392, 18392, 18392, 18392, 18392, 
18392, 18392, 18392, 18392, 18392, 18392, 18392, 18392, 18392, 
18406, 18406, 18377, 18377, 18377, 18377, 18377, 18377, 18390, 
18390, 18390, 18390, 18390, 18390, 18390, 18390, 18390, 18390, 
18390, 18390, 18390, 18390, 18390, 18390, 18390, 18390, 18390, 
18390, 18390, 18390, 18390, 18390, 18390, 18390, 18390, 18390, 
18390, 18390, 18390, 18390, 18406, 18406, 18406, 18406, 18406, 
18406, 18406, 18406, 18406, 18406, 18406, 18377, 18406, 18406, 
18406, 18406, 18389, 18389, 18389, 18389, 18389), class = "Date"), 
    status = structure(c(1L, 5L, 5L, 5L, 1L, 5L, 1L, 5L, 5L, 
    5L, 1L, 5L, 5L, 5L, 5L, 5L, 2L, 5L, 5L, 1L, 3L, 2L, 2L, 5L, 
    5L, 1L, 2L, 3L, 5L, 5L, 5L, 5L, 3L, 5L, 1L, 2L, 1L, 3L, 5L, 
    1L, 5L, 2L, 2L, 1L, 3L, 1L, 2L, 1L, 5L, 5L, 5L, 1L, 5L, 1L, 
    5L, 5L, 5L, 1L, 5L, 5L, 5L, 5L, 5L, 2L, 5L, 5L, 1L, 3L, 2L, 
    2L, 5L, 5L, 1L, 1L, 1L, 2L, 1L, 2L, 3L, 5L, 5L, 1L, 5L, 1L, 
    5L, 5L, 5L, 1L, 5L, 5L, 1L, 5L, 5L, 5L, 2L, 1L, 5L, 5L, 5L, 
    1L), .Label = c("Backlog", "Selected for Development", "In Progress", 
    "Closed", "Done"), class = "factor")), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))
艾伦·卡梅伦

我认为最好根据分组计数绘制汇总表。这样,堆叠的值将以与图例颜色相同的顺序出现:

library(dplyr)

data %>% 
  group_by(currentdate, status) %>% 
  summarise(count = length(status)) %>%
  ggplot(aes(x = currentdate, y = count, fill = status)) +
  geom_col(position = position_stack()) +
  ggtitle("Cumulative Flow Diagram")+
  theme(axis.title.x = element_blank())+
  ylab("Count of Tickets")+
  scale_fill_manual(values = c("#2eb9d1", "#7f1dcf", "#fc3903", 
                               "#a6948f", "#fc9d03"))

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据不同NSOrderedSet中指定的顺序对NSOrdered Set重新排序

来自分类Dev

按组对ggplot x变量重新排序

来自分类Dev

在Python中指定变量

来自分类Dev

文件中指定命令的选项

来自分类Dev

在函数中指定分组变量

来自分类Dev

在函数中指定变量的准则

来自分类Dev

如何根据我指定的变量在ggplot中排序点

来自分类Dev

在皮肤json文件中指定TextButton填充?

来自分类Dev

在tmap中指定尺寸图例的填充颜色

来自分类Dev

重新填充以选择选项

来自分类Dev

重新排序变量

来自分类Dev

在Eclipse CDT中指定外部Makefile选项

来自分类Dev

在Gatling中指定套接字选项

来自分类Dev

显示Ant选项,在调用中指定属性

来自分类Dev

在网格中指定默认排序

来自分类Dev

如何只排序JTable中指定的列?

来自分类Dev

ggplot2中的自定义重新排序直方图填充类别

来自分类Dev

如何使用填充重新排序ggplot geom_col中的cols?

来自分类Dev

在ggplot barplot中指定条形颜色

来自分类Dev

在R中指定了X变量的Logit预测

来自分类Dev

在协议中指定可设置的属性/变量

来自分类Dev

Java-类中指定的变量类型

来自分类Dev

在 stan 中指定整数潜在变量

来自分类Dev

如何在 dcast 中指定 ID 变量?

来自分类Dev

ggplot2:如何按填充变量的比例重新排列堆积的条形图

来自分类Dev

ggplot2:如何按填充变量的比例重新排列堆积的条形图

来自分类Dev

如何使用ggplot为两个不同的分组变量手动指定颜色填充?

来自分类Dev

在R中使用带ggplot的fct_relevel对变量重新排序

来自分类Dev

在R中使用带ggplot的fct_relevel对变量重新排序