如何从R(ggplot2)中的合并数据框添加第二个图例?

时间:2018-02-06 11:05:41

标签: r plot ggplot2

我有一个关于可负担性比率(针对特定项目)的合并数据框,我在ggplot中创建了一个图。年份在x轴上,经济性比率在y轴上。第一个变量(med.aupportability.ratio)通过sraight线表示,第二个变量(较低的四分位负担能力比率)通过虚线表示(通过使用两个变量的geom_line)。我可以为第一个变量创建一个图例,但是我无法在图表中添加第二个图例,它代表第二个变量,即虚线。有人有想法吗?提前谢谢!

PS:最终使用scale_color_manual函数?!

ggplot(affordability.ratio.final, aes   
    (x=affordability.ratio.final$year,
    here`y=affordability.ratio.final$affordability.ratio, 
    color=affordability.ratio.final$local_authority)) +
    labs(title="Affordability Ratios",
    x="Year", y = "Affordability Ratio", 
    color='Local \nAuthority') +
    # scale_y_continuous(breaks=c(75000, 100000, 125000, 150000, 175000)
    # geom_label_repel(force=15, show.legend = FALSE) +
    theme(axis.title=element_text(size="14"), 
    legend.title=element_text(size="14"), 
    legend.text=element_text(size="12"),
    legend.position="bottom",
    axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)),
    axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0)),
    plot.title = element_text(margin = margin(t = 0, r = 0, b = 10, l = 0))) +
    geom_line(data=affordability.ratio.final, 
            aes(x=affordability.ratio.final$year, 
                y=affordability.ratio.final$med.affordability.ratio, 
                group=affordability.ratio.final$local_authority),
            size=1) +
    geom_line(data=affordability.ratio.final, 
            aes(x=affordability.ratio.final$year, 
                y=affordability.ratio.final$lq.affordability.ratio, 
                group=affordability.ratio.final$local_authority),
            size=1, linetype='dashed')

0 个答案:

没有答案