标准化核密度图的宽度&图例比例 [英] Standardising Kernel Density Plot's Width & Legend Scale
本文介绍了标准化核密度图的宽度&图例比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 4 个核密度图,但希望图例比例以及图的宽度/高度在所有 4 个中都相同以进行比较.
我的代码是:
kde_pipit_2014_bw <- 密度(Pipit_ppp_2016, sigma=4.18, edge=TRUE, kernel="gaussian")kde_pipit_2015_bw <-密度(Pipit_ppp_2016,sigma=4.18,edge=TRUE,kernel="gaussian")kde_pipit_2016_bw <-密度(Pipit_ppp_2016,sigma=4.18,edge=TRUE,kernel="gaussian")kde_pipit_2016_bw <-密度(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))情节(kde_pipit_2014_bw)情节(kde_pipit_2015_bw)情节(kde_pipit_2016_bw)情节(kde_pipit_2017_bw)
下面是地块的图像.有什么办法可以将其缩放到相同的大小进行比较吗?
解决方案
您尚未提供示例数据,因此我使用了 mtcars
.这是一种带有
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?
解决方案
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
这篇关于标准化核密度图的宽度&图例比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文