Why does a theme not change all the colors of the data points in a Markdown?

Hi all,

I have general difficulty with unterstanding coloring data points in R:


  1. It is absolutely brilliant and understandable that attributes in the same script always get assigned the same color. However, if I execute the script again in another file, the plots again have the same colors. How can this be? Does R have an consistent algorithm here or a global overview of the coloring across different files?

  1. Does anyone have experience why a theme does not change the color of data points for certain graphics? No matter if I use ggthemes(), ggthemr() or ggtech(), all graphics of my whole Markdown are changed beautifully and consistently, except for the data points of the always same graphics. It is as if these were once set by R and protected.

Does anyone know if I have a general thinking problem? As an example, I ran my code with the diamond dataset (see code and graphs below). I run different themes and the colors of the different sections always stayed the same. But with all other graphics like barplots etc. the colors of the fillings changed with the themes as it should be.

Does anyone knows how to change the colors of an attribute consistently?


Thank you a lot and have a nice weekend :slight_smile:


library(tidyverse)
library(ggthemes)

data <- diamonds

data %>%
  select(carat, price, cut) %>% 
  filter(cut == c("Fair", "Good", "Very Good")) %>%
  na.omit() %>%
  ggplot(aes(x = carat, y = price, color = cut)) +
  geom_point(size = 2) + 
  theme_wsj() +
  scale_x_continuous(name = "\ncarat") +
  scale_y_continuous(name = "price\n\n") +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20),
        legend.title = element_blank(),
        legend.position = "top",
        legend.key.size = unit(1, 'cm'),
        legend.text = element_text(size=14))






With another theme:

library(tidyverse)
library(ggthemes)

data <- diamonds

data %>%
  select(carat, price, cut) %>% 
  filter(cut == c("Fair", "Good", "Very Good")) %>%
  na.omit() %>%
  ggplot(aes(x = carat, y = price, color = cut)) +
  geom_point(size = 2) + 
  theme_classic() +
  scale_x_continuous(name = "\ncarat") +
  scale_y_continuous(name = "price\n\n") +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20),
        legend.title = element_blank(),
        legend.position = "top",
        legend.key.size = unit(1, 'cm'),
        legend.text = element_text(size=14))

-> the result will have the same colors as above

Unfortunately I can just show one graph because I am a new user :confused:

Themes do not affect the aesthetic mapping, if you want to modify it use any of the scale_color_() functions as needed, for example, if you want to specify colors yourself use scale_colot_manual().

1 Like

Thanks. I did not know that :slight_smile:

That means when something is plotted, R selects an random color combination (from ggplot?) which can only be overwritten with scale_color_() ? Otherwise this combination is set?

Not exactly, when the color or fill aesthetics are mapped ggplot2 draws colors from default pallets (depending on the variable class the aesthetic is mapped to) unless you explicitly specify otherwise by modifying the corresponding scale.

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.