Manu
Manu

Reputation: 1120

Set color scheme in global options on Rmarkdown R

I want to set a global color scheme in Rmarkdown, I read that this is possible with options but I tried this without success:

---
title: "Iris"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(ggplot2.continuous.color = "viridis")
options(ggplot2.continuous.fill = "viridis")
options(ggplot2.discrete.fill = "viridis")
```

```{r}
library(tidyverse)
iris %>% ggplot(aes(Sepal.Length, Sepal.Width, color = Species)) +
   geom_point()
```

The plot I get uses the default color scheme, and I want the viridis scheme. Any help will be greatly appreciated.

Upvotes: 1

Views: 1497

Answers (1)

duckmayr
duckmayr

Reputation: 16940

Following this excellent blog post by Jim Hester, you can reassign to the default scale_colour_*() functions the scales you want:

---
title: "Iris"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
scale_colour_continuous <- scale_colour_viridis_c
scale_colour_discrete   <- scale_colour_viridis_d
scale_colour_binned     <- scale_colour_viridis_b
```

```{r}
iris %>% ggplot(aes(Sepal.Length, Sepal.Width, color = Species)) +
   geom_point()
```

enter image description here

Upvotes: 4

Related Questions