I want to differentiate the area of the plot color based on the factor variable "Status". I use fill=Status, however, I see a break in the plot area. Any ideas/suggestions on how to avoid this and why is this happening?
df1 <- data.frame(Date=seq(as.Date("2016/03/01"), as.Date("2016/03/10"), "day"),
Storypoints=c(8,14,16,23,28,35,40,44,46,55),
Status=c(rep("Completed",7), rep("Open",3)))
ggplot(data=df1, mapping = aes(x = Date)) +
geom_area(aes(y=Storypoints, fill=Status))
You can use geom_bar with width=1 to have a continuous area plot that accurately represents your data.
ggplot(data=df1, mapping = aes(x = Date)) +
geom_bar(aes(y=Storypoints, fill=Status), stat="identity",width=1)
The reason is that there are no data points beween Mar 07 and Mar 08. And because the points on either side of the gap do not belong to the same group, ggplot does not connect them. Simply put, the red are ends at Mar 07, the blue one starts at Mar 08 and there is nothing in between.
If you imaging the same plot, but with the gap simply removed, this would mean that the x axis should be labelled both, Mar 07 and Mar 08, where the two curves touch.
There is no obvious solution to this because there is really no data that could be plotted in the gap. If you want to fill the gap, you have to make a decision and the modify your data accordingly.
A possible decision (but most likely not a very good one) would be to simply add a data point such that the read area fills the gap:
df1[11, "Date"] <- as.Date("2016-03-08")
df1[11, "Storypoints"] <- 44
df1[11, "Status"] <- "Completed"
ggplot(data=df1, mapping = aes(x = Date)) +
geom_area(aes(y=Storypoints, fill=Status), pos = "identity")
But the problem with this is quite clear: The data point that I added should not exist, so what the plot shows on Mar 08 is actually misleading.
I'm not sure there is a satisfactory solution to this. But maybe someone else has a better idea.
I think it is because ggplot is not able to determine to which status that area belongs, so which color to give it.
I introduced another status and that gave another gap between the filled areas.