1

I would like to smooth up the edges of my graph here. Is there a way to get geom_density2d and/or stat_density2d to interpolate a bit differently to remove these apparent discontinuities?

enter image description here

I used this to create the included example:

  ggplot(data = PlotModel1, aes(x = Lon, y = Lat)) +
  geom_point(aes(color = Conc), shape = "") +
  stat_density2d(aes(fill = ..level..), n = 100, geom="polygon", contour = TRUE) +
  guides(fill = FALSE) +
  theme_bw()

I would like a smoother plot like this enter image description here

Thanks!

Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56

1 Answers1

0

This question is answered here: non-overlapping polygons in ggplot density2d

Basically, you can extend the x,y limits to allow the polygons to be fully drawn.

Then, if that makes the plot too zoomed out, you set limits with coord_cartesian:

r stat_contour incorrect fill with polygon

So, for your code, it will be something like:

  ggplot(data = PlotModel1, aes(x = Lon, y = Lat)) +
  geom_point(aes(color = Conc), shape = "") +
  stat_density2d(aes(fill = ..level..), n = 100, geom="polygon", contour = TRUE)+
  guides(fill = FALSE) +
  theme_bw()+
  xlim(-150, -50)+
  ylim(30, 45)+
  coord_cartesian(xlim=c(-110, -80),ylim=c(33, 41))
Grubbmeister
  • 857
  • 8
  • 25