简体   繁体   English

标准化内核密度图的宽度和图例比例

[英]Standardising Kernel Density Plot's Width & Legend Scale

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. 我有以下4个内核密度图,但希望图例比例以及图宽/高在所有4个图中都相同,以进行比较。

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? 有什么办法可以将其缩放到相同大小以进行比较?

在此处输入图片说明

You haven't provided sample data, so I am using mtcars . 您尚未提供示例数据,所以我正在使用mtcars This is a way with 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. 就是这种方法。 注释中提到的,诀窍是将每年可能存在的多个数据帧合并为一个数据帧,理想情况下将其合并为长格式,并在其中包含“ year”信息。

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 

在此处输入图片说明

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

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