Ggplot set colors separately for geom_point and geom_line manually

I would like to set the colors for line and points using color lists. Two questions:
1)how to have the geom_point use color_flag palette and the geom_line use the color_group palette?
2)When my current code is run, the legend combines the group and flag. How can I separate those in the legend or remove the flag entries.


library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.4.4
df<-read.table(header=TRUE, text='
               wk ht group flag
               1  5   A     1
               2  6   A     1
               3  7   A     1
               1  15  B     0
               2  17   B     0
               3  5   B     1
               1  6   C     1
               2  20   C     0
               3  3   C     1
               ')

color_flag <- c("red","green")
color_group <- c("blue","black","yellow")


 ggplot(df, aes(x=wk, y=ht))  +
      geom_point(aes(color=factor(flag)), size=3) +
      geom_line(aes(color=group))  

Created on 2018-09-07 by the reprex package (v0.2.0.9000).

To have two separate "color" mappings, use a filled point marker and then use the fill aesthetic for the points. For example, in the code below, we use shape=21, which is a filled circle. stroke=0 removes the black border around the markers. I've also changed one of the colors from "yellow" to "yellow2" to make it show up better. The scale_***_manual functions use your color palettes to set the colors:

color_group <- c("blue","black","yellow2")

ggplot(df, aes(x=wk, y=ht))  +
  geom_line(aes(color=group)) +
  geom_point(aes(fill=factor(flag)), size=4, shape=21, stroke=0) +
  scale_fill_manual(values=color_flag) +
  scale_colour_manual(values=color_group) +
  theme_classic() +
  labs(fill="Flag", colour="Group")

Rplot18

7 Likes

Thanks! This was just what I was looking for-and more! I have 2 follow-up items.:

  1. What does the appended '2' do to the color? Does that work for any color?

2)If I understand your explanation, the "stroke=0 " should remove the marker outlines, but mine still has them. Did I miss something? Here's what I did:

    library(ggplot2)
    #> Warning: package 'ggplot2' was built under R version 3.4.4
    df<-read.table(header=TRUE, text='
                   wk ht group flag
                   1  5   A     1
                   2  6   A     1
                   3  7   A     1
                   1  15  B     0
                   2  17   B     0
                   3  5   B     1
                   1  6   C     1
                   2  20   C     0
                   3  3   C     1
                   ')

    color_flag <- c("red","green")
    color_group <- c("blue","black","yellow2")


      ggplot(df, aes(x=wk, y=ht))  +
       geom_line(aes(color=group)) +
       geom_point(aes(fill=factor(flag)), size=4, shape=21, stroke=0) +
       scale_fill_manual(values=color_flag) +
       scale_colour_manual(values=color_group) +
       theme_classic() +
       labs(fill="Flag", colour="Group")      

Created on 2018-09-08 by the reprex package (v0.2.0.9000).

To answer your questions:

  1. What does the appended '2' do to the color? Does that work for any color?

There are a bunch of named colors that you can access in R (for example, see page 3 of this document). Many of these colors come in multiple shades, with names like "yellow1", "yellow2", etc., with higher numbers generally representing darker shades of the given color. If you type colors(), R will output a vector of all the color names. If you want to view some of these colors quickly, you can do, for example, library(scales); show_col(colors()[grep("yellow", colors())]).

2)If I understand your explanation, the "stroke=0 " should remove the marker outlines, but mine still has them. Did I miss something?

stroke sets the border thickness. I'm not sure why stroke=0 isn't working for you. However, you can also try:

geom_point(aes(fill=factor(flag)), size=4, shape=21, stroke=NA) +

or

geom_point(aes(fill=factor(flag)), size=4, shape=21, color="#00000000") +

The last two zeros in color="#00000000" make the color transparent, equivalent to alpha=0.

3 Likes

That solution works best for my case.
The stroke=0 is not recognized by certain application to render the picture.
stroke = NA worked, but it is not possible to adjust the size of the point
Best solution for me is to set the stroke color as transparent.