Search code examples
rggplot2time-seriesgraph-visualization

Need help to display legend and in similar color code to the data


I am visualizing a time-series plot using ggplot2 and trying to combine the legend. I have tried many options but in not yet gotten my desired output. In one plot the lines are missing the color coding and in the other, the chart is missing the legend. My desired output is to have a chart with the legend and the color scheme being the same.

Here is the script where the lines are missing the color-coding;

library(tidyverse)
deviation <- read_csv("C:/Users/JohnWaweru/Documents/Thesis/Data/yearly_CSVs/Turkana_new/2018_new.csv")

deviation %>% ggplot() + 
  geom_line(aes(x = as.Date(Month), y = Upper_curve, col = 'red'), linetype = 2) +
  
  
  geom_line(aes(x = as.Date(Month), y = Lower_curve, col = 'red'), linetype = 2) +
  
  geom_line(aes(x = as.Date(Month), y = Mean_NDVI, col = 'red'), linetype = 1) +
  
  
  geom_line(aes(x = as.Date(Month), y = NDVI_2018, col = 'green'), linetype = 1) +
  
  scale_color_manual(name = 'Legend',
                     values = c('Mean_NDVI'= 'red', 'NDVI_2018' = 'green', 'Upper_curve' = 'red', 'Lower_curve' = 'red'),
                     labels = c('Mean_NDVI', 'NDVI_2018', 'Upper_curve','Lower_curve')) +
  
  ylim(0.2, 0.6) +
  scale_x_date(date_labels = "%b", date_breaks = "1 month") +
  ylab(label = "NDVI") +
  xlab(label = "Month") +
  ggtitle("NDVI Deviation 2018") ```

Here is the Sample data I am working with;

structure(list(Month = structure(c(18262, 18293, 18322, 18353, 18383, 18414), class = "Date"), 
Mean_NDVI = c(0.26, 0.23, 0.25, 0.34, 0.36, 0.32), 
NDVI_2018 = c(0.22, 0.23, 0.23, 0.41, 0.46, 0.32), 
Mean_Std = c(0.01, 0.01, 0.01, 0.02, 0.02, 0.02), 
Std_2018 = c(0.01, 0.01, 0.03, 0.03, 0.04, 0.03), 
Upper_curve = c(0.27, 0.24, 0.26, 0.36, 0.38, 0.34), 
Lower_curve = c(0.25, 0.22, 0.24, 0.32, 0.34, 0.3)), 
row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

Solution

  • Setting literal colours only works outside the aes() function or when you use scale_colour_identity(). Most of the time when you want to label individual line layers, you can set aes(..., colour = "My legend label").

    library(ggplot2)
    
    deviation <- structure(list(
      Month = structure(c(18262, 18293, 18322, 18353, 18383, 18414), class = "Date"), 
      Mean_NDVI = c(0.26, 0.23, 0.25, 0.34, 0.36, 0.32), 
      NDVI_2018 = c(0.22, 0.23, 0.23, 0.41, 0.46, 0.32), 
      Mean_Std = c(0.01, 0.01, 0.01, 0.02, 0.02, 0.02), 
      Std_2018 = c(0.01, 0.01, 0.03, 0.03, 0.04, 0.03), 
      Upper_curve = c(0.27, 0.24, 0.26, 0.36, 0.38, 0.34), 
      Lower_curve = c(0.25, 0.22, 0.24, 0.32, 0.34, 0.3)), 
      row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")
    )
    
    ggplot(deviation) + 
      geom_line(aes(x = Month, y = Upper_curve, colour = 'Upper_curve'), linetype = 2) +
      geom_line(aes(x = Month, y = Lower_curve, colour = 'Lower_curve'), linetype = 2) +
      geom_line(aes(x = Month, y = Mean_NDVI, colour = 'Mean_NDVI'), linetype = 1) +
      geom_line(aes(x = Month, y = NDVI_2018, colour = 'NDVI_2018'), linetype = 1) +
      scale_color_manual(
        name = 'Legend',
        values = c('Mean_NDVI'= 'red', 'NDVI_2018' = 'green', 
                   'Upper_curve' = 'red', 'Lower_curve' = 'red'),
        # Setting appropriate linetypes
        guide = guide_legend(
          override.aes = list(linetype = c(2,1,1,2))
        )
      ) +
      ylim(0.2, 0.6) +
      scale_x_date(date_labels = "%b", date_breaks = "1 month") +
      ylab(label = "NDVI") +
      xlab(label = "Month") +
      ggtitle("NDVI Deviation 2018")
    

    Created on 2021-08-05 by the reprex package (v1.0.0)