Hi:
I get the same result as you did. Perhaps the problem is the failure to
define breaks in the scale code. The issue, of course, is to figure out what
should be the breaks...
One approach is to go back into the data, add variables below and above,
melt the data frame to stack below and above into one variable, and plot.
Why would one want to do that? In order to create a factor variable with
levels lower and upper to pass as an aesthetic in the ggplot() call. The
function melt() from the reshape package is useful for this task. A useful
trick is to define the variable and level names to match the legend titles
you want.
For some reason, melt doesn't like Time-Series objects in a data frame, so I
converted level to numeric.
# library(ggplot2)
huron2 <- huron
huron2$level <- as.numeric(huron2$level)
# Add levels below and above
huron2 <- transform(huron2, below = level - 5, above = level + 5)
# Melt the data to stack below and above. The names go into
# a factor variable named Direction;
# the values are in a new variable called (oddly enough) value
# variable_name is an argument in very recent versions of reshape(2)
huron3 <- melt(huron2, measure = c('below', 'above'),
variable_name 'Direction')
head(huron3)
# Direction is a factor variable, so it makes a nice choice for an
aesthetic:
ggplot(huron3, aes(x = year, y = value, colour = Direction)) +
ylab('Water level') + geom_line() +
scale_colour_manual(breaks = levels(huron3$Direction),
values = c('blue', 'red'))
With the levels of Direction defined, the breaks are easy to identify, and
we can use values = to specify the desired colors. If you're just learning
ggplot2, try to minimize the amount of work you have to do to specify a
scale (personal experience :) ggplot2 is easiest when you do the hard work
in arranging the data so that the call to ggplot() Is straightforward. In
other words, it's not just about ggplot2, it's also about plyr and
reshape(2). Keep reading; you'll get there (Chapter 9 :)
I prefer to think of this problem as an evolution of the package. I'll let
Hadley be the judge of whether or not the B word applies.
HTH,
Dennis
On Sun, Jan 30, 2011 at 9:54 AM, Robert Baer <rbaer@atsu.edu> wrote:
> According to Hadley's ggplot book (p. 109), both the graphs below
should
> have a legend, and yet none appears in my hands.
>
> Any suggestions? I can't see a typo. Is there a bug?
>
> library(ggplot2)
> data(LakeHuron)
> huron = data.frame(year=1875:1972,level=LakeHuron)
> p = ggplot(huron, aes(year)) +
> geom_line(aes(y= level - 5), colour = 'blue') +
> geom_line(aes(y= level + 5), colour = 'red')
> print(p)
>
> key = c('below' = "blue", 'above' =
"red")
> p = p + scale_color_manual("Direction", values = key)
> print(p)
>
> > sessionInfo()
> R version 2.12.1 (2010-12-16)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] grid stats graphics grDevices utils datasets methods
> [8] base
>
> other attached packages:
> [1] ggplot2_0.8.9 proto_0.3-8 reshape_0.8.4 plyr_1.4
>
> loaded via a namespace (and not attached):
> [1] tools_2.12.1
> >
> # on Windows XP
> > version
> _
> platform i386-pc-mingw32
> arch i386
> os mingw32
> system i386, mingw32
> status
> major 2
> minor 12.1
> year 2010
> month 12
> day 16
> svn rev 53855
> language R
> version.string R version 2.12.1 (2010-12-16)
> >
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]