需要帮助显示图例和与数据相似的颜色代码

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

我正在使用 ggplot2 可视化时间序列图并尝试组合图例。我尝试了很多选项,但还没有得到我想要的输出。在一个图中,线条缺少颜色编码,而在另一个图中,图表缺少图例。我想要的输出是图例和配色方案相同的图表。

这是脚本,其中的行缺少颜色编码;

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"
))

设置文字颜色仅在 aes() 函数之外或使用 scale_colour_identity() 时有效。大多数情况下,当您想要标记单个线层时,您可以设置 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")

reprex package (v1.0.0)

于 2021-08-05 创建