My question is very similar to this and this and also this question. I have a scatterplot (using geom_point
) coloured by a factor, using a particular colour palette. I'm using stat_smooth
to draw certain smoothing lines through the points, grouped by another factor. I'd like these lines to use a different colour palette.
Here is a Dropbox link to some example data. Just do a
currDT <- read.table("SO_data", sep = "|", header = TRUE, strip.white = TRUE)
I usually have my data in a data.table, so you might find it helpful change that as well. Oh and here is the colour scheme I'm using at the moment, you can use scale_colour_brewer
to generate your own, I'm just including this for completeness.
my_col_scheme <- c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#7B8A7B",
"#0B29D6", "#f781bf", "#999999", "black")
Hopefully that is clear enough. Here is some example code:
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"))
How can this be done in ggplot2? From the other questions I gathered I could specify each line manually, but I'd like to avoid that.
See Question&Answers more detail:
os