The main issue is the alpha
argument together with geom_line
. If you want the keys to show up as lines you set alpha to 1 in the legend via guides(color = guide_legend(override.aes = list(alpha = c(1, 1, 1, 1))))
. If you want colored rectangles for the keys this could be achieved by adding key_glyph = "rect"
to your geom_line
layers
Using the economics
dataset as example data:
library(ggplot2)
ggplot(economics, aes(x=date)) +
geom_line(aes(y=`psavert`/100, color="Less Than HS Diploma"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`uempmed`/100, color="HS Diploma"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`psavert`/10, color="Some College / Associate's Degree"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`uempmed`/10, color="Bachelor's Degree and Higher"), size=2, alpha=0.5, linetype=1) +
scale_color_manual(name="Educational Attainment", values = c("Less Than HS Diploma"="deepskyblue", "HS Diploma" = "firebrick1", "Some College / Associate's Degree"="mediumpurple", "Bachelor's Degree and Higher"="springgreen4")) +
guides(color = guide_legend(override.aes = list(alpha = c(1, 1, 1, 1)))) +
ggtitle("Unemployment Rate by Educational Attainment") +
xlab("Time") +
ylab("Unemployment Rate") +
scale_y_continuous(labels = scales::percent) +
theme(plot.title = element_text(hjust = 0.5), legend.position="bottom")

And with key_glyph="rect"
:
library(ggplot2)
ggplot(economics, aes(x=date)) +
geom_line(aes(y=`psavert`/100, color="Less Than HS Diploma"), size=2,alpha=0.5, linetype=1, key_glyph = "rect") +
geom_line(aes(y=`uempmed`/100, color="HS Diploma"), size=2, alpha=0.5, linetype=1, key_glyph = "rect") +
geom_line(aes(y=`psavert`/10, color="Some College / Associate's Degree"), size=2, alpha=0.5, linetype=1, key_glyph = "rect") +
geom_line(aes(y=`uempmed`/10, color="Bachelor's Degree and Higher"), size=2, alpha=0.5, linetype=1, key_glyph = "rect") +
scale_color_manual(name="Educational Attainment", values = c("Less Than HS Diploma"="deepskyblue", "HS Diploma" = "firebrick1", "Some College / Associate's Degree"="mediumpurple", "Bachelor's Degree and Higher"="springgreen4")) +
ggtitle("Unemployment Rate by Educational Attainment") +
xlab("Time") +
ylab("Unemployment Rate") +
scale_y_continuous(labels = scales::percent) +
theme(plot.title = element_text(hjust = 0.5), legend.position="bottom")

Created on 2020-10-22 by the reprex package (v0.3.0)