Different color palettes for two different color aesthetic displays in ggplot2

My question is very similar to this and this , as well as this question. I have a scatter chart (using geom_point ) painted with a multiplier using a specific color palette. I use stat_smooth to draw specific smoothing lines through dots grouped by another factor. I would like these lines to use a different color palette.

Below is a Dropbox link to some sample data . Just follow

 currDT <- read.table("SO_data", sep = "|", header = TRUE, strip.white = TRUE) 

I usually have data in a data table. Thus, you can also find useful changes. Oh, and here is the color scheme I'm using at the moment, you can use scale_colour_brewer to create your own, I just turn it on for completeness.

 my_col_scheme <- c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#7B8A7B", "#0B29D6", "#f781bf", "#999999", "black") 

Hope this is clear enough. Here is a sample 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 that I collected, I could specify each line manually, but I would like to avoid this.

+8
colors r ggplot2
source share
2 answers

You can get separate color comparisons for lines and dots with a filled point marker for dots and displays that match the fill aesthetics, while preserving the lines mapped to colour aesthetics. Filled point markers are numbers from 21 to 25 (see ?pch ). Here is an example adapting @RichardErickson code:

 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) 

Here is an explanation of what I did:

  • In geom_point change the colour value to fill . Also, put colour="transparent" outside of aes . This will get rid of the border around the glasses. If you need a border, set it to any border color you prefer.
  • In scale_colour_manual I set the colors to blue and red, but of course you can customize them the way you like.
  • Add scale_fill_manual to set point colors using fill aesthetics.
  • Add scale_shape_manual and set the values ​​to 21 and 24 (filled circles and triangles respectively).
  • All contents inside guides() should change the legend. I'm not sure why, but without these overrides, the legends to fill out and the forms are empty. Notice that I set fill="black" for the shape legend, but it appears gray. I don’t know why, but without fill="somecolor" shape legend is empty. Finally, I redefined the colour legend to include the line style in the color legend, which allowed me to get rid of the excess linetype legend. I am not entirely happy with the legend, but it was the best I could come up with without resorting to targeted craps.

enter image description here

NOTE. I changed color=NA to color="transparent" , since color=NA (in version 2 ggplot2) causes the dots to completely disappear, even if you use a dot with a separate border and fill the colors. Thanks @aosmith for pointing this out .

+10
source share

I do not think ggplot2 will allow you to change the color twice and update the legend. I seem to recall that you cannot change scale_color_manual twice in the plot. I could not find this web page, but this post touches on the topic. I also tried using the sentences in this article , but that didn't work. Probably because we are trying to mix geoms (but this is just an assumption).

I can get either regression color:

 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) 

enter image description here Or the data indicates a graph:

 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) 

enter image description here But not both:

 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) 

enter image description here

I think you will need to add each linear instruction. I hope someone else knows how to answer this, but I don't think @hadley allows me to change both colors. Fortunately, you are data.table , so this should be easy to do data.table

Comment so that someone else tries to solve this problem . I hope my partial answer helps you answer this question. Also, my third image shows how ggplot2 doesn't get the legend color as the OP wants it. As another tip, you can try playing with the legendary options.

+3
source share

All Articles