Search code examples
rplotlayout

image plots with color key legends in layout


I want to make plots arranged like this, but with color key legends.

layout(matrix(c(1,1,2,3,2,3),2,3))
y <- matrix(1:200,10,20)
par(mai=c(0.9,0.9,0.1,0.1))
plot(1,1)
par(mai=c(0.6,0.6,0.1,0.8))
image(y)
image(y)

The problem with this code is that the image plots do not have a color key legend. image.plot can make the legend, but image.plot does not work with layout. This question was asked here but has not been answered.

layout(matrix(c(1,1,2,3,2,3),2,3))
y <- matrix(1:200,10,20)
par(mai=c(0.9,0.9,0.1,0.1))
plot(1,1)
par(mai=c(0.6,0.6,0.1,0.8))
library(fields)    
image.plot(y)
image.plot(y)

I tried to make a legend with addLegend,

layout(matrix(c(1,1,2,3,2,3),2,3))
y <- matrix(1:200,10,20)
par(mai=c(0.9,0.9,0.1,0.1))
plot(1,1)
par(mai=c(0.6,0.6,0.1,0.8))
library(fields)
image(y,col=hcl.colors(12, "RdYlBu", rev = TRUE))
addLegend(setupLegend(), col=hcl.colors(12, "RdYlBu", rev = TRUE), zlim = c(0,200))
image(y,col=hcl.colors(12, "RdYlBu", rev = TRUE))
addLegend(setupLegend(), col=hcl.colors(12, "RdYlBu", rev = TRUE), zlim = c(0,200))

but it still has problems. I like to make the plots without using ggplot.


Solution

  • color.legend worked fine.

    layout(matrix(c(1,1,2,3,2,3),2,3))
    y <- matrix(1:200,10,20)
    par(mai=c(0.9,0.9,0.1,0.1))
    plot(1,1)
    par(mai=c(0.6,0.6,0.1,0.6))
    library(fields)
    library(plotrix)
    
    image(y,col=hcl.colors(12, "RdYlBu", rev = TRUE))
    color.legend(1.1,0,1.15,1,c(1,100,200),hcl.colors(12, "RdYlBu", rev = TRUE),gradient="y",align="rb")
    image(y,col=hcl.colors(12, "RdYlBu", rev = TRUE))
    color.legend(1.1,0,1.15,1,c(1,100,200),hcl.colors(12, "RdYlBu", rev = TRUE),gradient="y",align="rb")