tom91
tom91

Reputation: 685

stat_density2d tile geom showing grid when exporting as PDF

I'm having an issue when exporting stat_density2d plots.

ggplot(faithful, aes(eruptions, y = waiting, alpha = ..density..)) +
  stat_density2d(geom = 'tile', contour = F)

When exporting as a png it looks like so:

enter image description here

But when I export as a PDF a grid appears:

enter image description here

I'm assuming that this is because the boundaries of the tiles overlap and so have equivalent of a doubled alpha value.

How can I edit just the edges of the tiles to stop this from happening?

Secondary question:

As Tjebo mentioned geom = 'raster' would fix this problem. However, this raises a new issue that only one group gets plot.

df <- faithful
df$new <- as.factor(ifelse(df$eruptions>3.5, 1, 0))

ggplot(df, aes(eruptions, waiting, fill = new, alpha = ..density..)) +
  stat_density2d(geom = 'tile', contour = F) +
  scale_fill_manual(values = c('1' = 'blue', '0' = 'red'))

enter image description here

ggplot(df, aes(eruptions, waiting, fill = new, alpha = ..density..)) +
  stat_density2d(geom = 'raster', contour = F) +
  scale_fill_manual(values = c('1' = 'blue', '0' = 'red'))

enter image description here

help on this second issue would also be much appreciated!

Upvotes: 2

Views: 519

Answers (1)

tjebo
tjebo

Reputation: 23757

Now I decided to transform my comment into an answer instead. Hopefully it solves your problem.

There is an old, related google thread on this topic - It seems related to how the plots are vectorized in each pdf viewer.

A hack is suggested in this thread, but one solution might simply be to use geom = 'raster' instead.

library(ggplot2)
ggplot(faithful, aes(eruptions, y = waiting, alpha = ..density..)) +
  stat_density2d(geom = 'raster', contour = F)

Created on 2019-08-02 by the reprex package (v0.3.0)

If you have a look at the geom_raster documentation - geom_raster is recommended if you want to export to pdf!

The most common use for rectangles is to draw a surface. You always want to use geom_raster here because it's so much faster, and produces smaller output when saving to PDF

edit - second part of the question

Your tile plot can't be correct - you are creating cut-offs (your x value), so the fill should not overlap. This points to the core of the problem - the alpha=..density.. probably calculates the density based on the entire sample, and not by group. I think the only way to go is to pre-calculate the density (e.g., using density(). In your example, for demonstration purpose, we have this luckily precalculated, as faithfuld (this is likely not showing the results which you really want, as it is the density on the entire sample!!).

I'd furthermore recommend not to use numbers as your factor values - this is pretty confusing for you and R. Use characters instead. Also, ideally don't use df for a sample data frame, as this is a base R function;) Hope this helps

mydf <- faithfuld ## that is crucial!!! faithfuld contains a precalculated density column which is needed for the plot!!
mydf$new <- as.factor(ifelse(mydf$eruptions>3.5, 'large', 'small'))

ggplot(mydf, aes(eruptions, waiting)) +
  geom_raster(aes(fill = new, alpha = density), interpolate = FALSE)

Created on 2019-08-02 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions