x<- 1:10
y<- 1:15
z<- outer( x,y,"+")
imagePlot(x,y,z)
# to view z in the usual matrix indexing:
plotMatrix( z)
# or
obj<- list( x=x,y=y,z=z)
imagePlot(obj, legend.lab="Sverdrups")
# to test add some points on diagonal using standard plot function
# (with some clipping beyond 10 anticipated)
points( 5:12, 5:12, pch="X", cex=3)
# in general imagePlot will reset the plot window so you
# can add any feature that normally works in base R
# e.g. lines, text, contour, boxplots, ....
#
################################################################
# the next sequence of examples explain how to quickly
# adapt this basic plot to include more features
###############################################################
#
# adding explicit breaks and distinct colors for intervals of z
brk<- quantile(c(z), c(0, .1,.25, .5, .75, .9, 1.0))
imagePlot(x,y,z, breaks=brk, col=topo.colors(6))
# last bins are depicted as end triangles on the color bar.
imagePlot(x,y,z, breaks=brk, col=topo.colors(6),
lowerTriangle=TRUE, upperTriangle=TRUE)
# NOTE: the image function does not use -Inf and Inf for the breaks argument
# and so if one wants the triangles at ends to indicate values beyond the
# range of the color bar one has to create the breaks "by hand" when the
# two outer bins will define the values for the triangles.
# annotate legend strip with the break point values and add a label
imagePlot(x,y,z, breaks=brk, col=rainbow(6),
lab.breaks=names(brk))
#
# compare to default color scale and special labels on color bar
zp <-quantile(c(z), c( .05, .1,.25, .5,.75, .9,.95))
imagePlot(x,y,z,
axis.args=list( at=zp, labels=names(zp) ) )
# a log scaling for the colors
ticks<- c( 1, 2,4,8,16,32)
imagePlot(x,y,log(z), axis.args=list( at=log(ticks), labels=ticks))
# see help(designer.colors) to generate a color scale that adapts to
# quantiles of z.
# Add some color scales together here is an example of5 blues to white to 5 reds
# with white being a specific size.
colorTable<- designer.colors(11, c( "blue","white", "red") )
# breaks with a gap of 10 to 17 assigned the white color
brks<- c(seq( 1, 10,,6), seq( 17, 25,,6))
imagePlot( x,y,z,breaks=brks, col=colorTable)
#
#fat (5 characters wide) and short (50% of figure) color bar on the bottom
imagePlot( x,y,z,legend.width=5, legend.shrink=.5, horizontal=TRUE)
# adding a label and all kinds of additional arguments to color bar
# use side=4 for vertical legend and side= 1 for horizontal legend
# to be parallel to axes. See help(mtext).
imagePlot(x,y,z,
legend.args=list( text="unknown units",
col="magenta", cex=1.5, side=4, line=2))
# and finally add some grid lines
dx <- x[2] - x[1]
dy <- y[2] - y[1]
xtemp<- seq( min( x)- dx/2, max(x)+ dx/2,
length.out = length(x) +1)
ytemp<- seq( min( y)- dy/2, max(y)+ dy/2,
length.out = length(y) +1)
xline( xtemp, col="grey", lwd=2)
yline( ytemp, col="grey", lwd=2)
###############################################################
#### example using an irregular quadrilateral grid
###############################################################
data( RCMexample)
imagePlot( RCMexample$x, RCMexample$y, RCMexample$z[,,1])
ind<- 50:75 # make a smaller image to show bordering lines
imagePlot( RCMexample$x[ind,ind], RCMexample$y[ind,ind], RCMexample$z[ind,ind,1],
border="grey50", lwd=2)
###############################################################
#### multiple images with a common legend
###############################################################
set.panel()
# Here is quick but quirky way to add a common legend to several plots.
# The idea is leave some room in the margin and then at the end
# overplot the legend in this margin
par(oma=c( 0,0,0,4)) # margin of 4 spaces width at right hand side
set.panel( 2,2) # 2X2 matrix of plots
# now draw all your plots using usual image command
for ( k in 1:4){
data<- matrix( rnorm(150), 10,15)
image( data, zlim=c(-4,4), col=tim.colors())
# and just for fun add a contour plot
contour( data, add=TRUE)
}
par(oma=c( 0,0,0,1))# reset margin to be much smaller.
imagePlot( legend.only=TRUE, zlim=c(-4,4))
# imagePlot tricked into plotting in margin of old setting
set.panel() # reset plotting device
#
# Here is a more learned strategy to add a common legend to a panel of
# plots. Consult the split.screen help file for more explanations.
# For this example we draw two
# images top and bottom and add a single legend color bar on the right side
# first divide screen into the figure region (left) and legend region (right)
split.screen( rbind(c(0, .8,0,1), c(.8,1,0,1)))
# now subdivide up the figure region into two parts
split.screen(c(2,1), screen=1)-> ind
zr<- range( 2,35)
# first image
screen( ind[1])
image( x,y,z, col=tim.colors(), zlim=zr)
# second image
screen( ind[2])
image( x,y,z+10, col=tim.colors(), zlim =zr)
# move to skinny region on right and draw the legend strip
screen( 2)
imagePlot( zlim=zr,legend.only=TRUE, smallplot=c(.1,.2, .3,.7),
col=tim.colors())
close.screen( all=TRUE)
# you can always add a legend arbitrarily to any plot;
# note that here the plot by default is too big for the vertical strip
# or horizontal strip.
# just increase the margin size to accomodate
# this fix will be graphics device dependent so I recommend
# using the pdf function and saving to a pdf file to get consistent results
plot( 1:10, 1:10)
imagePlot( zlim=c(0,25), legend.only=TRUE)
imagePlot( zlim=c(0,25), legend.only=TRUE, horizontal =TRUE)
par( mar=c( 10,5,2,2))
plot( 1:10, 1:10)
imagePlot( zlim=c(0,25), legend.only=TRUE, horizontal = TRUE)
if (FALSE) {
# drawing and saving the plot to a pdf file.
pdf("test.pdf", width=6, height=4)
par( mar=c( 10,5,2,2) ) # you may have to adjust these.
plot( 1:10, 1:10)
imagePlot( zlim=c(0,25), legend.only=TRUE, horizontal = TRUE)
dev.off()
}
# combining the usual image function and adding a legend
# first change margin for some more room
if (FALSE) {
par( mar=c(10,5,5,5))
image( x,y,z, col=topo.colors(64))
imagePlot( zlim=c(0,25), nlevel=64,legend.only=TRUE,
horizontal=TRUE,
col=topo.colors(64))
}
#
# adding a legend by automatically making room.
# and coloring points
info<- setupLegend()
colTab<- rainbow(10)
plot( 201:210, 201:210, col=colTab, pch=16)
addLegend(info, col=colTab, zlim = c(201,210))
#
#######################################################
##### Comparison to ggplot
#######################################################
# The following example was created as way avoid doing more important
# things
# Note how close base graphics can get to reproducing the ggplot style.
# and how a first cut imagePlot(x,y,z, col = viridis(128) ) is probably
# acceptable in most cases for fast EDA
# Surprisingly the hardest feature is to add the grey gird lines behind
# the image. I (DWN) don't know how to do it!
if (FALSE) {
library( viridis)
library( ggplot2)
x<- 1:20
y<- 1:24
z<- outer( x, y, "+")
# ggplot version
mesh<- expand.grid( x= x, y=y)
mesh$z <- c(z)
ggplot( data=mesh, aes( x=x, y=y, fill=z)) +
geom_raster(interpolate= FALSE) +
scale_fill_continuous(type = "viridis") +
theme_bw()
# reset to a single plot
set.panel()
# inflate range to give a margin around image
xr<- range(x) + c(-.08, .08)* diff( range(x))
yr<- range(y) + c(-.08, .08)* diff( range(y))
# changing these graphics parameters tends to push
# text closer to the axes.
par( mgp=c(1.5,.5,0), mar=c(2.5,2.5,.5,1), cex=.8)
imagePlot(x,y,z,
col = viridis(128),
legend.shrink = .27,
xlim = xr,
ylim = yr,
legend.width = 1.5,
legend.mar = 3,
legend.args = list( text = "z",
cex = .8,
side = 3,
line = .5)
)
}
Run the code above in your browser using DataLab