Single Legend with color and linetype

14 views
Skip to first unread message

Glenn Schultz

unread,
Feb 21, 2015, 11:12:07 AM2/21/15
to ggp...@googlegroups.com
Hello All,

I am trying to plot the below with a single legend using both color and line type.  I would like colored lines one dashed one solid with a legend (not two).  I have been at this for hours now with no success.  any help is appreciated.  You can replicate what I have done so far with the code below.

#plot the forward and spot rates for Chapter 2 graph 2
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)


# Stacking the data for GG plot graph ch2_2
rates <- with(curve20061,
              data.frame(rate = c(fwd, spt),
                         ratetype = factor(rep(c("forward curve", "spot rate curve"),
                                               each = NROW(curve20061))),
                         mat = rep(mat,2)))

#making the graph 2_2
ggplot(rates, aes(x = mat, y = rate, colour = ratetype)) +
  geom_line(size = 1.0) +
  scale_colour_manual(values = cbbPalette, name = "Legend", labels = c("Forward Curve", "Spot Curve"))+
  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)) 

Thanks,
Glenn
  
  

Dennis Murphy

unread,
Feb 21, 2015, 3:42:05 PM2/21/15
to Glenn Schultz, ggplot2
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.
Reply all
Reply to author
Forward
0 new messages