Reputation: 129
I am creating a barchart and in it im using fill to make distinction between 5 different variables. Here's the code:
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"))
What I am trying to achieve is that the status variables specified in fill-option would be in the order that im trying to re-order it in the code line 5 (starting with data$status) but i only achieve to re-order how its presented in the Legend.
How could I specify the order of these variables in the graph itself?
Edit to include 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"))
Upvotes: 0
Views: 69
Reputation: 173793
I think it would be best to plot a summarised table based on grouped counts. That way the stacked values appear in the same order as the legend colours:
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"))
Upvotes: 1