如何通过相同的变量设置图例中的线型和颜色

时间:2015-11-16 16:01:05

标签: r ggplot2

我有以下代码:

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

包含AR.df数据帧。现在我想绘制线型和颜色使用相同的变量,但我得到两个传说,我想要一个传奇,但不同颜色和线型的线条为apt。 这是我的代码:

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")) 

给出了以下两个图例: enter image description here

如何摆脱第一个图例框并在第二个框中获取图例以获得线型?

1 个答案:

答案 0 :(得分:4)

感谢Roland,答案是:

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