Standardising Kernel Density Plot's Width & Legend Scale

血红的双手。 提交于 2019-12-11 16:44:10

问题


I have the following 4 kernel density plots, but would like the legend scale as well as the plot width/height to be the same across all 4 for comparison.

My codes are:

kde_pipit_2014_bw <- density(Pipit_ppp_2016, sigma=4.18, edge=TRUE, kernel="gaussian") 
kde_pipit_2015_bw <- density(Pipit_ppp_2016, sigma=4.18, edge=TRUE, kernel="gaussian") 
kde_pipit_2016_bw <- density(Pipit_ppp_2016, sigma=4.18, edge=TRUE, kernel="gaussian") 
kde_pipit_2016_bw <- density(Pipit_ppp_2016, sigma=4.18, edge=TRUE, kernel="gaussian")

par(mfrow=c(2,2),cex=0.7, mai=c(0.1,0.1,0.2,0.2))
plot(kde_pipit_2014_bw) 
plot(kde_pipit_2015_bw) 
plot(kde_pipit_2016_bw) 
plot(kde_pipit_2017_bw)

Below is an image of the plots. Is there any way I can scale it to the same size for comparison?


回答1:


You haven't provided sample data, so I am using mtcars. This is a way with ggplot2 The trick is, as mentioned in the comment, to merge your possibly existing several dataframes from each year, into one data frame, into ideally a long format with a column containing the 'year' information.

library(ggplot2)

ggplot(mtcars, aes(x = mpg, y = wt)) + 

  stat_density2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
  scale_fill_gradient(low="green",high="red")+
  facet_grid(~cyl)  ## you can replace this with your year

#the colors are just examples, I did not try to reproduce exactly your color scale 



来源:https://stackoverflow.com/questions/50875226/standardising-kernel-density-plots-width-legend-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!