Home > OS >  Common color bar for image plots
Common color bar for image plots

Time:11-05

I have to plot a figure (image plot; z ~ x y) with four subplots and common color bar in the bottom. The plots should be arranged with 2 rows and 2 columns. I tried the code below. What changes do I need to make to save these plots into single figure having common color bar for all the subplots?

library(akima)
library(sinkr)
#----Generate random xyz data----
x = seq(0.1,0.8,0.02)
y = seq(0.1,5,0.1)
z1 <- runif(length(x)*length(y),0,600)  # Sample 1
z1 <- matrix(unlist(z1), nrow = length(x), ncol = length(y))
z2 <- runif(length(x)*length(y),0,600)  # Sample 2
z2 <- matrix(unlist(z2), nrow = length(x), ncol = length(y))
z3 <- runif(length(x)*length(y),0,600)  # Sample 3
z3 <- matrix(unlist(z3), nrow = length(x), ncol = length(y))
z4 <- runif(length(x)*length(y),0,600)  # Sample 4
z4 <- matrix(unlist(z4), nrow = length(x), ncol = length(y))
#----Image Plot----
png(
  filename="~/plot.PNG",
  width=12,
  height=8,
  units="in",
  res=600)
par(family = "serif")
par(mfrow=c(2,2)) # Arrange plots in 2 rows and 2 columns

# Basic settings
ncolors <- 64
colors <- jetPal(ncolors)
zlim <- c(0,600)
breaks <- c(seq(zlim[1], zlim[2], length.out = ncolors), 1e6)
layout(matrix(1:2,1,2), widths = c(4,1), heights = 4) # To plot colorbar besides the main plot 
#---Subplot 1----
image(x, y, z1, col = colors, breaks = breaks, zlim = zlim,cex.lab=1.5,cex.axis=1.5,xlab="",ylab="",ylim=c(0.1,5.0),xlim=c(0.1,0.8))
title(ylab=expression(y),line=2.0,cex.lab=1.5, family='serif')
title(xlab=expression(x),line=2.5,cex.lab=1.5, family='serif')
title(main="(a)",adj=0, cex.main=1.5)
imageScale(z = z1, ylim = zlim, zlim = zlim, col = colors, breaks = breaks, axis.pos = 2)
text(x=-1.8,y=500,labels = expression(g[sc]~(mmol~m^-2~s^-1)),xpd=NA,srt=90,cex=1.5,family='serif') 

#---Subplot 2----
image(x, y, z2, col = colors, breaks = breaks, zlim =zlim,cex.lab=1.5,cex.axis=1.5,xlab="",ylab="",ylim=c(0.1,5.0),xlim=c(0.1,0.8))
title(ylab=expression(y),line=2.0,cex.lab=1.5, family='serif')
title(xlab=expression(x),line=2.5,cex.lab=1.5, family='serif')
title(main="(b)",adj=0, cex.main=1.5)
imageScale(z = z2, ylim = zlim, zlim = zlim, col = colors, breaks = breaks, 
       axis.pos = 2)
text(x=-1.8,y=500,labels = expression(g[sc]~(mmol~m^-2~s^-1)),xpd=NA,srt=90,cex=1.5,family='serif') 
#---Subplot 3----
image(x, y, z3, col = colors, breaks = breaks, zlim =zlim,cex.lab=1.5,cex.axis=1.5,xlab="",ylab="",ylim=c(0.1,5.0),xlim=c(0.1,0.8))
title(ylab=expression(y),line=2.0,cex.lab=1.5, family='serif')
title(xlab=expression(x),line=2.5,cex.lab=1.5, family='serif')
title(main="(c)",adj=0, cex.main=1.5)
imageScale(z = z3, ylim = zlim, zlim = zlim, col = colors, breaks = breaks, 
       axis.pos = 2)
text(x=-1.8,y=500,labels = expression(g[sc]~(mmol~m^-2~s^-1)),xpd=NA,srt=90,cex=1.5,family='serif')
#---Subplot 4----
image(x, y, z4, col = colors, breaks = breaks, zlim = zlim,cex.lab=1.5,cex.axis=1.5,xlab="",ylab="",ylim=c(0.1,5.0),xlim=c(0.1,0.8))
title(ylab=expression(y),line=2.0,cex.lab=1.5, family='serif')
title(xlab=expression(x),line=2.5,cex.lab=1.5, family='serif')
title(main="(d)",adj=0, cex.main=1.5)
imageScale(z = z4, ylim = zlim, zlim = zlim, col = colors, breaks = breaks, 
       axis.pos = 2)
text(x=-1.8,y=500,labels = expression(g[sc]~(mmol~m^-2~s^-1)),xpd=NA,srt=90,cex=1.5,family='serif') 

dev.off()

CodePudding user response:

Actually just slightly modify the layout matrix and the height and width. The numbers correspond to the plots, the imageScale is also a new plot. If you want one plot to spread across multiple cells, just repeat the number. There's a layout.show() function that helps you, use layout.show(5) to check how you layout will look like with your 5 plots. Setting mfrow= in par is redundant and only disturbs the layout.

library('akima')
library('sinkr')

#----Generate random xyz data----
x <- seq(0.1, 0.8, 0.02)
y <- seq(0.1, 5, 0.1)
z1 <- runif(length(x)*length(y), 0, 600) # Sample 1
z1 <- matrix(unlist(z1), nrow=length(x), ncol=length(y))
z2 <- runif(length(x)*length(y), 0, 600) # Sample 2
z2 <- matrix(unlist(z2), nrow=length(x), ncol=length(y))
z3 <- runif(length(x)*length(y), 0, 600) # Sample 3
z3 <- matrix(unlist(z3), nrow=length(x), ncol=length(y))
z4 <- runif(length(x)*length(y), 0, 600) # Sample 4
z4 <- matrix(unlist(z4), nrow=length(x), ncol=length(y))

#----Image Plot----
png(
 filename="~/plot.PNG",
 width=8,
 height=12,
 units="in",
 res=600)
par(family="serif")
# par(mfrow=c(2, 2)) # Arrange plots in 2 rows and 2 columns

# Basic settings
ncolors <- 64
colors <- jetPal(ncolors)
zlim <- c(0, 600)
breaks <- c(seq(zlim[1], zlim[2], length.out=ncolors), 1e6)
layout(matrix(c(1:4, rep(5, 4)), 4, 2)
    , widths=c(4.25, .75), heights=rep(4, 4)
    ) # To plot colorbar besides the main plot
# layout.show(5)

#---Subplot 1-------------------------------------------------------------------
image(x, y, z1, col=colors, breaks=breaks, zlim=zlim, cex.lab=1.5, cex.axis=1.5,
      xlab="", ylab="", ylim=c(0.1, 5.0), xlim=c(0.1, 0.8))
title(ylab=expression(y), line=2.0, cex.lab=1.5, family='serif')
title(xlab=expression(x), line=2.5, cex.lab=1.5, family='serif')
title(main="(a)", adj=0, cex.main=1.5)

#---Subplot 2-------------------------------------------------------------------
image(x, y, z2, col=colors, breaks=breaks, zlim =zlim, cex.lab=1.5, cex.axis=1.5, 
      xlab="", ylab="", ylim=c(0.1, 5.0), xlim=c(0.1, 0.8))
title(ylab=expression(y), line=2.0, cex.lab=1.5, family='serif')
title(xlab=expression(x), line=2.5, cex.lab=1.5, family='serif')
title(main="(b)", adj=0, cex.main=1.5)

#---Subplot 3-------------------------------------------------------------------
image(x, y, z3, col=colors, breaks=breaks, zlim =zlim, cex.lab=1.5, cex.axis=1.5,
      xlab="", ylab="", ylim=c(0.1, 5.0), xlim=c(0.1, 0.8))
title(ylab=expression(y), line=2.0, cex.lab=1.5, family='serif')
title(xlab=expression(x), line=2.5, cex.lab=1.5, family='serif')
title(main="(c)", adj=0, cex.main=1.5)

#---Subplot 4-------------------------------------------------------------------
image(x, y, z4, col=colors, breaks=breaks, zlim=zlim, cex.lab=1.5, cex.axis=1.5, 
      xlab="", ylab="", ylim=c(0.1, 5.0), xlim=c(0.1, 0.8))
title(ylab=expression(y), line=2.0, cex.lab=1.5, family='serif')
title(xlab=expression(x), line=2.5, cex.lab=1.5, family='serif')
title(main="(d)", adj=0, cex.main=1.5)

#---Colorbar--------------------------------------------------------------------
imageScale(z=z1, ylim=zlim, zlim=zlim, col=colors, breaks=breaks, axis.pos=2)
text(x=-.875, y=300, labels=expression(g[sc]~(mmol~m^-2~s^-1)), xpd=NA, srt=90,
     cex=1.5, family='serif') 

dev.off()

enter image description here

  • Related