我想要删除部分 colored颜色 图例,同时保留我设置的手动 colored颜色 ,但我无法做到这一点.
下面是一个例子:
library(data.table)
library(ggplot2)
# create dummy data
df <- data.table(x = rep(1:10,10),year = rep(2011:2020,each = 10))
df[,y := rnorm(.N,year[1] - 2011,.5),year]
unique_years <- df$year %>% unique()
# number of lines I want to highlight
Ncol <- 4
grey_gradient <- paste0("grey",round(seq(90,55,length.out = length(unique_years)-Ncol)))
highlight_colors <- viridis::inferno(Ncol,begin = .2,end = .8)
ggplot(df,aes(x,y,color = as.factor(year),group = year))+
geom_line()+
scale_color_manual(breaks = unique_years,
values = c(grey_gradient ,highlight_colors ))+
theme_minimal()
在这个例子中,我想用绿草的 colored颜色 来突出过go 4年.我想在图例中保留only these years年,但其他年份保留我定义的灰色渐变 colored颜色 .
我试过:
ggplot(df,aes(x,y,color = as.factor(year),group = year))+
geom_line()+
scale_color_manual(breaks = 2017:2020,
values = c(highlight_colors,grey_gradient ))+
theme_minimal()
但这并不能在其他年份保持灰色渐变:
我怎么能做到这一点?