Graphics Reference
In-Depth Information
Here, the automatic tick marks were placed every five inches, but that looks a little off for this
data. We can instead have
ggplot()
set tick marks every four inches, by specifying
breaks
(
Figure 8-16
,
right):
hwp
+
scale_y_continuous(breaks
=
seq(
48
,
72
,
4
), labels
=
footinch_formatter)
Figure 8-16. Left: scatter plot with a formatter function; right: with manually specified breaks on
the y-axis
Another common task is to convert time measurements to HH:MM:SS format, or something
similar. This function will take numeric minutes and convert them to this format, rounding to the
nearest second (it can be customized for your particular needs):
timeHMS_formatter
<-
function
function
(x) {
h
<-
floor(x
/
60
)
m
<-
floor(x
%%
60
)
s
<-
round(
60
*
(x
%%
1
))
# Round to nearest second
lab
<-
sprintf(
"%02d:%02d:%02d"
, h, m, s)
# Format the strings as HH:MM:SS
lab
<-
gsub(
"^00:"
,
""
, lab)
# Remove leading 00: if present
lab
<-
gsub(
"^0"
,
""
, lab)
# Remove leading 0 if present
return
return
(lab)
}
Running it on some sample numbers yields: