简体   繁体   English

清理R中的密度图

[英]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? 有没有办法让geom_density2d和/或stat_density2d以不同的方式插值以消除这些明显的不连续性?

在此输入图像描述

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 这里回答了这个问题: ggplot density2d中的非重叠多边形

Basically, you can extend the x,y limits to allow the polygons to be fully drawn. 基本上,您可以扩展x,y限制以允许完全绘制多边形。

Then, if that makes the plot too zoomed out, you set limits with coord_cartesian: 然后,如果这使得绘图也缩小了,则使用coord_cartesian设置限制:

r stat_contour incorrect fill with polygon r stat_contour填充多边形不正确

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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