How do I get my legend dots blue and red instead that both are red?

Viewed 111

I am trying to get a red and a blue dot in my legend, this is not the data that I am using but a reproducible example,

The graph exits of two dataset, example is one of them and example1.

here is my code:

    if (!require("pacman")) install.packages("pacman")
    pacman::p_load_gh("trinker/wakefield")
    
    
    


    example <- r_data_frame(
        n = 100,
        id,
        iq,
        age,
        height
        )
  
 
    example1 <- r_data_frame(
        n = 100,
        id,
        iq,
        age,
        height)
    
    
   
    
    library(ggplot2)
    color_names <- c("example", "example1")
    color_values <- c("blue", "red")
    names(color_values) <- color_names
    ggplot() + 
       
 #These points need to be blue and in the legend as well.     
        geom_point(data=example, aes(x=ID, y=Height,
        fill ="example"), 
        colour="darkblue", size=1) +
    
   #These points need to be red and red in the legend 
      geom_point(data=example1, aes(x=ID, y=Height, 
        fill ="example1"), colour = "red"
        , size=1) +
      
      # plot configuration scales, theme, etc...
      scale_colour_manual(values = color_values) +
      scale_fill_manual(values = color_values) +
      theme_bw()
2 Answers

The main issue here is that you are setting the colour using the color names for the points but also overwriting these settings with scale_colour_manual and scale_fill_manual.

As you are using geom_point() it is not necessary to change the fill unless you use a different point type.

I would suggest labelling the colours in aes() with the examples (not just fill). My solution removes all excess scales I can add these back in if you need them in your actual implementation (not the reprex as it is unneccessary here).

I have additionally modified the colour_values variable to contain the name and the colour in one line (rather than your implementation).

if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh("trinker/wakefield")

example <- r_data_frame(
  n = 100,
  id,
  iq,
  age,
  height
)
#> Warning: `tbl_df()` was deprecated in dplyr 1.0.0.
#> Please use `tibble::as_tibble()` instead.

example1 <- r_data_frame(
  n = 100,
  id,
  iq,
  age,
  height)

library(ggplot2)
color_values <- c("example" = "darkblue", "example1" = "red")
ggplot() + 
  
  #These points need to be blue and in the legend as well.     
  geom_point(data=example, aes(x=ID, y=Height,color = "example"),  size=1) +
  
  #These points need to be red and red in the legend 
  geom_point(data=example1, aes(x=ID, y=Height, 
                                color = "example1"), size=1) +
  
  # plot configuration scales, theme, etc...
  scale_colour_manual(name = "colour", values = color_values) +
  theme_bw()

Created on 2021-04-07 by the reprex package (v2.0.0)

Taking Joel Kandiah's answer a step further (+1), I'd use a more natural way for ggplot2 to make use of their fantastic aesthetics - use a long format if your data frames contain the same variables. Just bind them together.

Avoids headaches and hard coding, and also reduces your code.

library(wakefield)
library(tidyverse)

example <- r_data_frame(n = 100, id, iq, age, height)
#> Warning: `tbl_df()` was deprecated in dplyr 1.0.0.
#> Please use `tibble::as_tibble()` instead.
example1 <- r_data_frame(n = 100, id, iq, age, height)

color_values <- c(example = "darkblue", example1 = "red")

bind_rows(mget(ls(pattern = "^example")), .id = "example") %>%
ggplot() + 
  geom_point(aes(x=ID, y=Height,color = example),  size=1) +
  scale_colour_manual(name = "colour", values = color_values) +
  theme_bw()

Created on 2021-04-07 by the reprex package (v1.0.0)

Related