When doing a ggplot with geom_area
, the negative y-range contains some holes as in the example below.
I also got to code a very simple reproducible example, that illustrates this phenomenon:
require(ggplot2)
require(data.table)
data <- data.table(index = c(0, 1),
x1 = c(-1, -1.5),
x2 = c(-1, 0),
x3 = c(0, -1))
mdt <- melt(data, id.vars = "index")
print(data)
# index x1 x2 x3
# 1: 0 -1.0 -1 0
# 2: 1 -1.5 0 -1
# Negative range: Holes
ggplot(data=mdt, aes(x=index, y=value, fill=variable)) +
geom_area(position="stack")
# Positive range: No holes
ggplot(data=mdt, aes(x=index, y=abs(value), fill=variable)) +
geom_area(position="stack")
Negative Range:
Positive Range:
As you can see in the first plot a hole appears because x2 goes from -1 to zero and x3 from 0 to -1 at the same time. Interestingly, this weird behavior only seems to happen in the negative y-range while in the second plot there are no holes anymore.
Do you have any idea, why this happens in the negative y-range but not in the positive y-range? And any idea how to fix the problem for the negative y-range?
Thank you very much in advance!