问题
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