我的问题非常类似于this https://stackoverflow.com/questions/15363035/ggplot2-how-to-specify-multiple-fill-colors-for-points-that-are-connected-by-li and this https://stackoverflow.com/questions/14757394/overplotting-with-different-colour-palettes-in-ggplot2并且this https://stackoverflow.com/questions/9427950/how-can-i-use-different-color-palettes-for-different-layers-in-ggplot2问题。我有一个散点图(使用geom_point
)使用特定的调色板按某个因素着色。我在用着stat_smooth
通过点绘制某些平滑线,按以下方式分组another因素。我希望这些线条使用不同的调色板。
这里有一个Dropbox 链接到一些示例数据 https://www.dropbox.com/s/08zponb87c7mq59/SO_data?dl=0。只需做一个
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 中如何做到这一点?从我收集的其他问题中,我可以手动指定每一行,但我想避免这种情况。