gpt4 book ai didi

r - 如何使 geom_line 中的一种线条颜色遮盖其他线条颜色?

转载 作者:行者123 更新时间:2023-12-04 03:23:33 27 4
gpt4 key购买 nike

我正在尝试创建一个类似于下图的图,其中蓝色遮盖了灰色。然而,在我正在创建的情节中,蓝色并没有掩盖其他颜色。有人可以指导我如何实现吗?

这是我想得到的颜色图:

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

最佳答案

用户@stefan 已将此问题作为 this one 的副本关闭但我不相信骗子会回答它。这个问题问的是如何在所有其他行之上放置一条线,而不仅仅是突出显示它。

这是一个方法。反转因子水平并反转图例,以便将 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

关于r - 如何使 geom_line 中的一种线条颜色遮盖其他线条颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68079185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com