ggplot2 中两种不同颜色美学映射的不同调色板
Different colour palettes for two different colour aesthetic mappings in ggplot2
我的问题与 this and this and also this 的问题非常相似。我有一个散点图(使用 geom_point
),使用特定的调色板按一个因素着色。我正在使用 stat_smooth
通过点绘制某些平滑线,按 另一个 因子分组。我希望这些线条使用不同的调色板。
这是一个Dropbox link to some example data。做一个
currDT <- read.table("SO_data", sep = "|", header = TRUE, strip.white = TRUE)
我通常将我的数据放在 data.table 中,因此您可能会发现更改它也很有帮助。哦,这是我目前正在使用的配色方案,您可以使用 scale_colour_brewer
生成您自己的配色方案,为了完整起见,我将其包括在内。
my_col_scheme <- c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#7B8A7B",
"#0B29D6", "#f781bf", "#999999", "black")
希望这已经足够清楚了。这是一些示例代码:
icorr_elec <- ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
geom_point(aes(colour = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_colour_manual(values = my_col_scheme) +
stat_smooth(method = "lm", formula = y ~ x + 0, aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides"))
如何在 ggplot2 中完成此操作?从我收集到的其他问题中,我可以手动指定每一行,但我想避免这种情况。
我认为ggplot2
不会让你换两次颜色并更新图例。我似乎记得读过你不能在一个情节中改变 scale_color_manual
两次。我找不到那个网页,但是这个 post touches on the topic. I also tried using the suggestions in this post,但是没有用。可能是因为我们正在尝试混合 geoms
(但这只是一个猜测)。
我可以得到颜色回归线:
part1 <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"),
labels = c("Halides", "Non-Halides")) +
scale_color_manual(name = "", values = c("red", 'blue'),
labels = c("Halides", "Non-Halides"))
ggsave('part1.jpeg', part1)
或者要绘制的数据点:
part2 <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
geom_point(aes(color = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"),
labels =c("Halides", "Non-Halides")) +
scale_colour_manual(values = my_col_scheme)
ggsave('part2.jpeg', part2)
但不是两者:
both <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides")) +
geom_point(aes(color = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_colour_manual(values = my_col_scheme)
ggsave('both.jpeg', both)
我认为您需要添加每一行手册。希望其他人知道如何回答这个问题,但我认为@hadley 不允许更改两种颜色。幸运的是,您 data.table
所以应该很容易做到:-)
对其他试图解决此问题的人发表评论希望我的部分回答能帮助您回答这个问题。另外,我的第三个图显示了 ggplot2
如何没有像 OP 想要的那样得到图例颜色 correct。作为另一个提示,您可以尝试使用图例选项。
您可以为点使用填充点标记并将其映射到 fill
美学,同时保持线映射到 colour
,从而为线和点获得单独的颜色映射审美的。实心点标记是编号为 21 到 25 的标记(参见 ?pch
)。这是一个例子,改编@RichardErickson 的代码:
ggplot(currDT, aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides")) +
geom_point(aes(fill = Anion, shape = Cation), size = 3, alpha = 0.4, colour="transparent") +
scale_colour_manual(values = c("blue", "red")) +
scale_fill_manual(values = my_col_scheme) +
scale_shape_manual(values=c(21,24)) +
guides(fill = guide_legend(override.aes = list(colour=my_col_scheme[1:8],
shape=15, size=3)),
shape = guide_legend(override.aes = list(shape=c(21,24), fill="black", size=3)),
colour = guide_legend(override.aes = list(linetype=c("dotdash", "F1"))),
linetype = FALSE)
这是我所做的解释:
- 在
geom_point
中,将 colour
美学更改为 fill
。另外,将 colour="transparent"
放在 aes
之外。这将摆脱点周围的边界。如果您想要边框,请将其设置为您喜欢的任何边框颜色。
- 在
scale_colour_manual
中,我将颜色设置为蓝色和红色,但您当然可以将它们设置为您喜欢的任何颜色。
- 添加
scale_fill_manual
以使用 fill
美学设置点的颜色。
- 添加
scale_shape_manual
并将值设置为 21 和 24(分别为实心圆和三角形)。
- 里面的东西
guides()
都是修改图例的。我不确定为什么,但如果没有这些覆盖,填充和形状的图例是空白的。请注意,我为 shape
图例设置了 fill="black"
,但它显示为灰色。我不知道为什么,但没有 fill="somecolor"
shape
图例是空白的。最后,我覆盖了 colour
图例,以便在颜色图例中包含线型,这让我摆脱了多余的 linetype
图例。我对这个传说并不完全满意,但这是我在不求助于特殊用途的 grob 的情况下能想到的最好的。
注意: 我将 color=NA
更改为 color="transparent"
,因为 color=NA
(在 ggplot2 的版本 2 中)导致点完全消失,即使您使用具有单独边框和填充颜色的点。感谢@aosmith pointing this out.
为了完整起见 - 现在可以使用 ggnewscale
包
轻松地为相同的美学添加两个尺度
我的问题与 this and this and also this 的问题非常相似。我有一个散点图(使用 geom_point
),使用特定的调色板按一个因素着色。我正在使用 stat_smooth
通过点绘制某些平滑线,按 另一个 因子分组。我希望这些线条使用不同的调色板。
这是一个Dropbox link to some example data。做一个
currDT <- read.table("SO_data", sep = "|", header = TRUE, strip.white = TRUE)
我通常将我的数据放在 data.table 中,因此您可能会发现更改它也很有帮助。哦,这是我目前正在使用的配色方案,您可以使用 scale_colour_brewer
生成您自己的配色方案,为了完整起见,我将其包括在内。
my_col_scheme <- c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#7B8A7B",
"#0B29D6", "#f781bf", "#999999", "black")
希望这已经足够清楚了。这是一些示例代码:
icorr_elec <- ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
geom_point(aes(colour = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_colour_manual(values = my_col_scheme) +
stat_smooth(method = "lm", formula = y ~ x + 0, aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides"))
如何在 ggplot2 中完成此操作?从我收集到的其他问题中,我可以手动指定每一行,但我想避免这种情况。
我认为ggplot2
不会让你换两次颜色并更新图例。我似乎记得读过你不能在一个情节中改变 scale_color_manual
两次。我找不到那个网页,但是这个 post touches on the topic. I also tried using the suggestions in this post,但是没有用。可能是因为我们正在尝试混合 geoms
(但这只是一个猜测)。
我可以得到颜色回归线:
part1 <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"),
labels = c("Halides", "Non-Halides")) +
scale_color_manual(name = "", values = c("red", 'blue'),
labels = c("Halides", "Non-Halides"))
ggsave('part1.jpeg', part1)
part2 <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
geom_point(aes(color = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"),
labels =c("Halides", "Non-Halides")) +
scale_colour_manual(values = my_col_scheme)
ggsave('part2.jpeg', part2)
both <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides")) +
geom_point(aes(color = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_colour_manual(values = my_col_scheme)
ggsave('both.jpeg', both)
我认为您需要添加每一行手册。希望其他人知道如何回答这个问题,但我认为@hadley 不允许更改两种颜色。幸运的是,您 data.table
所以应该很容易做到:-)
对其他试图解决此问题的人发表评论希望我的部分回答能帮助您回答这个问题。另外,我的第三个图显示了 ggplot2
如何没有像 OP 想要的那样得到图例颜色 correct。作为另一个提示,您可以尝试使用图例选项。
您可以为点使用填充点标记并将其映射到 fill
美学,同时保持线映射到 colour
,从而为线和点获得单独的颜色映射审美的。实心点标记是编号为 21 到 25 的标记(参见 ?pch
)。这是一个例子,改编@RichardErickson 的代码:
ggplot(currDT, aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides")) +
geom_point(aes(fill = Anion, shape = Cation), size = 3, alpha = 0.4, colour="transparent") +
scale_colour_manual(values = c("blue", "red")) +
scale_fill_manual(values = my_col_scheme) +
scale_shape_manual(values=c(21,24)) +
guides(fill = guide_legend(override.aes = list(colour=my_col_scheme[1:8],
shape=15, size=3)),
shape = guide_legend(override.aes = list(shape=c(21,24), fill="black", size=3)),
colour = guide_legend(override.aes = list(linetype=c("dotdash", "F1"))),
linetype = FALSE)
这是我所做的解释:
- 在
geom_point
中,将colour
美学更改为fill
。另外,将colour="transparent"
放在aes
之外。这将摆脱点周围的边界。如果您想要边框,请将其设置为您喜欢的任何边框颜色。 - 在
scale_colour_manual
中,我将颜色设置为蓝色和红色,但您当然可以将它们设置为您喜欢的任何颜色。 - 添加
scale_fill_manual
以使用fill
美学设置点的颜色。 - 添加
scale_shape_manual
并将值设置为 21 和 24(分别为实心圆和三角形)。 - 里面的东西
guides()
都是修改图例的。我不确定为什么,但如果没有这些覆盖,填充和形状的图例是空白的。请注意,我为shape
图例设置了fill="black"
,但它显示为灰色。我不知道为什么,但没有fill="somecolor"
shape
图例是空白的。最后,我覆盖了colour
图例,以便在颜色图例中包含线型,这让我摆脱了多余的linetype
图例。我对这个传说并不完全满意,但这是我在不求助于特殊用途的 grob 的情况下能想到的最好的。
注意: 我将 color=NA
更改为 color="transparent"
,因为 color=NA
(在 ggplot2 的版本 2 中)导致点完全消失,即使您使用具有单独边框和填充颜色的点。感谢@aosmith pointing this out.
为了完整起见 - 现在可以使用 ggnewscale
包