Reputation: 348
I am trying to produce a GANTT chart in ggplot2. I am not sure what is going on with the x axis but rather than giving me nice evenly spaced intervals, it is giving me a tick mark for every date within the df and mixing up the timeline (i.e. not plotting chronologically, it plots numerically with all the 1s first). In the df it is ordered from the earliest date to the latest date so I am not sure why this is happening. How can I get it to plot just the first of every month for example 01/21 02/21 etc will suffice.
Thanks.
library(ggplot2)
library(readr)
setwd("~/Google Drive/R")
activities<- read_csv("GANTT2.csv")
activities$activity<- factor(activities$activity, levels = activities$activity)
plot_gantt <- qplot(ymin = start,
ymax = end,
x = activity,
colour = category,
geom = "linerange",
data = activities,
size = I(5)) + coord_flip() +
theme_bw(16) +
theme(panel.grid = element_blank()) +
xlab("") +
ylab("") +
ggtitle("Schedule of work for thesis completion")+
theme(axis.text.x = element_text(angle = 90))+
labs(y = "Month", x = "Activities")+
scale_colour_manual(values=c("#9590FF","#01BFC4","#00B6EB","#7CAE00"))
plot_gantt
structure(list(Chapter = c(2, 6, 6, 6, 6, 6), activity = c("Literature Review (Continuous)",
"Obtain final chronology data - run models", "Reconstructions",
"Final MAT model evaluations", "Sea-level fingerprinting", "Chapter 6 writing"
), category = c("Writing", "Data Analysis", "Data Analysis",
"Data Analysis", "Data Analysis", "Writing"), start = c("01/01/2021",
"15/02/2021", "15/03/2021", "20/05/2021", "01/06/2021", "01/07/2021"
), end = c("01/09/2021", "15/04/2021", "20/05/2021", "21/05/2021",
"30/06/2021", "30/07/2021")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
Upvotes: 0
Views: 122
Reputation: 76615
Here is a solution.
It does not use coord_flip
, all that is needed is to map y = activity
. And the x axis labels angle is 45 degrees, not 90.
library(dplyr)
library(ggplot2)
gantt_data %>%
mutate(start = as.Date(start, "%d/%m/%Y"),
end = as.Date(end, "%d/%m/%Y")) %>%
ggplot(aes(y = activity, colour = category)) +
geom_linerange(aes(xmin = start, xmax = end), size = 5) +
scale_colour_manual(values=c("#9590FF","#01BFC4","#00B6EB","#7CAE00")) +
scale_x_date(date_breaks = "1 months", date_labels = "%b %Y") +
labs(y = "Month", x = "Activities") +
ggtitle("Schedule of work for thesis completion") +
theme_bw() +
theme(panel.grid = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1))
Upvotes: 3