How to add ggplot legend to scatterplot with two separate dataset lines

Jack Thomas

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.

morgan121

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to add legend for vertical lines in ggplot?

Add separate legend in ggplot for trendline

Ggplot, how to have two lines in same chart and have it produce a legend

How do I add a legend to identify vertical lines in ggplot?

How to add multiple geom_smooth lines to the legend (ggplot)?

How to add legend to plot with multiple lines using ggplot

ggplot2 - how to add a break between dotted lines in legend?

Add more factors to ggplot scatterplot without adding lines using two dataframes

How to add legend based on color in scatterplot [matplotlib]

How add legend in ggplot

How to add legend to a ggplot?

How to add area between two lines in ggplot

ggplot2: scatterplot with two variables (measured on the same scale) on the y-axis: how do I change the aesthetics & add seperate regression lines?

Adding a single legend for two horizontal lines in ggplot

How to create an legend for ggplot for just one series of data and then add legend for additional horizontal lines?

ggplot2 add separate legend each for two Y axes in facet plot

How do I add regression lines to scatterplot

ggplot2 - separate legend for multiple geom_lines

how to add manually a legend to ggplot

How to add a legend to ggplot in R?

Add two geom_lines to 1 legend

Add lines to Scatterplot in R

How can I combine two ggplot2 custom functions makig the legend separate

How to add legend labels per plotted column to multiple scatterplot subplots?

ggplot2: modify legend's elements for two factors in scatterplot {ggplot2}?

Add legend to ggplot object (why two legends?)

matplotlib - How to add two labels to one scatterplot

Adding a legend to a ggplot with a scatterplot and a line graph

How to add a legend for two geom layers in one ggplot2 plot?