Help adding legend to my code

library(readr)
EEB119_Sheet1 <- read_csv("Downloads/EEB119_final_Sheet1.csv")
x <- ggplot() +
geom_line(data=EEB119_final_Sheet1, aes(x = Year, y = Population),colour="green", size=1) +
geom_line(data=EEB119_final_Sheet1, aes(x = Year2, y = Population2),colour="orange", size=1) +
geom_line(data=EEB119_final_Sheet1, aes(x = Year3, y = Population3),colour="red", size=1) +
geom_line(data=EEB119_final_Sheet1, aes(x = Year4, y = Population4),colour="blue", size=1)+
geom_line(data=EEB119_final_Sheet1, aes(x = Year5, y = Population5),colour="purple", size=1)

x + ggtitle("Bluefin Tuna population over 10 years with various harvesting rates") +
xlab("Years") + ylab("Population Size") + theme_bw()

Ive tried to add a legend with + theme(legend.position="right") and other ways but nothing seems to be working. PLEASE HELP

Hi, welcome to the forum @dlase626! So I can't answer your question directly, but here is a solution which takes your data and converts it from wide to long format which is typically prefereable for R. There are probably more elegant ways to do this though...

library(tidyverse)

data<- data.frame(
          Year = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    Population = c(878000, 879460, 958246, 911342, 933491, 938868, 938608,
                   946621, 950277, 954953, 960061),
         Year2 = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
   Population2 = c(878000, 870380, 884095, 896338, 896338, 892257, 884688,
                   883457, 878678, 874893, 871292),
         Year3 = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
   Population3 = c(878000, 859938, 907107, 852758, 854020, 839844, 824447,
                   813650, 800347, 788123, 776074),
         Year4 = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
   Population4 = c(878000, 849950, 880930, 822787, 813952, 790908, 768598,
                   749692, 729361, 710295, 691625),
         Year5 = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
   Population5 = c(878000, 840416, 855951, 794175, 776077, 745281, 716903,
                   691183, 665017, 640586, 616825)
)

# Convert your data to the prefered format... 
long_format <-
data %>%
  gather(Population, value, -Year) %>%
  # Get rid of 'values' in the Population column which don't contain 'Population'
  filter(grepl("Population", Population)) %>%
 # Extract the population number
  mutate(Population = str_extract(Population, "[0-9]+"),
  # For population 1, add 1 since it's not defined in the data
         Population = ifelse(is.na(Population), "1", Population))


head(long_format)
#>   Year Population  value
#> 1    0          1 878000
#> 2    1          1 879460
#> 3    2          1 958246
#> 4    3          1 911342
#> 5    4          1 933491
#> 6    5          1 938868

  ggplot(long_format, aes(x = Year, y = value, group = Population, colour = Population)) +
  geom_line(size = 1) +
  theme_bw() +
  scale_x_continuous(
    breaks = seq(0,10,1),
    name = "Year"
  ) +
  scale_y_continuous(
    name = "Population size"
  )

Created on 2019-06-06 by the reprex package (v0.2.1)

Is this what you want?

I would want the legend to display values other than "1","2" etc... Is there a way I can rename those values displayed on the legend?

Sure, try playing around with the following:

  + scale_colour_discrete(
    labels = c("one", 2, "three", 4, 5)
  )

If you want your own specified colours, use:

+ scale_colour_manual(
    labels = c("one", 2, "three", 4, 5),
    breaks = c(1, 2, 3, 4, 5),
    values = c("red", "blue", "green", "pink", "black")
  )

OMG THANK YOU SO MUCH IT WORKED! I have been stuck on this for a while now. Thanks again!!

You're most welcome @dlase626, glad I could help :smile:. I think for the future, if you reorganise your data in long format so it looks similiar to mine, you'll have far fewer headaches when it comes to ggplot2. Also just FYI, the way you read your data into R is perfectly fine (and preferable to the way I did it). My code above should work with EEB119_Sheet.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.