Why does geom_vline not inherit the colour scale palette?

Hi All,

In the below example, why does the vline not get coloured blue? I would have thought the colour scale would be inherited by all layers with the col aesthetic.

I understand that I could add a col = "blue" argument to geom_vline, but I am trying to get my head around how colour scales work.

library(tidyverse)
library(palmerpenguins)

penguins %>% 
  ggplot(aes(flipper_length_mm, body_mass_g, colour = "1")) +
  geom_point() +
  geom_vline(xintercept = 200) +
  scale_color_manual(values = "blue") 

Thanks
David

I think the answer is that geom_vline does not consider any aesthetics, it just draws a line given the intercept.

Maybe you need specify the exactly color and other options, like colour, linetype and size.

library(tidyverse)
library(palmerpenguins)

penguins %>% 
  ggplot(aes(flipper_length_mm, body_mass_g, colour = "1")) +
  geom_point() +
  geom_vline(xintercept = 200, colour="#62CC10", linetype="dashed", size= 1.5) +
  scale_color_manual(values = "blue") 

1 Like

When you provide the xintercept argument, outside of an aes() mapping, geom_vline() behaves like an annotation, not like a mapped geom, so, it ignores any mapping definition. You would need to pass the colour property as an argument, as you would do with an annotation.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.