Graphics Reference
In-Depth Information
Figure 8-15. Left: scatter plot with automatic tick labels; right: with manually specified labels on
the y-axis
Discussion
Instead of setting completely arbitrary labels, it is more common to have your data stored in one
format, while wanting the labels to be displayed in another. We might, for example, want heights
to be displayed in feet and inches (like 5'6") instead of just inches. To do this, we can define a
formatterfunction, which takes in a value and returns the corresponding string. For example, this
function will convert inches to feet and inches:
footinch_formatter <- function
function (x) {
foot <- floor(x / 12 )
inch <- x %% 12
return
return (paste(foot, "'" , inch, "\"", sep="" ))
}
Here's what it returns for values 56-64 (the backslashes are there as escape characters, to distin-
guish the quotes ina string from the quotes that delimita string):
footinch_formatter( 56 : 64 )
"4'8\"" " 4 '9\"" "4' 10 \ "" "4'11\"" " 5 '0\"" "5' 1 \ "" "5'2\"" " 5 '3\"" "5' 4 \ ""
Now we can pass our function to the scale, using the labels parameter ( Figure 8-16 ):
hwp + scale_y_continuous(labels = footinch_formatter)
Search WWH ::




Custom Search