我正在创建一个条形图,并在其中使用填充来区分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] 删除。
我来说两句