Graphics Reference
In-Depth Information
together and should be connected with a line (see Making a Line Graph with Multiple Lines for
an explanation of why group is needed with factors):
BOD1 <- BOD # Make a copy of the data
BOD1$Time <- factor(BOD1$Time)
ggplot(BOD1, aes(x = Time, y = demand, group = 1 )) + geom_line()
Figure 4-2. Basic line graph with a factor on the x-axis (notice that no space is allocated on the x-
axis for 6)
In the BOD data set there is no entry for Time=6, so there is no level 6 when Time is converted
to a factor. Factors hold categorical values, and in that context, 6 is just another value. It happens
to not be in the data set, so there's no space for it on the x-axis.
With ggplot2, the default yrange of a line graph is just enough to include the yvalues in the
data. For some kinds of data, it's better to have the yrange start from zero. You can use ylim()
to set the range, or you can use expand_limits() to expand the range to include a value. This
will set the range from zero to the maximum value of the demand column in BOD ( Figure 4-3 ) :
# These have the same result
ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + ylim( 0 , max(BOD$demand))
ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + expand_limits(y = 0 )
Search WWH ::




Custom Search