Try this:
library('reshape2')
library('ggplot2')
### Starting from your input data frame DSTest...
DSm <- melt(DSTest, id = 'Weeks')
ggplot(DSm, aes(x = Weeks, y = value, colour = variable)) +
geom_point() + geom_line(aes(group = variable)) +
scale_colour_manual(breaks = levels(DSm$variable),
values = c('red', 'green', 'blue'))
Look at DSm carefully and observe that the reshape of your data from
wide format to long makes it easier to define a color aesthetic with
the factor 'variable' that can be applied to both points and lines
simultaneously. Then it's easy to apply scale_colour_manual to the
levels of the constructed factor variable. More inline.
What you're attempting to do here is to create a color aesthetic on
the fly, using red, green and blue as the names of the levels of the
factor being constructed with the multiple lines of code. However, you
then try to redefine the colors by using scale_fill_manual() rather
than scale_colour_manual(). That won't work. As someone who struggled
with scales when first learning ggplot2, I feel your pain...
Consult the examples on pp. 108-109 of the ggplot2 book to see how to
do this correctly; if you don't have a copy available, then the
relevant code from Chapter 6 is shown below. Take note of the
differences among the three graphs and make sure you understand why
they are different, because they illustrate the fundamental concepts
of mapping vs. setting a plot aesthetic. (Notice that only the
x-variable year is defined as an aesthetic in the ggplot() statement;
this is because the 'y' will vary in different calls to geom_line().
Aesthetics defined in ggplot() are expected to be the same in all
layers created by different geom calls.)
huron <- data.frame(year = 1875:1972, level = LakeHuron)
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5), colour = "blue") +
geom_line(aes(y = level + 5), colour = "red")
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above"))
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above")) +
scale_colour_manual("Direction",
c("below" = "blue", "above" = "red"))
>
> plDSTest2
>
> plDSTest2 + scale_fill_manual(
> #values=c("red", "green", "blue"),
> name="Means ",
> labels=c("No Risk Mean", "Function Mean", "Wellbeing Mean"),
> breaks=c("NoRiskM", "FunctM", "WellbeM")
> )
>
> ### Example 2 uses plDSTest1
> ################################################
> plDSTest1 <- ggplot(DSTest, aes(x=Weeks)) +
> geom_point(colour="red",aes(y = NoRiskM)) +
> geom_line(colour="red", aes( y = NoRiskM )) +
> geom_point(colour="green",aes(y = FunctM)) +
> geom_line(colour="green",aes(y = FunctM)) + ## colour = label for lines
> geom_point(colour="blue",aes(y = WellbeM)) +
> geom_line(colour="blue",aes(y = WellbeM))
In this case, you're setting colors for each variable rather than
mapping them, so no legend will be created because legends are only
drawn from *mapped* aesthetics**. You get the graph you want, but not
the legend, and trying to create it with the ensuing code will be a
futile exercise.
The code from the book shows you how to correctly define a factor for
an aesthetic on the fly - note that the aesthetic used in the manual
scale must match the aesthetic you created (in your case, color rather
than fill). A simpler way to do things, once you know how, is to
reshape the data first. Quite often, rearranging the data prior to
invoking ggplot() or qplot() can save a lot of time and frustration.
** A single value associated with an aesthetic is said to be 'set';
this is done outside the aes() call inside ggplot() or a geom. A
variable associated with an aesthetic is said to be 'mapped' (i.e., a
1-1 mathematical relationship) and is defined inside an aes() call. In
order for a legend to be drawn, an aesthetic has to be mapped. This is
a fundamental concept in ggplot2 which has to be grasped; otherwise,
you're in for a lot of frustration.
Dennis
>
> plDSTest1
>
> plDSTest1 + scale_fill_manual(
> values=c("red", "green", "blue"),
> name="Means ",
> labels=c("No Risk Mean", "Function Mean", "Wellbeing Mean"),
> breaks=c("NoRiskM", "FunctM", "WellbeM")
> )
>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
The simplest way is to melt the data, reorder the levels of the factor
variable in the melted data and then construct the ggplot.
Starting from DSString,
DSm <- melt(DSString, id = 'Weeks')
DSm$variable <- factor(DSm$variable, levels = c('NoRiskM', 'FunctM', 'WellbeM'))
...same ggplot() + ... code as before based on the melted data.
I wouldn't even try to order the factor levels within ggplot() using
your approach, but maybe someone else is willing to go there. AFAIK,
factors created in ggplot() are ordered lexicographically, but there
may be a trick of which I'm unaware. My suggestion would be to melt
the data; it gives you more control and the graphics code is cleaner,
less repetitive and easier to understand.
Dennis