0

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?

enter image description here

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Grace
  • 201
  • 2
  • 13
  • welcome. https://stackoverflow.com/help/how-to-ask Please provide minimal reproducible data https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – tjebo Jun 15 '18 at 12:13
  • add an image *into* the question and ideally do not link to another site (see my edit) – tjebo Jun 15 '18 at 12:13
  • I would probably use [tag:ggplot2] (also because I dont know so much baseR graphics). and define my axis limits.. Saying this - it must be surely possible to define the axis limits in baseR too. – tjebo Jun 15 '18 at 12:15
  • BUT why does your code give different results? I have the impression that you are having a copy paste error there maybe?? Because you are always using the same data in your plot calls. If those are different data sets - maybe merge them , put them into a long format, and use **facetting** – tjebo Jun 15 '18 at 12:17
  • 1
    @Tjebo, new users can't embed images. They just get uploaded to imgur. You can edit to embed. – Axeman Jun 15 '18 at 12:24

1 Answers1

0

You haven't provided sample data, so I am using 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.

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 

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94