如何使geom_line中的一条线颜色盖过其他线的颜色?

3
我想创建一个类似于下面的图表,其中蓝色颜色盖过了灰色。然而,在我创建的图表中,蓝色并没有盖过其他颜色。有人能指导我如何实现吗?
这是我想要在颜色方面得到的图表:

enter image description here

这是我的情节:

  ggplot(df, aes(year, unemp), color=cntry)+
  geom_line(aes(group=cntry, color=cntry), size=1.5)+
  scale_color_manual(values=c('dark blue','#999999','#999999','#999999','#999999','#999999'))

enter image description here

这是我的数据:

   df= structure(list(cntry = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("France", 
"Germany", "Italy", "Poland", "Spain", "United Kingdom"), class = "factor"), 
    year = c(2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
    2010, 2011, 2012, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
    2009, 2010, 2011, 2012, 2002, 2003, 2004, 2005, 2006, 2007, 
    2008, 2009, 2010, 2011, 2012, 2002, 2003, 2004, 2005, 2006, 
    2007, 2008, 2009, 2010, 2011, 2012, 2002, 2003, 2004, 2005, 
    2006, 2007, 2008, 2009, 2010, 2011, 2012, 2002, 2003, 2004, 
    2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012), unemp = c(8, 
    9, 9, 9, 9, 8, 7, 9, 9, 9, 10, 9, 10, 10, 11, 10, 9, 7, 8, 
    7, 6, 5, 9, 8, 8, 8, 7, 6, 7, 8, 8, 8, 11, 20, 20, 19, 18, 
    14, 10, 7, 8, 10, 10, 10, 11, 11, 11, 9, 8, 8, 11, 18, 20, 
    21, 25, 5, 5, 5, 5, 5, 5, 6, 8, 8, 8, 8)), row.names = c(NA, 
-66L), class = c("tbl_df", "tbl", "data.frame"))

1
你可能会对gghighlight感兴趣:https://cran.r-project.org/web/packages/gghighlight/vignettes/gghighlight.html - MrFlick
@stefan 我不认为重复的答案回答了这个问题。这里的重点是让一行排在其他行的上方,而不是突出显示它。 - Rui Barradas
1
@RuiBarradas。谢谢您。您是对的。看起来像是一个高亮显示的问题,但实际上是不同的。 - stefan
2个回答

2
用户@stefan将此问题关闭为此问题的重复,但我不认为这个答案是正确的。这个问题询问如何将一条线放在其他所有线的顶部,而不仅仅是突出显示它。
以下是一种方法。反转因子水平和反转图例,以便将France置于顶部。
library(dplyr)
library(ggplot2)

clrs <- c('dark blue','#999999','#999999','#999999','#999999','#999999')

df %>%
  mutate(cntry = factor(cntry, levels = rev(levels(cntry)))) %>%
  ggplot(aes(year, unemp), color=cntry)+
  geom_line(aes(group=cntry, color=cntry), size=1.5)+
  scale_color_manual(values = rev(clrs),
                     guide = guide_legend(reverse = TRUE))  

enter image description here


1
你可以把它单独绘制出来,放在其他图表之后。就像这样:
ggplot(df, aes(year, unemp), color=cntry)+
  geom_line(aes(group=cntry, color=cntry), size=1.5)+
  geom_line(data = df[df$cntry == 'France',], aes(group=cntry, color=cntry), size=1.5)+
  scale_color_manual(values=c('dark blue','#999999','#999999','#999999','#999999','#999999'))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接