R - geom_line ggplot2中的手动图例颜色

Posted

技术标签:

【中文标题】R - geom_line ggplot2中的手动图例颜色【英文标题】:R - Manual legend color in geom_line ggplot2 【发布时间】:2015-08-02 23:31:53 【问题描述】:

我正在尝试仅手动更改 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) 是我的参考,我想用黑色显示它的行。我设法通过添加一个带有条件variable=='CRU' 的额外 geom_line 来做到这一点:

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

【问题讨论】:

如果你不想使用scale_color_manual,你可以定义一个以黑色为第一色的调色板。 【参考方案1】:

您可以简单地提供一个自定义调色板,其中CRU 映射为黑色,而不是拆分数据并绘制两个geom_lines。

如果你想为其他变量保留默认颜色,你首先需要定义一个小辅助函数来以ggplot2 does it的方式检索它们。

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

【讨论】:

您的解决方案非常棒,我非常感谢!我自己永远也想不通。

以上是关于R - geom_line ggplot2中的手动图例颜色的主要内容,如果未能解决你的问题,请参考以下文章

R语言ggplot2可视化多变量组合(interaction,group=interaction())分组下的线图(geom_line)和箱图(geom_boxplot)实战

R语言ggplot2使用geom_line函数geom_point函数可视化哑铃图并对哑铃图进行排序(reorder dumbbell plot)

R语言ggplot2可视化:自定义不同颜色的线条互相覆盖的次序按照需要的覆盖次序自定义geom_line函数的次序(Change the overlaying order of lines)

R语言ggplot2可视化:使用geom_line函数将dataframe中数据可视化为时间序列(或折线图)(Time Series Plot From a Data Frame)添加标题副标题

【R语言】--- ggplot2绘制折线图

ggplot2 geom_line 的两个不同图例