Skip to content
Prev 5354 / 7420 Next

Contour colour gradations and overplotting problems

One option, if you are satisfied with the results of plotting, is to draw
your own scale bar.

Drawing a scale bar from scratch requires only minimal code.  The approach
below places the scale bar relative to percentages of the width and height
of the plot area, so is readily adapted to plots with different x and y
limits.

#A plot
plot(log(1:100), 1:100, pch=21, bg=topo.colors(100))

#Add to plot on the current device
par(new=TRUE)
#Set up coordinate system.  This one
#is easy to visualize, e.g., x=0.2 is
#approximately 20% of the distance
#from left to right, y=0.20 is approx.
#20% of the distance from bottom to
#top.
plot.window(xlim=c(0,1), ylim=c(0,1))

#Larger value gives smoother gradation
#of colors on scale
n <- 100

#On the x axis, the scale will run from
#0.05 t0 0.055, left to right.  Width is
#approximately 5% of width of plotting
#region.
x1 <- rep(0.05,100)
x2 <- x1+0.05

#On the y axis, the scale will run from
#20% of the way up to 80% of the way up.
y1 <- seq(from=0.2, to=0.8, length.out=100)
y2 <- y1+0.01

#Draw colored rectangles and frame the entire
#collection.  I used topo.colors() for simplicity,
#but any vector of 100 color specifications would
#work. For fine control, see functions for color ramps.
rect(x1,y1,x2,y2, border=NA, col=topo.colors(100))
rect(x1[1],y1[1],x2[n],y2[n])

#Adding text
n.labels <- 4
x <- unique(x2)
y <- seq(min(y1), max(y2), length.out=n.labels)

text(x, y, c("Label 1", "Label 2", "Label 3", "Label 4"), adj=-0.2)


On Thu, May 5, 2016 at 5:07 AM, Margaret Donald <merricks.merricks at gmail.com