Skip to content

geom_area with holes in the negative y-range #2802

@PabloRMira

Description

@PabloRMira

As I already described here

https://stackoverflow.com/questions/51656490/holes-with-geom-area-ggplot2-for-negative-y-range

there is an issue with geom_area. When using geom_area with negative values, some holes arise on the negative y-range. Interestingly, this weird behavior does not occur on the positive y-range. Here is a reproducible example, that illustrates what I mean

library(ggplot2)
library(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")

Created on 2018-08-04 by the reprex package (v0.2.0).

In the first plot the problem is that x2 is changing from -1 to 0 and x3 from 0 to -1 at the same time, hence producing a hole. But again, this only seems to occur on the negative y-range. As you can see in the second plot, the stacking behavior of geom_area is completely satisfactory with no holes between the areas. Therefore my supposition that this may be due to a bug.

Thank you in advance for you help!

EDIT: As pointed out by @smouksassi (many thanks for this!), a quick & dirty workaround would be to set the zeros to some very small negative value as here:

library(ggplot2)
library(data.table)

data <- data.table(index = c(0, 1),
                   x1 = c(-1, -1.5),
                   x2 = c(-1, -1e-36), # <- Changed!
                   x3 = c(-1e-36, -1)) # <- Changed!
mdt <- melt(data, id.vars = "index")

print(data)
#>    index   x1     x2     x3
#> 1:     0 -1.0 -1e+00 -1e-36
#> 2:     1 -1.5 -1e-36 -1e+00

# Negative range: No holes anymore
ggplot(data = mdt) +
  geom_area(aes(x = index, y = value, fill = variable), position = "stack")

Created on 2018-08-04 by the reprex package (v0.2.0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions