Breaking the y-axis in a plot
Hilde wanted to make a x-y plot in which the y-axis runs from for instance 0 to 10, and then suddenly from 40 to 50, with a 'lightning-ray' drawn on the y-axis between the 10 and 40 labels. This is useful for two sets of data that are measured the same scale, but have a different range. Here's one way:
# generate some data
Trial = 1:15 # measurement occasion
x = 1:15/2 + rnorm(15,,.5) # 1st dataset (lower values)
y = 40 + 1:15/1.4 + rnorm(15,,.5) # 2nd dataset (higher values)
labx = pretty(x)
laby = pretty(y)
mx = min(laby) - max(labx) - max(labx[2]-labx[1],laby[2]-laby[1])
matplot( Trial, cbind(x, y-mx), type='b', yaxt='n', bty=']',
ylab="Amplitude", ylim=range(c(labx,laby-mx)),
main="Lighting: example of broken y-axis plot")
dpos = abs(par('tcl')*0.3)
pos = par('usr')[1] + dpos
axis(2, at=labx, pos=pos)
axis(2, at=laby-mx, yaxp=c(min(laby)-mx,max(laby)-mx,length(laby)),
labels=laby, pos=pos)
lightning = list(x=c(pos, pos, pos+dpos/1.5,pos-dpos/1.5,pos, pos),
y=c(max(labx),
(3*max(labx)+1*(min(laby)-mx))/4,
(max(labx)+min(laby)-mx)/2.005,
(max(labx)+min(laby)-mx)/1.995,
(1*max(labx)+3*(min(laby)-mx))/4, min(laby)-mx))
lines(lightning)
The idea is to shift the data in y down by the amount mx to just above the maximum of the measurements in x (leaving enough space for a pretty annotated axis), and to draw the y-axis in two separate parts. The lightning ray is drawn in the remaining gap between the two parts of the y-axis. The result is shown below. In this code the x are assumed to be on the lower end of the axis.
Trial = 1:15 # measurement occasion
x = 1:15/2 + rnorm(15,,.5) # 1st dataset (lower values)
y = 40 + 1:15/1.4 + rnorm(15,,.5) # 2nd dataset (higher values)
labx = pretty(x)
laby = pretty(y)
mx = min(laby) - max(labx) - max(labx[2]-labx[1],laby[2]-laby[1])
matplot( Trial, cbind(x, y-mx), type='b', yaxt='n', bty=']',
ylab="Amplitude", ylim=range(c(labx,laby-mx)),
main="Lighting: example of broken y-axis plot")
dpos = abs(par('tcl')*0.3)
pos = par('usr')[1] + dpos
axis(2, at=labx, pos=pos)
axis(2, at=laby-mx, yaxp=c(min(laby)-mx,max(laby)-mx,length(laby)),
labels=laby, pos=pos)
lightning = list(x=c(pos, pos, pos+dpos/1.5,pos-dpos/1.5,pos, pos),
y=c(max(labx),
(3*max(labx)+1*(min(laby)-mx))/4,
(max(labx)+min(laby)-mx)/2.005,
(max(labx)+min(laby)-mx)/1.995,
(1*max(labx)+3*(min(laby)-mx))/4, min(laby)-mx))
lines(lightning)
0 Comments:
Post a Comment
<< Home