# NOT RUN {
###### Artificial example to show why addScales() might be useful and
###### how it works
##
###### Create a data set whose panels have different
###### centers and scales for y
x <- rep(0:10, 4)
scaling <- rep(c(1, 2, 5, 10), e = 11)
y <- sin(pi*x/10)*scaling + scaling ## add some structure
f <- factor(rep(LETTERS[1:4], e = 11))
## Now add noise proportional to data mean (= constant CV)
set.seed(91834)
y <- y + rnorm(44,sd = scaling/3)
## Plot this with the default "same" scaling and a loess curve
xyplot(y ~ x| f, layout = c(4,1), col = "darkblue",
scales = list(alternating = 1, tck = c(1,0)),
panel = function(...){
panel.points(...)
panel.loess(..., span = .6, col.line = "blue")
})
##
## Because of the different scaling, it's somewhat difficult to
## see the common panel data behavior. With relation = "free", it
## becomes clearer:
##
trellis.par.set(plot.symbol = list(col = "darkblue"),
plot.line = list(col = "darkblue"))
xyplot(y ~ x| f, layout = c(4,1),
scales = list(alternating = 1, tck = c(1,0),
y = list(relation = "free")),
panel = function(...){
panel.points(...)
panel.loess(..., span = .6, col.line = "blue")
})
##
## Unfortunately, the y-scales take up a lot of space and are
## difficult to read. With more panels, they would completely
## mess things up. To avoid this, don't draw them and use addScales
## to layer visual scaling onto the panels.
##
freeplot <- xyplot(y ~ x| f, layout = c(4,1),
scales = list(alternating = 1, tck = c(1,0),
y = list(relation = "free", draw = FALSE)),
panel = function(...){
panel.points(...)
panel.loess(..., span = .6, col.line = "blue")
})
addScales(freeplot) ## using defaults
##
## The labeled midline allows location comparison among the panels.
## The fixed distance from the dashed scale lines to the midline are given
## by the legend at top. This allows scaling among the panels to be
## compared, because the more y varies within a panel, the closer together
## these fixed scale lines become.
##
## NOTE:
## The addScales object inherits from class "trellis", so can be
## saved and plotted in the same way as 'freeplot' was. That is, the
## following also works:
##
enhanced <- addScales(freeplot)
enhanced
## Further panel options, which we use the update() method to change,
## allow for color coded scale regions and midlines:
##
#### Warning: Nothing may display if your graphics device does not support
## alpha transparency
update(enhanced, scaleType = "region", colCode = "r")
##
## cleanup
rm(scaling, x, y, f, freeplot, enhanced)
##
######## Some real examples #############
############################################
## Historical daily temperatures for Chicago, New York, and San Francisco.
data(CHITemps, NYCTemps, SFTemps)
preprocess.temps <- function(d){
meanTemp <- with(d, (TMAX + TMIN)/2)
Month <- months(as.Date(d$DATE))
z<- aggregate(meanTemp, list(
Month = factor(Month, levels = unique(Month)),
Year = as.numeric(substring(d$DATE,1,4))
), FUN = mean)
names(z)[3] <- "meanTemp"
z
}
## Create a list containing the preprocessed data for all 3 cities
plotdat <- lapply(
list(CHI = CHITemps, NYC = NYCTemps, SF = SFTemps),
preprocess.temps)
## Consider NYC. Because of monthly temperature variation, monthly temperature
## histories are mostly whitespace with the default relation = "same".
## Note also the use of the prepanel.trim function with defaults to remove
## extreme y values.
##
## Consider New York City
nyctemps <-
xyplot(meanTemp ~ Year|Month, type = "l", layout = c(3,4),
data = plotdat[[2]],
as.table = TRUE,
between = list(x=1, y=0),
## reduce strip size
par.strip.text = list(lines = .8, cex = .7),
## remove blank space for top axis
par.settings = list(layout.heights = list(axis.top = 0)),
prepanel = prepanel.trim, ## to remove possible extreme values
panel = function(...){
panel.grid(v = -1, h = 0, col = "gray70")
panel.xyplot(...)
panel.loess(..., span = .5, col = "darkred",
lwd = 1.5)
},
scales = list(axs = "i", alternating = 1, tck = c(1,0)),
xlab = "Year",
ylab = "Average Temperature (\u00B0F)",
main = "Mean Monthly Historical Temperatures in NYC"
)
nyctemps
## Now try it with y-scale = "free' and addScales
##
nyctemps <- update(nyctemps,
scales = list(axs = "i", alternating = 1,
tck = c(1,0),y = list(relation = "free", draw = FALSE)))
addScales(nyctemps)
## The historical temperature trend as the city
## built up and modernized (more concrete and asphalt,people,
## heat sources, etc.) is clearer and quantified by the
## legend and scale lines; and the scale lines also show
## that winter temperatures are clearly more variable than summer.
## This was almost undetectable in the previous plot.
## The same plot using region shading instead of scale lines.
## Warning: May not display if your graphics device does not support
## alpha transparency
addScales(nyctemps, scaleType = "region")
## ... and using color coding for midlines and regions to better visually
## distinguish their values...
##
addScales(nyctemps, scaleType = "region", colCode = "r")
## You can repeat the exercise with the other two cities if you like.
## cleanup
rm(nyctemps, preprocess.temps, plotdat)
####### Historical Crime Data #####
data(USAcrime)
## We explore the relationship beween property and violent crime over time.
## Point transparency via the 'alpha' setting is used to code year
## and the violent vs. property crime relationship is trellised by state
## for a selection of states.
##
## First with scales = "same", the default..
state.smpl <- c("CA","TX","GA","CO","VA","FL","NY","OH","MO","UT","MA","TN")
wh <- USAcrime$State %in% state.smpl
pcols <- hcl.colors(55, rev = TRUE)
crm <-xyplot(allViolent ~ allProperty|State, data = USAcrime[wh,],
subscripts = TRUE, as.table = TRUE,
layout = c(4,3), type = c("p", "g"),
cex= .75, pch = 19,
col = pcols[USAcrime[wh,'Year'] -1959],
par.strip.text = list(lines = .8, cex = .7),
between = list(x = 1),
scales = list(axs="i",alternating =1, tck = c(1,0)),
xlab = "Property Crime Rate (incidents/100,000 population)",
ylab = "Violent Crime Rate (incidents/100,000 population)",
main = paste0("Violent vs. Property Crime Rates from 1960-2014 For 12 States"),
sub = "Point Darkness Codes Years: Darker = Later Years",
panel = function(subscripts,col,...)
panel.xyplot(col = col[subscripts],...)
)
crm
## remove the grid and update with
## "free" scales and no axes for both x and y
crm2 <- update(crm, type = "p",
scales = list(axs="i", relation = "free", draw = FALSE))
## Add scales for both x and y and color code midlines
addScales(crm2, scaleline = TRUE, colCode = "m")
## Some features to note:
## 1. As one might expect, violent and property crime rates are
## correlated.
##
## 2. Crime rates first increased, peaked, and then decreased over time.
##
## 3. For most states there appears to be a kind of 'hysteresis':
## the trajectory of the crime decrease is shifted up (higher violent
## crime rate for the same property rate) from when it increased.
## This could have been due to a change in reporting procedures,
## over time, for example.
##
## 4. The midline colors and labels show that NY has the highest
## violent crime rate, but a modest property crime rate: Tennessee
## has a middling violent crime rate but the lowest (with VA) property
## crime rate.
##
## cleanup
rm( state.smpl, wh, pcols, crm, crm2)
# }
Run the code above in your browser using DataLab