简体   繁体   中英

Cleaning up density plots in R

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?

在此输入图像描述

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 在此输入图像描述

Thanks!

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM