Hi:
1. It would be better to label the factor levels with the names you
want in the legend.
2. If you want a single legend for both color and linetype, then you
need to map both aesthetics to the same variable and make sure they
have the same legend title and same labels.
3. The labs() function is your friend in this example.
A little editing of your code seems to work:
require(ggplot2)
require(termstrc)
#calculate forward and spot rates
mat <- as.numeric(seq(1:30))
fwd <- forwardrates("ns", c( 5.2047634, -0.3508385, -0.9003785,
3.001), m = 1:30)
spt <-spotrates("ns",c(5.1931403, -0.2330438, -1.1028435, 2.6001000 ),m = 1:30)
curve20061 = data.frame(mat = mat, fwd = fwd, spt = spt)
## Modified the factor labels
rates <- with(curve20061,
data.frame(rate = c(fwd, spt),
ratetype = factor(rep(c("Forward curve", "Spot curve"),
each = NROW(curve20061))),
mat = rep(mat,2)))
ggplot(rates, aes(x = mat, y = rate, colour = ratetype, linetype = ratetype)) +
geom_line(size = 1) +
labs(colour = "Legend", linetype = "Legend", x = "Maturity (years)",
y = "Fixed Rate Payer Side") +
# ylab("Fixed Rate Payer Side") +
# xlab("Maturity (years)") +
theme_minimal()+
theme(legend.text = element_text(size = 12))+
theme(panel.grid.major = element_line(size = .25, color = "grey")) +
theme(legend.position=c(.18,.85),
legend.background = element_rect(fill = "white", colour = "white"))+
theme(axis.text = element_text(size = 15)) +
theme(axis.title = element_text(size = 20))
If you want to change the colors from the defaults, then you can add a
scale_colour_manual() line to the above, or simply do, for example,
last_plot() + scale_colour_manual(values = c("firebrick", "blue"))
Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
>
https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email
ggp...@googlegroups.com
> To unsubscribe: email
ggplot2+u...@googlegroups.com
> More options:
http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
ggplot2+u...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.