Reputation: 185
I am creating a multi-line chart with legend, but I'm not sure how to force the squares in the legend to be the same color as my lines.
ggplot(df, aes(x=Month)) +
geom_line(aes(y=`Less than a high school diploma`/100, color="Less Than HS Diploma"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`High school graduates, no college`/100, color="HS Diploma"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`Some college or associate degree`/100, color="Some College / Associate's Degree"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`Bachelor's degree and higher`/100, 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")) +
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")
Upvotes: 1
Views: 415
Reputation: 123768
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)
Upvotes: 2
Reputation: 39585
This should work (No output included as no data was shared). If you want the legend filled, you must also enable inside aes()
the option fill
. After that you can scale the colors for filling with scale_fill_manual()
and use labs()
to give them a common name. Here the code:
library(ggplot2)
#Code
ggplot(df, aes(x=Month)) +
geom_line(aes(y=`Less than a high school diploma`/100,
color="Less Than HS Diploma",
fill="Less Than HS Diploma"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`High school graduates, no college`/100,
color="HS Diploma",
fill="HS Diploma"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`Some college or associate degree`/100,
color="Some College / Associate's Degree",
fill="Some College / Associate's Degree"), size=2, alpha=0.5, linetype=1) +
geom_line(aes(y=`Bachelor's degree and higher`/100,
color="Bachelor's Degree and Higher",
fill="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")) +
scale_fill_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")+
labs(color='Class',fill='Class')
Upvotes: 0
Reputation: 4456
The values
argument from scale_color_manual
should have color names instead of the line names, which you don't need to pass. Example:
scale_color_manual(name="Educational Attainment", values = c("red","yellow","white",...))
Upvotes: 0