Set a theme and palette for all plots - r

I'm trying to simplify my plots in ggplot2. Suppose I want to create a scatterplot from iris dataset:
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point()
But suppose I don't like ggplot2 default theme and palette. Let's say I want to use theme_bw and Dark2 palette:
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point() +
theme_bw() +
scale_color_brewer(palette="Dark2")
And suppose I have lots of plots and I want all of them using theme_bw and Dark2 palette. I know I can use theme_set(theme_bw()) to make all my plots have the black and white theme. Is there a similar function to make all my plots use Dark2 palette? In other words, how can I run a code like
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point()
and have theme_bw and Dark2 palette in all my plots?

One solution would be to write a custom wrapper:
ggcust <- function(...){
ggplot(...) +
theme_bw()
}
fill in all the theme options you need, then use it like this:
ggcust(data = mtcars, aes(x = mpg, y = cyl)) +
geom_point()

You can also put layers into a list:
gglayer_theme <- list(
theme_bw(),
scale_color_brewer(palette="Dark2")
)
And treat the list like a new layer (Note + becomes , in this list notation):
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point() +
gglayer_theme
The advantage to the custom wrapper approach is the ability to easily mix layers:
gglayer_labs <- list(
labs(
x = "x",
y = "y"
)
)
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point() +
gglayer_theme +
gglayer_labs
Or combine them beforehand:
gglayer_all <- c(gglayer_theme, gglayer_labs)
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point() +
gglayer_all

Related

How to switch facet_grid to facet_wrap

Context: I am trying to convert facet_grid to facet_wrap to add ncols. I know the cols=conference will change, but I am not sure how.
graph <- ggplot(data, aes(week, positivity_rate, color=school)) +
geom_line() +
facet_grid(cols=vars(conference))
graph
library(tidyverse)
ggplot(iris, aes(Petal.Length, Petal.Width, color=Species)) +
geom_line() +
facet_grid(cols=vars(Species))
ggplot(iris, aes(Petal.Length, Petal.Width, color=Species)) +
geom_line() +
facet_wrap(~Species,ncol = 2)

Facet, plot axis on all outsides

Is there a way to mirror the x-/y-axis to the opposite side (only outside, no axis on the inside)? My goal is to get something like:
MWE
library(ggplot2)
ggplot(data = iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
facet_grid(rows = "Species")
This should work:
ggplot(data = iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~.*1))+
scale_x_continuous(sec.axis = sec_axis(~.*1))+
facet_grid(rows = "Species")
Yes, you can use the guides() function to set axes for secondary positions as well, see example below. To have it more like your example, the strip placement should be changed to "outside".
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
facet_grid(rows = vars(Species)) +
guides(
x.sec = "axis", y.sec = "axis"
) +
theme(strip.placement = "outside")
Created on 2022-06-07 by the reprex package (v2.0.0)

ggplot color by factor levels, some color are very similar, unable to distinguish points for some groups

I used the following code to get the graph. The problem of this graph is the colors of d=330421, d=330500 and d=330623 are very similar, it is hard to distinguish which point belongs to which group.
ggplot(subdt2, aes(x=year, y=aniHusb, color = d)) + geom_point()
You could try this to generate the plot with distinguishable colors:
library(randomcoloR)
n <- length(unique(subdt2$d))
palette <- unname(distinctColorPalette(n))
ggplot(subdt2, aes(x=year, y=aniHusb, color = d)) +
geom_point() + scale_color_manual(values=palette)
or Using RColorBrewer package:
library(RColorBrewer)
ggplot(subdt2, aes(x=year, y=aniHusb, color = d)) +
geom_point() + scale_colour_brewer(palette = "Set3")
Example:
ggplot(iris[iris$Species=='setosa',], aes(x=Sepal.Length, y=Sepal.Width, color = as.factor(Petal.Length))) +
geom_point() + scale_colour_brewer(palette = "Set3")
Plot:
n <- length(unique(iris[iris$Species=='setosa','Petal.Length']))
palette <- unname(distinctColorPalette(n))
ggplot(iris[iris$Species=='setosa',], aes(x=Sepal.Length, y=Sepal.Width, color = as.factor(Petal.Length))) +
geom_point() + scale_color_manual(values=palette)

How to reverse the default color palette for ggplot2

I am trying to reverse the color map for a plot using scale_color_brewer(direction = -1). However, doing so also changes the palette.
library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()
# reverse colors
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
scale_color_brewer(direction = -1)
Potential solution
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
scale_color_brewer(direction = -1, palette = ?)
The default color palette ggplot uses is scale_color_hue.
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()
is equivalent to
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point() + scale_color_hue(direction = 1)
direction = -1 does reverse the colors. But then you need to adjust the starting point in the hue wheel, in order to get the same three colors in reversed order.
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
scale_color_hue(direction = -1, h.start=90)
Each color moves the hue pointer 30 degrees. So we set the starting point at 90.
BTW, in order to let scale_colour_brewer work for categorical variables, you need to set type = 'qual':
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
scale_color_brewer(type = 'qual', palette = 'Dark2')
We can use the hue_pal function from the scales package to get the name of the colors. After that, use scale_color_manual to specify the color with rev to reverse the order of the colors from hue_pal.
library(ggplot2)
library(scales)
# Get the colors with 3 classes
cols <- hue_pal()(3)
# Plot the data and reverse the color
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point() +
scale_color_manual(values = rev(cols))
I would use scale_color_manual() for more control. Here are two versions with reversed color maps.
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
+ scale_color_manual(values = RColorBrewer::brewer.pal(3,'Blues'))
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+
+ scale_color_manual(values = rev(RColorBrewer::brewer.pal(3,'Blues')))

ggplot stat_smooth: change look of multiple bands

I'm trying to customize the look of multiple loess plots within the same graph for different levels of a group var. I looked at this post, but wasn't able to make it work:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(method = "loess")
I'd like to change the color of each band and line.
You can specify the looks with for example the scale_color_manual scale. In the example below I also used override.aes within guides to get a nice legend as well:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(aes(fill=Species), method = "loess", size=1) +
scale_color_manual(values = c("green","blue","red")) +
scale_fill_manual(values = c("green","blue","red")) +
scale_linetype_manual(values = c("dashed","dotted","solid")) +
theme_bw() +
guides(fill=guide_legend(override.aes = list(fill="white",size=1.2)))
this gives:
Other alternatives to the manual scales are the hue and brewer scales.
I added size = 2 so you can see that the line type is different for each line:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(method = "loess", aes(fill = Species), size= 2)

Resources