0

I have a merged dataframe about the affordability ratio (for a specific item), from which I made a plot in ggplot. The years are on the x-axis and the affordability ratio is on the y-axis. The first variable (med. affordability.ratio) is represented through sraight lines and the second one (lower quartile affordability ratio) through dashed lines (by using the geom_line for both variables). I can create a legend for the first variable, but I cannot add a second legend to the graph, which represents the second variable, the dashed lines. Someone an idea? Thanks in advance!

PS: eventually by using the scale_color_manual function?!

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')
timfaber
  • 2,060
  • 1
  • 15
  • 17
  • Please properly format your question. – Vikas Yadav Feb 06 '18 at 11:15
  • 1
    thank you for the advice, sorry for the wrong format ;) – boombox.92 Feb 06 '18 at 12:27
  • 2
    Perhaps Vikas is referring to [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It's at least a tad hard to help you when I can't run the code you show here. We don't have your dataset, remember? – 4rj4n Feb 06 '18 at 13:44
  • And have you seen [this question](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot)? To me, the question and the answers (especially answer 2) seem to overlap with what you're asking here. – 4rj4n Feb 06 '18 at 13:46
  • thank you for the hint, I will check them out. – boombox.92 Feb 07 '18 at 21:27

0 Answers0