2017-04-18 88 views
1

我想在這裏平滑我的圖的邊緣。有沒有辦法讓geom_density2d和/或stat_density2d內插有點不同以消除這些明顯的不連續性?清理R中的密度圖

enter image description here

我用它來創建包含的例子:

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

我想這樣 ​​

感謝平滑的陰謀!

+1

可以請你包含一些數據,以便你的問題可以被複制 –

回答

0

這個問題是在這裏找到答案: non-overlapping polygons in ggplot density2d

基本上,你可以擴展X,Y的限制,允許多邊形被完全繪製。

然後,如果使情節太縮小,可以設置限制與coord_cartesian:

r stat_contour incorrect fill with polygon

因此,對於您的代碼,這將是這樣的:

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