I can't figure out how to add a legend to this scatterplot when 2 different lines are present. I've tried different types of solutions to this (like adding scale_colour_manual), but it isn't working.
Here is the code. It produces the graph properly, but a legend does not appear.
months <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
num_17 <- c(NA, 12, 20, 25, 13, 22, 6, 55, 7, 44, 14, 11)
num_18 <- c(33, 21, 45, 31, 27, 42, 16, 38, 22, 19, 39, 9)
time_df <- data.frame("Months" = months, "Submissions 2017" = num_17, "Submissions 2018" = num_18)
two_colors <- c("lightblue", "orange")
time_df$Months <- factor(time_df$Months, levels=c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))
ggplot(time_df, aes(x = Months, color = Months, group = 1)) +
geom_line(y = num_17, size = 2, colour = "lightblue") +
geom_line(y = num_18, size = 2, colour = "orange") +
geom_point(aes(y = num_17), size = 3, color = "blue") +
geom_point(aes(y = num_18), size = 3, color = "red") +
xlab("2017 and 2018") + ylab("Number Submissions") +
ggtitle("Submissions By Month (2017 and 2018)") + theme_bw()+
scale_colour_manual(name="Legend", values = c(num_17 = "2017", num_18 = "2018"))+
theme(axis.text = element_text(size = 15),
axis.title.y = element_text(size = 20, margin = margin(r = 25)),
axis.title.x = element_text(size = 20, vjust = -7),
plot.title = element_text(size = 22, face = "bold"),
plot.margin = unit(c(1,1,1,1), "cm"))
I just want a legend that labels each of the groups. Thanks in advance.
This should do what you want:
ggplot(time_df, aes(x = Months, group = 1)) +
geom_line(aes(y = Submissions.2017, color="2017"), size=2) +
geom_line(aes(y = Submissions.2018, color="2018"), size=2) +
geom_point(aes(y = Submissions.2017), size = 3, color="blue") +
geom_point(aes(y = Submissions.2018), size = 3, color="red") +
xlab("2017 and 2018") +
ylab("Number Submissions") +
ggtitle("Submissions By Month (2017 and 2018)") +
scale_colour_manual(values = c("lightblue", "orange")) +
theme_bw()+
theme(axis.text = element_text(size = 15),
axis.title.y = element_text(size = 20, margin = margin(r = 25)),
axis.title.x = element_text(size = 20, vjust = -7),
plot.title = element_text(size = 22, face = "bold"),
plot.margin = unit(c(1,1,1,1), "cm"))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments