在ggplot2中使用geom_line时手动设置图例颜色

3
我试图手动更改 ggplot2 线图中传说中的第一项的颜色。
我有几个变量的观察结果需要在线图中展示,就像这样:
ggplot(tmp1, aes(x=factor(month), y=value, group=variable, colour=variable ) ) + 
       geom_line(size=1) + geom_point(size=2.5) + theme_grey(base_size = 18) +
       xlab(NULL) + ylab('%') + theme(legend.title = element_blank()) + theme(axis.text.x=element_blank()) +
       ggtitle("a) Cloud fraction") + theme(plot.title = element_text(hjust = 0))

然而,第一个变量(CRU)是我的参考系,我想要以黑色显示它的线条。我通过添加一个额外的geom_line并加入条件variable=='CRU'来实现这一点。
ggplot(tmp1, aes(x=factor(month), y=value, group=variable, colour=variable ) ) + 
       geom_line(size=1) + geom_point(size=2.5) + theme_grey(base_size = 18) +
       geom_line(data=subset(tmp1, variable == "CRU"), colour="black", linetype="solid", size=1) +
       geom_point(data=subset(tmp1, variable == "CRU"), colour="black", size=2.5) +
       xlab(NULL) + ylab('%') + theme(legend.title = element_blank()) + theme(axis.text.x=element_blank()) +
       ggtitle("a) Cloud fraction") + theme(plot.title = element_text(hjust = 0))

这段话的意思是:“这适用于线条,但图例仍保留旧颜色。我该如何仅更改图例的第一个元素的颜色,以匹配新的黑色线条?这是我的数据示例。”
library(ggplot2)

tmp1 <- structure(list(month = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L), .Label = c("Jan", "Feb", "Mar", 
"Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L
), .Label = c("CRU", "CanESM2", "GFDL-ESM2M", "GISS-E2-H", "GISS-E2-R-CC", 
"GISS-E2-R", "HadGEM2-AO", "HadGEM2-CC", "IPSL-CM5A-MR", "IPSL-CM5B-LR", 
"MIROC4h", "MRI-CGCM3", "NorESM1-M", "bcc-csm1-1-m", "bcc-csm1-1", 
"inmcm4"), class = "factor"), value = c(68.9226631460789, 68.2418796877392, 
68.3045372212868, 66.5727907036073, 64.278360290491, 60.6452267972856, 
56.4079999829923, 57.4384828307567, 60.874295882443, 63.70427487797, 
65.9934520468731, 68.9723871966257, 69.0959015590216, 68.6126351492122, 
65.9106136896166, 65.790169283913, 64.6320994816801, 63.894111784301, 
62.0459530253135, 60.0455773681386, 59.4195693791228, 59.8531302282566, 
62.8877658601921, 66.4625078340445, 63.4659654507164, 64.5466810117518, 
63.6412932878715, 61.5786848043378, 60.6491980933614, 63.5160886052168, 
62.739218138279, 60.8826348052995, 60.1196738813257, 59.0451443027396, 
58.9044684656519, 61.5033887899156, 62.442928703121, 61.9933297554931, 
61.686560285787, 62.1675956585161, 63.0625380934021, 63.3192922622326, 
62.6727899590586, 60.9706714311941, 59.4656895840826, 59.8689092461429, 
60.7585523645951, 62.2374164636759, 62.2586495696979, 62.3005886556949, 
62.0719314334763, 61.7786313583016, 62.1037020616999, 62.5919637033876, 
60.7746642298107, 58.7307471416832, 57.6602849809809, 57.3379551651851, 
59.8210398283061, 61.5997238276034, 62.1190176575675, 62.2214930174241, 
61.607539296931, 61.836536870373, 61.8298589429815, 62.0478835210295, 
60.8165122782774, 59.224498365607, 57.5387307267022, 56.8641846144649, 
59.6779581588162, 61.5822371331742, 56.9625864272884, 55.0519081266715, 
53.9161532646461, 52.0847886852487, 54.1855963059705, 54.1565901942167, 
53.8164314129289, 53.3013959169719, 52.1283494730607, 49.9814907883562, 
51.0053330490513, 54.1758812796363, 54.1947459143536, 53.2985061657513, 
51.5351727215781, 51.2131541342776, 53.040182168441, 53.4657505459587, 
52.8257974728027, 52.8523832284788, 51.2527233914323, 48.0999294191007, 
48.3915726340961, 50.9305288780026, 65.3647375158419, 64.6894843930494, 
62.2700707798592, 60.2848148985731, 59.0797813854392, 58.6641353922813, 
60.36671822738, 61.0883458866571, 60.3963355506111, 60.989444946264, 
62.1570976843054, 64.0549504714623, 63.043822206253, 61.5388900651697, 
61.0125502971802, 60.4999006674972, 60.9554692113674, 61.2665703834057, 
61.1470225339614, 61.4827838311531, 60.0397138517742, 61.6503963603034, 
62.7421837830534, 63.9911949044232, 55.7117557057576, 55.0687784028633, 
51.7447044604762, 50.5160095376821, 51.7744811245234, 52.6710116909617, 
52.9126480516047, 51.6347065362984, 50.6773480024225, 48.8928054774924, 
50.3505731163001, 53.7488684714513, 61.558109087334, 61.6673093977654, 
61.008465555097, 58.5478578294864, 57.4119260976748, 57.9275733769477, 
56.9129774651439, 55.6494927089111, 52.0222406797903, 51.9215916366208, 
53.4679949695072, 58.2128251869788, 64.7955701998493, 62.8319013929061, 
60.8391061131818, 56.1759467734789, 55.4331550199683, 55.8437923896573, 
54.998540828777, 54.7840203124691, 54.3853750266133, 52.7590435522892, 
56.1409799671355, 62.0047140533332, 57.5185465474672, 57.2532289998115, 
55.9911913829976, 54.6479285609432, 53.1659722964534, 53.3609799276622, 
51.321452599498, 49.6933914680193, 48.6718229103421, 49.5393207890844, 
52.8096091918065, 56.1667672797739, 60.7380412023987, 60.1791897430251, 
58.7798069796932, 58.061108119255, 59.7770862278418, 60.2070273632675, 
59.074898814382, 55.5571990297011, 53.8564792650491, 54.0753885029223, 
56.2369958393563, 58.9062125901571, 70.7538119957697, 69.4271857400385, 
67.3954189057409, 66.9262104442679, 67.1558044757422, 65.8848885390536, 
65.3092556552615, 64.3799468889004, 64.9999333535186, 65.6493831700943, 
69.2646980549075, 70.6342115226731)), row.names = c(NA, -192L
), .Names = c("month", "variable", "value"), class = "data.frame")

1
如果您不想使用scale_color_manual,您可以定义一个颜色调色板,其中黑色是第一种颜色。 - Roland
1个回答

5

不需要将数据分割并绘制两个geom_line,而是可以简单地提供自定义的颜色调色板,其中CRU被映射为黑色。

如果希望保留其他变量的默认颜色,则首先需要定义一个小助手函数来检索它们,就像ggplot2所做的那样。

gg_color_hue <- function(n) {
  hues = seq(15, 375, length=n+1)
  hcl(h=hues, l=65, c=100)[1:n]
}

然后创建一个自定义的颜色调色板向量,将标准调色板和黑色组合在一起。由于 CRU 是您的因子变量的第一级(总共有16个级别),因此这很简单

custom_palette <- c("#000000", gg_color_hue(15))

以下内容将生成您想要的图表:
ggplot(tmp1, aes(x=factor(month), y=value, group=variable, colour=variable)) + 
  geom_line(size=1) + 
  geom_point(size=2.5) + 
  scale_colour_manual(values=custom_palette) +
  theme_grey(base_size = 18) +
  xlab(NULL) + ylab('%') + 
  theme(legend.title = element_blank()) + 
  theme(axis.text.x=element_blank()) +
  ggtitle("a) Cloud fraction") + 
  theme(plot.title = element_text(hjust = 0))

你的解决方案非常出色,我非常感激!我自己永远也想不出来。 - thiagoveloso

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