7

I am trying to construct a graph consisting of 2-3 filled.contour plots next to each other. The color scale is the same for all plots, and I would like only one z value key plot. I am having difficulties to do this with par(mfrow=c(1,3))

Example code:

x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
filled.contour(x,y,z)
filled.contour(x,y,z,color.palette=rainbow)
z2 <- z
z2[5,5] <- Inf
filled.contour(x,y,z2,col=rainbow(200),nlevels=200)

Is it possible to stack 2-3 of these plots next to each other with only one z value color key? I can do this in GIMP but I was wondering if it is natively in R possible.

ECII
  • 10,297
  • 18
  • 80
  • 121

2 Answers2

8

No I do not think this is possible in filled.contour.

Although extensions have been written for you already. To be found here, here and here and a legend code here. [If you are using the filled.contour3 function referred to on those sites, and using a more recent version, then you need to use the upgrade fix referred to in this SO post]. Using those codes I produced:

enter image description here

Obromios
  • 15,408
  • 15
  • 72
  • 127
user1317221_G
  • 15,087
  • 3
  • 52
  • 78
  • 3
    It's a shame that `filled.contour` has a personality of it's own. I find it very difficult to manipulate `filled.contour` plots. Thanks for pointing out the modified version and to Carey McGilliard and Bridget Ferris for putting their code out there http://wiki.cbr.washington.edu/qerm/index.php/R/Contour_Plots – ECII Feb 08 '13 at 08:40
0

It can be solved with cowplot. The clue is the cowplot::draw_plot function which successfully represent the plot (cowplot::draw_grob(~filled.contour(x,y,z))). Later the grob representations can be binded. The possible problem is that each plot will have own legend.

x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
z2 <- z
z2[5,5] <- Inf

cowplot::plot_grid(~filled.contour(x,y,z), ~filled.contour(x,y,z,color.palette=rainbow), ~filled.contour(x,y,z2,col=rainbow(200),nlevels=200))

Created on 2023-02-14 with reprex v2.0.2

polkas
  • 3,797
  • 1
  • 12
  • 25