I have the following dataframe in which the x-axis represents the phases of a project (1 to 5 in ascending order) and the y-axis represents the names of the projects.
df1 = data.frame(a = c("Project1", "Project2", "Project3","Project4"),
b = c(2, 4, 2, 5))
project 1 >>> phase 2
project 2 >>> phase 4
...
I want to plot a graph using ggplot that shows the x-axis with all the phases of the project according to the phases vector even if my dataframe does not have projects in phase 1 or 3, for example.
phases <- c("phase1", "phase2", "phase3", "phase4", "phase5")
The x-axis of the dataframe represents which phase the project is in at the moment. Therefore, project 1 is in phase 2, that is, it has passed through phase 1 and is now in phase 2. Project 2 is in phase 4 and has passed through phases 1, 2 and 3. I would like the graph to highlight all the project phases for each project and a line or bar showing the phases it has passed and the current phase.
To avoid any doubt, consider that there is a possible phase 6 that I would like to be represented in the graph above even if my dataframe does not have any projects in this phase.
You can use the scale_y_continuous()
to set the labels on the axis.
library(ggplot2)
# Sample data
df1 <- data.frame(
a = c("Project1", "Project2", "Project3", "Project4"),
b = c(2, 4, 2, 5)
)
N <- 6
ggplot(df1, aes(x = a, y = b)) +
geom_bar(stat = "identity") +
labs(x = "Project", y = "Phase") +
scale_y_continuous(breaks = 1:N, labels = paste("phase", seq_len(N))) +
expand_limits(y = N) +
coord_flip()