Graphics Reference
In-Depth Information
Specifying the breaks is similar to with a numeric axis—the main difference is in specifying the
sequence of dates to use. We'll use a subset of the
economics
data, ranging from mid-1992 to
mid-1993. If breaks aren't specified, they will be automatically selected, as shown in
Figure 8-38
(top):
# Take a subset of economics
econ
<-
subset(economics, date
>=
as.Date(
"1992-05-01"
)
&
date
<
as.Date(
"1993-06-01"
))
# Base plot - without specifying breaks
p
<-
ggplot(econ, aes(x
=
date, y
=
psavert))
+
geom_line()
p
The breaks can be created by using the
seq()
function with starting and ending dates, and an
interval (
Figure 8-38
, bottom):
# Specify breaks as a Date vector
datebreaks
<-
seq(as.Date(
"1992-06-01"
), as.Date(
"1993-06-01"
), by
=
"2 month"
)
# Use breaks, and rotate text labels
p
+
scale_x_date(breaks
=
datebreaks)
+
theme(axis.text.x
=
element_text(angle
=
30
, hjust
=
1
))