4

I have the following code:

structure(list(Type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Complex-valued", "Magnitude-only"), class = "factor"), 
    AR.coef = c(0, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.175, 
    0.2, 0.225, 0.25, 0.275, 0.3, 0, 0.025, 0.05, 0.075, 0.1, 
    0.125, 0.15, 0.175, 0.2, 0.225, 0.25, 0.275, 0.3), variable = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "p=phat", class = "factor"), 
    value = c(0.978520704704333, 0.97587336999776, 0.973265254008069, 
    0.968200928906036, 0.964398292699414, 0.958921339792823, 
    0.954257350293665, 0.949424417504299, 0.942783668590964, 
    0.937194487113138, 0.931500996099189, 0.925212095505999, 
    0.918554141700981, 0.978840106896505, 0.976226910306292, 
    0.973587133603869, 0.968631836203812, 0.965199812495933, 
    0.960256158986167, 0.956236529101204, 0.951525171907093, 
    0.944964615583583, 0.938993495998045, 0.933391264497136, 
    0.926722511197902, 0.920070531126096)), row.names = c(NA, 
-26L), .Names = c("Type", "AR.coef", "variable", "value"), class = "data.frame")
>ere

which contains the AR.df dataframe. Now I want to plot using the same variable for linetype and colour but I am getting two legends, I would like one legend but the lines with different colors and linetypes as apt. Here is my code:

ggplot(AR.df, aes(x = AR.coef, y = value, linetype = Type, colour = Type)) +  
            geom_line(size=1.25) + xlab("AR(1) coefficient") + 
            ylab("Proportion") + theme_bw() + 
            theme(legend.justification=c(1,-0.2), 
                  legend.position=c(0.45,0.5), 
                  legend.text=element_text(size=10), 
                  legend.title=element_text(size=10), 
                  axis.title.x=element_text(size=10), 
                  axis.title.y=element_text(size = 10), 
                  legend.key = element_blank(), 
                  legend.background = element_rect(color="black",size = 0.1)) + 
            scale_colour_manual(values=cbPalette, name="Analysis Type") + 
            scale_linetype_manual(values=c("solid", "dashed")) + 
            theme(legend.key.width=unit(3,"line")) 

which gives the following plot with two legends: enter image description here

How do I get rid of the first legend box and get the legend in the second box to also have the linetype?

7
  • 2
    You need to ensure that both scales have the same title. Add a name to scale_linetype_manual.
    – Roland
    Commented Nov 16, 2015 at 16:08
  • @Roland, I noticed that one scale is named so it would be a matter of adding the name to the second scale.
    – Konrad
    Commented Nov 16, 2015 at 16:11
  • Yes, both of you are right. Are there any other ways of doing it? Commented Nov 16, 2015 at 16:17
  • @Konrad That's exactly what Roland is saying.
    – Jaap
    Commented Nov 16, 2015 at 16:19
  • @Jaap, correct, duly deleted. I was typing when the comment arrived!
    – Konrad
    Commented Nov 16, 2015 at 16:21

1 Answer 1

9

Thanks to Roland, here is the answer:

require(ggplot2)
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", 
           "#0072B2", "#D55E00", "#CC79A7") 
cbPalette <- cbPalette[-1]

ggplot(subset(AR.df)) + 
  geom_line(aes(x = AR.coef, y = value, linetype = Type, colour = Type),size=1.25) + 
  xlab("AR(1) coefficient") + 
  ylab("Proportion") + 
  theme_bw() + 
  theme(legend.justification=c(1,-0.2), 
        legend.position=c(0.4,0.3), 
        legend.text=element_text(size=10), 
        legend.title=element_text(size=10), 
        axis.title.x=element_text(size=10),
        axis.title.y=element_text(size = 10), 
        legend.key = element_blank(), 
        legend.background = element_rect(color="black",size = 0.1)) + 
  scale_colour_manual(values=cbPalette, name="Analysis Type") + 
  scale_linetype_manual(values=c("solid", "dashed"),name="Analysis Type") + 
  theme(legend.key.width=unit(3,"line")) 

enter image description here

6
  • As this is actually Roland's answer, you might consider making this answer a community wiki answer.
    – Jaap
    Commented Nov 16, 2015 at 16:20
  • How does one do this? Commented Nov 16, 2015 at 16:34
  • at the bottom-right of the answer-box you will find a check-mark for that
    – Jaap
    Commented Nov 16, 2015 at 16:36
  • Done! However, I have a new issue, about changing the text in the legend which messes the plot. Should that be a new post? Commented Nov 16, 2015 at 16:41
  • yes, it's better to post a new question about that (and you can accept this answer)
    – Jaap
    Commented Nov 16, 2015 at 16:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.