In the other examples, we presented only basic customization of ggpubr and ggplot2 graphics. However, using both libraries, we can customize practically every element of a plot. Here, we will modify the look of our ggpubr and ggplot2 charts.
Changing outline color, fill color, and opacity
Use the basic box plot code:
# Creating exemplary box plot with ggpubr:
library(ggpubr)
library(tidyverse)
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label", y = "SM 41:1;O2")
# Creating exemplary box plot with ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label, y = `SM 41:1;O2`))+
geom_boxplot()
We obtain raw box plots (black and white):
To modify colors of outline, filling, and opacity, we need to set the following parameters:
color - influences the color of the plot outline,
fill - influences the color of the plot filling,
alpha - influences the opacity.
Suppose we want the color of the outline of all box plots to be violet, while the box plot fill color should be pink:
# Changing the color of box plots outline and filling:
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
color = 'violet',
fill = 'pink')
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(color = 'violet', # We add colors to appropriate layer!
fill = 'pink')
And we obtain:
We can also set the color of the outline to dark blue and the filling to dark blue, but with alpha set to 0.4 (to make the filling partially transparent):
# Changing box plots opacity.
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
color = 'darkblue',
fill = 'darkblue',
alpha = 0.4)
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(color = 'darkblue', # Remember about appropriate layer again!
fill = 'darkblue',
alpha = 0.4)
The modified plot:
If you want to keep the box plots empty inside, just set the fill to NA:
# Changing box plots opacity.
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
color = 'darkblue',
fill = NA)
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
alpha =)) +
geom_boxplot(color = 'darkblue',
fill = NA)
The effect of removing box plots fill color:
There is one more way we can use color and fill parameters. If we provide color or fill with
a grouping variable (discrete variable) - every group will be automatically assigned a different color. In our case, a grouping variable is stored in the Label column. This way, we can differentiate box plots by biological groups:
# Different color outline, depending on biological group:
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
color = "Label")
## ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
color = Label)) +
geom_boxplot()
We obtain:
In the case of filling, we slightly modify the code:
# Box plots filling color by biological groups:
# ggpubr
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label")
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot()
The output:
NOTE: In the case of ggplot2, if we want to set different colors for every discrete variable (e.g., for each biological group), we can add the color-related aesthetics (color = factor_variable or fill = factor_variable) to the main aesthetics of a plot or the aesthetics of a particular layer. It means that:
# This code:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = `Label`)) +
geom_boxplot()
# and this code:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(aes(fill = `Label`))
# ...will produce the same plot!
However, there is a difference between these two pieces of code. If the fill is added to the main plot aesthetics, it will be inherited by every newly added layer automatically. In turn, fill = Label specified in the aesthetic of one selected layer (geom_boxplot) results in applying this discrete variable to fill color only in this single layer - in the second example above - to the box plots only. If you would add another layer, e.g. geom_point(), it would be empty in this case (without fill color).
Remember: In ggplot2, if you do not want to inherit the main plot aesthetics in a selected layer, you can always set inherit.aes to FALSE and specify new aesthetic mappings for this individual layer. You can recheck the example with dot plots (Basic plotting in R - Dot plots in ggplot2).
Although our box plots are colored or filled according to the biological group, we would like to select custom colors of outlines (or fill colors) in the next step. The ggpubr functions, e.g. ggboxplot, ggbarplot, etc. - contain the argument palette, which expects a vector with colors. This way, we can easily modify the ggpubr charts' look according to our preferences.
The ggplot2 library has a different solution. Here, we need to use scale_color_manual() or scale_fill_manual() if we want to customize the outlines' colors or plot fill colors according to our preferences. The plot obtained through ggpubr functions can also be customized using scale_color_manual() or scale_fill_manual(). In scale_color_manual() or scale_fill_manual(), we use the argument values where in the form of a vector we specify the color names or color codes.
Take a look at the following code. Let's assume we want to color (fill) our box plots with royal blue, orange, and red2:
# Assinging outlines' colors to biological groups:
# ggpubr:
# Method 1:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
color = "Label",
palette = c('royalblue',
'orange',
'red2'))
# Method 2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
color = "Label") +
scale_color_manual(values = c('royalblue',
'orange',
'red2'))
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
color = Label)) +
geom_boxplot() +
scale_color_manual(values = c('royalblue',
'orange',
'red2'))
The output:
In the case of filling:
# Assinging outlines' colors to biological groups:
# ggpubr:
# Method 1:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label",
palette = c('royalblue',
'orange',
'red2'))
# Method 2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_manual(values = c('royalblue',
'orange',
'red2'))
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot() +
scale_fill_manual(values = c('royalblue',
'orange',
'red2'))
The output:
NOTE: It is also possible to change the opacity according to the biological group, however, it is not advised to use alpha with a discrete variable like Label. Hence, we will not continue with adjusting transparency through a discrete variable.
If you cannot decide about the outline color or fill color - no worries. Take a look at the R solution proposed below. You will find colors or palettes that will improve the presentation of your plot.
R has a lot to offer!
Let's start with scale_color_hue() and scale_fill_hue(). These functions will map your outline color or fill color to evenly spaced hues on the color wheel. It is a great solution if you want to color outline or fill by biological groups. The scale_color_hue() (or scale_fill_hue()) can be adjusted through 3 parameters:
h (ranges of colors in use - from 0 to 360),
c (chroma, intensity of a color),
l (luminance, lightness from 0 to 100).
Using these three values, one can specify in what range of colors are you interested, e.g.:
# Using scale_fill_hue():
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_hue(h = c(270, 360), c = 100, l = 40)
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot() +
scale_fill_hue(h = c(270, 360),
c = 100,
l = 40)
The output:
Sometimes simple solutions are effective. Try scale_color_grey() and scale_fill_grey()! The colors of outlines or fill will be connected to the greyscale:
# Using grey scales:
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label",
shape = "Label",
color = 'red2',
add = 'jitter',
add.params = list(color = 'red2')) +
scale_fill_grey(start = 0.2, end = 0.7)
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(color = 'red2',
outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
color = 'red2',
shape = 5) +
scale_fill_grey(start = 0.2, end = 0.7) +
theme_bw() # Select a different background
The output:
In the next step, you can test different color palettes offered by R! Try:
RColorBrewer package:
ggsci R package:
wesanderson package:
viridis package:
First, we need to install packages containing these palettes:
# Installing libraries with new palettes:
packages <- c("RColorBrewer", "ggsci", "wesanderson", "viridis")
install.packages(packages)
We will begin with RColorBrewer. First, let's check what palettes are available through this library:
IMPORTANT: Many scientific journals suggest using colorblind-friendly palettes. Remember, colorblind-friendly palettes make your charts more accessible. This way, you ensure that people with visual impairments can understand the plot design. You can also convey your scientific message to a broader group of readers!
If you want to browse a particular palette, or get the color codes, use:
You can use these colors through scale_color_manual() or scale_fill_manual(). Otherwise, you can try scale_color_brewer() or scale_fill_brewer() to select a specific palette:
# Using RColorBrewer:
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_brewer(palette = "Oranges", direction = -1)
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
scale_fill_brewer(palette = "Dark2")
# ggplot2 with selected colors from RColorBrewer:
RColorBrewer::brewer.pal(9, 'YlOrRd')
# We pick #E31A1C for T.
RColorBrewer::brewer.pal(12, 'Paired')
# We pick #FF7F00 for PAN.
RColorBrewer::brewer.pal(9, 'YlGnBu')
# We pick #081D58 for N.
# Now, we assemble a vector with our colors
colors <- c("#081D58", "#FF7F00", "#E31A1C")
# Creating ggplot with our selected colors:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_manual(values = colors) +
theme_bw()
The output:
Let's try the amazing package ggsci. We call the library first:
# Calling the library
library(ggsci)
The ggsci offers palettes inspired by colors appearing in:
scientific journals,
data visualization libraries,
science fiction movies, and TV shows.
We will test here some of them. According to the package vignette, the color scales can be simply accessed as scale_color_<palette_name>() or scale_fill_<palette_name>(). For example, if one plans to send a manuscript to the Lancet, a lancet palette can be used, we create scale_color_lancet() or scale_fill_lancet(). See more examples below:
# Testing ggsci palettes
# Plot 1 - Lancet
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_lancet()
# Plot 2 - NPG
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_npg()
# Plot 3 - Frontiers
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_frontiers()
# Plot 4 - Star Trek
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_startrek() +
theme_bw()
# Plot 5 - Futurama
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_futurama() +
theme_bw()
# Plot 6 - Rick & Morty
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_rickandmorty() +
theme_bw()
# Plot 7 - Simpsons
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_simpsons() +
theme_bw()
# Plot 8 - Tron Legacy
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_tron() +
theme_dark()
# Plot 9 - selected from Lancet
install.packages("scales") # We will use the scales library to review palettes.
show_col(pal_lancet("lanonc")(20))
# #00468BFF for all N
# #FDAF91FF for all PAN
# #ED0000FF for all T
colors <- c("#00468BFF", "#FDAF91FF", "#ED0000FF")
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_manual(values = colors) +
theme_bw()
# Plot 10 - selected from Simpsons
show_col(pal_simpsons("springfield")(20))
# #71D0F5FF for all N
# #FED439FF for all PAN
# #C80813FF for all T
colors <- c("#71D0F5FF","#FED439FF", "#C80813FF")
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_manual(values = colors) +
theme_bw()
The outputs:
Look at Plot 9 and Plot 10. There is an interesting trick presented, which may simplify finding interesting colors for you. We installed library scales and used the function show_col(). Next, we put in the function the palette generator (you can find it in the Introduction to the ggsci), for example, for Frontiers, we have pal_frontiers(), and inside we specify palette type, here: "default". Behind it, in parentheses, we specify how many colors we would like to visualize. If we turn it all into code we have:
# Investigating palettes.
# Calling library:
library(scales)
# Bulding visualization of the Frontiers palette:
show_col(pal_frontiers("default")(10))
We obtain:
# We could create a similar code for RColorBrewer.
# If we investigate RColorBrewer...
RColorBrewer::brewer.pal()
# ... we find brewer.pal() function.
# It is similar to pal_frontiers().
# brewer.pal() expects a number and the name of a palette.
# Hence:
show_col(brewer.pal(10, "Oranges"))
We obtain:
The next library is called wesanderson. The wesanderson package gives you the possibility to style your plots in palettes used in movies made by the famous director Wes Anderson. How to use this palette? First, we need to call the library:
# Calling a library with palette:
library(wesanderson)
If you want to use palettes from this package, you start with our regular scale_color_manual() or scale_fill_manual(). Then, using argument values you indicate that you want to use wesanderson's palettes:
# Using wesanderson's palette:
... +
scale_fill_manual(values = wes_palette())
In the wes_palette, you need to specify:
the name of the palette, use for example: "BottleRocket1", "BottleRocket2", "Rushmore1", "Royal1", "Royal2", "Zisson1", "Darjeeling2", "Chevalier1", "FantasticFox1", "Moonrise1", "Moonrise2", "Moonrise3", "Cavalcanti1", "GrandBudapest1", "GrandBudapest2".
type of variable - in our case, it is discrete (Label)
n - number of colors you need.
For instance, if you run:
# Checking the colors in the FantasticFox 1 palette for a discrete variable:
wes_palette(name = "FantasticFox1", type = 'discrete', n = 5)
You obtain:
If you want to check all color codes, use:
# Checking color codes for wesanderson's palettes:
wes_palettes
Let's use wesandersons's palette to fill our box plots. We will select the GrandBudapest1 palette:
# Using wesanderson's palettes:
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_manual(values = wes_palette(name = "GrandBudapest1",
n = 4,
type = 'discrete'))
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_manual(values = wes_palette(name = "GrandBudapest1",
n = 4,
type = 'discrete')) +
theme_bw()
The obtained plots:
We can also pick specific colors from a selected palette, e.g.: from the Zissou1 palette, we select:
All N - #3B9AB2,
All PAN - #EBCC2A,
All T - #F21A00.
# Using picked colors from the Zissou1 palette (wesanderson):
colors <- c("#3B9AB2", "#EBCC2A", "#F21A00")
# ggpubr:
# Method 1
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_manual(values = colors)
# Method 2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label",
palette = colors)
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_manual(values = colors) +
theme_bw()
The plots:
The last package is called viridis. First, we call the viridis library:
# Calling viridis library:
library(viridis)
The application of viridis is very straightforward. Simply use scale_color_viridis() or scale_fill_viridis(). Using the option argument, you can select a specific color option, which is offered by viridis (go to the Introduction to the viridis color maps to browse the available palettes). Below, please find examples of code:
# Using viridis palettes:
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
scale_fill_viridis(option = "viridis", discrete = T)
# Remember, that our `Label` is a discrete variable, discrete must be set to TRUE!
# Otherwise, this code will result in an error.
# ggplot2:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_viridis(option = "magma", discrete = T) +
theme_bw()
The plots:
If you would like to pick a specific color (e.g., by color code), you can again run show_col() from the scales package. Just, add the name of the palette from the viridis package and the number of colors you would like to obtain:
# Extracting color codes from the viridis' palettes:
show_col(turbo(20))
show_col(viridis(10))
show_col(inferno(5))
Until now, we set the color of the plot outline or plot fill using the discrete variables from the Label column. But, imagine: we would like to color/fill the points according to the increase in lipid concentration (a continuous scale). We can use for this purpose:
The first four of them create a two-color gradient. The scale_color_gradient2(), and scale_fill_gradient2() create a three-color gradient with a selected midpoint. The scale_color_gradientn() and scale_fill_gradientn() work with n-color gradients.
Let's prepare some plots with continuous color scaling. The simplest, scale_fill_gradient(), will expect two colors: one for low values (argument low), and a second for high values (argument high). See the examples below:
# Box plots with observations as dots.
# Filling points according to increasing concentration of a lipid:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(fill = 'grey',
outlier.shape = NA) +
scale_fill_brewer() +
geom_point(aes(fill = `SM 41:1;O2`),
position = position_jitter(width = 0.1),
shape = 21,
size = 4) +
scale_fill_continuous(low = 'blue', high = 'red2') +
theme_bw()
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(fill = 'grey',
outlier.shape = NA) +
scale_fill_brewer() +
geom_point(aes(fill = `SM 41:1;O2`),
position = position_jitter(width = 0.1),
shape = 21,
size = 4) +
scale_fill_gradient(low = 'blue', high = 'red2') +
theme_bw()
The obtained plot:
IMPORTANT: Both codes from above result in the same plot!
We can also fill a histogram according to counts or density (if we co-plot with a density plot). See the example below:
# Computing mean value to be co-plotted:
mean <- data %>%
filter(Label == "T") %>%
select(`SM 34:1;O2`) %>%
summarise(mean = mean(`SM 34:1;O2`))
# Creating ggplot2 histogram.
# Filling with color according to density.
data %>%
filter(Label == "T") %>%
ggplot(aes(x = `SM 34:1;O2`)) +
geom_histogram(aes(fill = ..density..,
y = ..density..),
position = 'identity',
bins = 25,
color = 'black')+
geom_density(linewidth = 2,
color = 'violet',
linetype = 'dashed') +
geom_vline(data = mean,
aes(xintercept = mean),
linetype = 'dashed',
linewidth = 1.5,
color = 'red2') +
scale_fill_gradient(low = 'red2', high = 'blue') +
theme_bw()
The obtained plot:
Now, let's try creating a correlation heat map in ggplot2 so we can test the scale_***_gradient2() and scale_***_gradientn() functions. Here, we will rely on correlations as a continuous scale. First, we again need to compute a correlation matrix:
We need to install the reshape2 library, which delivers a function called melt(). Using melt(), we can change the wide correlation matrix into a long one (in one line of code!):
# Install reshape2:
install.packages("reshape2")
# Calling library:
library(reshape2)
# Consulting the documentation:
?melt()
# Melting a wide correlation matrix into a long correlation matrix:
long.corr.matrix <- melt(corr.matrix)
Take a look at the figure below to understand what the reshaping of a wide correlation matrix looks like:
Using the long correlation matrix, we can create a correlation heat map through ggplot2 (with geom_tile()):
# Correlation heat map in ggplot2:
ggplot(long.corr.matrix, aes(x = Var1, y = Var2, fill = value)) +
geom_tile()
We can now test our fill color scaling. Let's try with with scale_fill_gradient2() first. As low - we set 'blue', as mid - 'white', and as high - 'red':
# Correlation heat map with customized color scaling:
ggplot(long.corr.matrix, aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low = 'blue', mid = 'white', high = 'red')
Such a simple code results in this beautiful correlation heat map:
Using scale_fill_gradientn(), we could select more colors to further improve the look of our plot. Look at this example:
# Correlation heat map with customized color scaling (scale_fill_gradientn()):
# Step - 1: Select colors:
colors <- c("#002060", "#0d78ca", "#00e8f0", "white", "#FF4D4D", "red", "#600000")
# Step - 2: Create plot:
ggplot(long.corr.matrix, aes(x = Var1, y = Var2, fill = value)) +
geom_tile()
# Step - 3: Add color scaling:
ggplot(long.corr.matrix, aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gradientn(colours = colors, limits = c(-1,1))
New colors are delivered as a vector to the colours (or colors) argument. We need to set limits to -1 and 1 (correlation ranges), so our scale is spread within this range. We produce such a fine plot:
Except for selected colors, we can again test color scales, e.g. from the ggsci package (let's try the beautiful color scale - GSEA GenePattern Color Scales):
# Correlation heat map with customized selected color scales (scale_fill_gsea):
# Calling library:
library(ggsci)
# Creating plot:
ggplot(long.corr.matrix, aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gsea(limits = c(-1,1))
Our new plot:
If you are looking for inspiration, we again recommend checking The R Graph Gallery:
Finally, if you want to be sure that your visualizations are color-blind-friendly, we recommend installing the package colorBlindness:
This library contains a tool called Color Vision Deficiency (CVD) simulator. Itsimulates color vision deficiency. The most general rule says: avoid using green and red for coloring/filling your plots. Using CVD, you can ensure that your selection is safe and matches the guidelines of leading scientific journals. See the examples below:
# Installing colorBlindness package:
install.packages("colorBlindness")
# Calling library:
library(colorBlindness)
# Creating box plots:
# We will use the colors from the RColorBrewer package:
colors <- c("#081D58", "#FF7F00", "#E31A1C")
# Plot:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_manual(values = colors) +
theme_bw()
# Running the CVD simulator:
cvdPlot()
We obtain:
Let's see what happens if you select green/red-based palettes:
# Creating box plots:
# We will use the colors from the RColorBrewer package:
colors <- c("darkgreen","red2", "darkred")
# Plot:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggplot(aes(x = Label,
y = `SM 41:1;O2`,
fill = Label)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.05),
shape = 21,
size = 3) +
scale_fill_manual(values = colors) +
theme_bw()
# Running the CVD simulator:
cvdPlot()
Changing the width, size, shape, line width, and line type (other aesthetics)
We showed you position and color-related aesthetics. The ggplot2 library also enables changing the width, size, shape, line width, and line type. The ggpubr enables adjusting the same aesthetics. Take a look at the examples below.
We will begin with width. The easiest way to modify width is by setting a different value than the default:
The geom_point() is a perfect layer to demonstrate shape and size. The ggplot2 offers 25 different point shapes (considering empty / filled shapes). According to the ggplot2 authors - using more than 6 shape types can make a chart unreadable anyway. All of them were presented in the Basic plotting in R subchapter (Box plots - adding geom_point layer). Here, we will recycle this information, as it is important:
In turn, size of all points in the chart can be changed by setting a specific value. Both shape and size have dedicated scale_shape_manual() and scale_size_manual() options. Using scale_shape... or scale_size... a user can set a selected and different shape type or size for points corresponding, e.g., to different biological groups. Take a look at the examples below:
# Changing the shape type of all points:
# Plot 1:
ggplot(data, aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(width = 0.5) +
geom_point(shape = 1) # All points have shape no. 1 - a circle
# Plot 2:
ggplot(data, aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(width = 0.5) +
geom_point(shape = 4) # Change of shape type to no. 4 - little crosses.
# Changing the size of all points:
# Plot 3:
ggplot(data, aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(width = 0.5) +
geom_point(shape = 4, size = 2, color = 'red2') # All points have size of 2.
# Plot 4:
ggplot(data, aes(x = Label,
y = `SM 41:1;O2`)) +
geom_boxplot(width = 0.5) +
geom_point(shape = 4, size = 4, color = 'red2') # We increase the size of all points to 4.
In the effect, we obtain:
If we want a different point shape or size for every biological group - first, in the aesthetics of an appropriate layer (or in the plot aesthetics), we set shape = <grouping_variable> or size = <grouping_variable>.
Next, using scale_shape_manual() and scale_size_manual(), we select appropriate settings according to our preferences. We provide a vector with numbers of the selected shapes or sizes to the argument called values.
NOTE: For size, similar to opacity, it is not advised to use it with a discrete variable (factor, grouping variable - like our Label). Change point shapes by group instead!
# Different shapes for each biological group:
# Plot 1:
ggplot(data, aes(x = Label,
y = `SM 41:1;O2`,
shape = Label, # Here we indicate that every group must have its shape.
color = Label)) +
geom_boxplot(width = 0.5, outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.1))
# Selecting shapes for every biological group:
# Plot 2:
ggplot(data, aes(x = Label,
y = `SM 41:1;O2`,
shape = Label, # Here we indicate that every group must have its shape.
color = Label)) +
geom_boxplot(width = 0.5, outlier.shape = NA) +
geom_point(position = position_jitter(width = 0.1)) +
scale_shape_manual(values = c(2,5,8)) # Change point shapes to 2, 5, and 8.
The obtained plots:
NOTE: In ggpubr, you can use the add argument to add points (add = 'point' or add = 'jitter') and customize their look through add.params through a list containing information about size, shape, color, etc.
# The customization of shape and size of points in ggpubr:
# Example - 1:
ggboxplot(data,
x = "Label",
y = "SM 41:1;O2",
color = 'black',
fill = 'gray',
outlier.shape = NA,
add = "jitter",
add.params = list(color = "red2",
shape = 4,
size = 2))
The output:
# The customization of shape and size of points in ggpubr:
# Example - 2:
ggboxplot(data,
x = "Label",
y = "SM 41:1;O2",
color = 'black',
fill = 'gray',
shape = "Label",
outlier.shape = NA,
add = "jitter",
add.params = list(color = "red2",
size = 2))
The output:
However, we remind you, that you can also modify all ggpubr plots through ggplot2 commands, e.g.:
Customizing ggpubr plot with ggplot2 commands (here - adding points):
library(RColorBrewer)
ggboxplot(data,
x = "Label",
y = "SM 41:1;O2",
fill = "Label") +
geom_point(aes(shape = Label),
position = position_jitter(width = 0.1),
color = "red2") +
scale_shape_manual(values = c(0,1,2)) +
scale_fill_brewer(palette = 'Oranges')
The output:
The aesthetics also allows for adjusting the width and type of lines in the plot. The linewidth argument enables changing the thickness of lines, and linetype the selecting among 6 (7) types of lines. All of them are shown here:
First, we will change the linewidth and linetype of a box plot outline. Then, one more example for box plots depicting paired data (connected with the lines).
The modification of box plots outlines is presented below:
# Changing box plots' outline (line types, widths).
# Plot 1:
# ggpubr:
data %>%
select(`Label`, `SM 41:1;O2`) %>%
ggboxplot(x = "Label",
y = "SM 41:1;O2",
fill = "Label",
linetype = 'dashed') # Any other type can be selected: "twodash", "dotted", "dotdash", "longdash", etc.
# Plot 2:
# ggplot2:
ggplot(data, aes(x = Label,
y = `SM 41:1;O2`,
shape = Label,
color = Label)) +
geom_boxplot(width = 0.5,
outlier.shape = NA,
linetype = 'longdash') + # Change of the line type.
geom_point(position = position_jitter(width = 0.1))
# Plot 3:
# Changing outline thickness:
ggplot(data, aes(x = Label,
y = `SM 41:1;O2`,
shape = Label,
color = Label)) +
geom_boxplot(width = 0.5,
outlier.shape = NA,
linewidth = 1) + # Change of the line width (thickness).
geom_point(position = position_jitter(width = 0.1))
The obtained plots:
We can naturally use the linetype argument with a grouping variable. Then, using scale_linetype_manual() you can specify in the values a vector with the numbers of line types or names of line types.
Here, we will use again the data published by Jirásko et al. in Cancers for clear cell renal cell carcinoma (ccRCC, kidney cancer) tumors and adjacent tissues (paired samples). You can download them again, if necessary:
Load the data as 'data.paired'. Adjust the Tissue part column type to factor (it contains the annotation N for healthy adjacent tissues and T for tumors) Then, you can test the following block of code:
# Remember to set the same `Sample code` for matching samples.
# Remove the T - from the `Sample codes` describing tumors:
data.paired$`Sample code` <- gsub("^T", "", data.paired$`Sample code`)
# Creating a long tibble with data for plotting:
data.paired.long <-
data.paired %>%
select(`Sample code`,
`Tissue part`,
`SM 38:1;O2`,
`SM 40:1;O2`,
`SM 42:1;O2`) %>%
pivot_longer(cols = `SM 38:1;O2`:`SM 42:1;O2`,
names_to = 'Lipids',
values_to = 'Concentrations')
# Creating plot:
ggplot(data.paired.long,
aes(x = `Tissue part`,
y = `Concentrations`,
fill = `Tissue part`)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_dodge(width = 0.75), shape = 21) +
geom_line(aes(group = `Sample code`,
linetype = `Lipids`, # We want different line types for every lipid.
color = `Lipids`))+ # We will also change the color of every line (to highlight it).
scale_fill_manual(values = c('royalblue', 'red2'))+
facet_grid(. ~ Lipids, switch = "x") +
theme(strip.placement = "outside",
strip.background = element_blank(),
panel.spacing.x = unit(0, 'cm')) +
scale_linetype_manual(values = c(2,3,4))
The output:
Changing the order of values in a factor column
Imagine the situation, you obtain this plot:
However, you want to change the order of box plots so that all patients with pancreatic cancer (T) appear first from the left side, next all controls (N), and at the end - the patients with pancreatitis (PAN). At the moment, the order is simply alphabetical. To achieve this, you have to rearrange the order of variables in the factor column. First, we need to check the current order, and the function that we need is called levels(). It is a base R function. Here is an example of how to use it:
# Checking the order of variables in the factor column `Label`:
levels(data$Label)
We obtain in the console the current order, which is:
> levels(data$Label)
[1] "N" "PAN" "T"
Changing the order can be achieved through mutate() (dplyr package) and fct_relevel() (forcats package). Both functions can be used if we call the tidyverse collection. The operation is simple. Here is the code:
# Calling library:
library(tidyverse)
# Changing the order of variables in `Label`:
data %>%
mutate(Label = fct_relevel(Label, "T", "N", "PAN"))
We indicate in mutate() what column we would like to mutate (here Label). Next, we use the fct_relevel() function. In fct_relevel(), first, we specify the factor column that we would like to relevel (Label), and right after - we list all factor variables in the new order. If we want this modification to be permanent, we store the newly obtained tibble in the global environment as 'data' (we simply overwrite our current tibble 'data'):
# Changing the order of variables in `Label`:
data <-
data %>%
mutate(Label = fct_relevel(Label, "T", "N", "PAN"))
# or
data <- mutate(data, Label = fct_relevel(Label, "T", "N", "PAN"))
Let's check what's the order after using mutate() with fct_relevel():
# Rechecking the order of variables in the factor column `Label`:
levels(data$Label)
Now, we obtain:
> levels(data$Label)
[1] "T" "N" "PAN"
And now our chart looks like this:
The forcats package has much more to offer. More examples are shown by The R Graph Gallery, here:
Modifications in the plot themes (background, grids, axes)
It is time to customize all non-data displays! In ggpubr and ggplot2, these objects are controlled by themes. They constitute a powerful and well-developed set of tools that enable modifying all objects unrelated to our data, e.g.,
plot backgrounds,
grid lines,
the look of x-y axes,
plot legends,
panels and stripes,
plot descriptions.
By default, ggplot2 comes with 9 themes:
theme_gray() or theme_grey()
theme_bw()
theme_linedraw()
theme_light()
theme_dark()
theme_minimal()
theme_classic()
theme_void()
theme_test()
The first one of them, theme_gray() (or theme_grey()), is one that automatically appears in every ggplot2 chart. Except for it, we frequently used the theme_bw(). If you are not a fan of creating your theme, you can start with other themes proposed by ggplot2. Some of them are great, check out these four options:
NOTE: You can use the classic ggplot2 themes with all plots, which were generated using ggpubr.
If none of the above-presented themes attracted your attention - try ggthemes. This package is a true mine of ggplot2 themes. First, we need to install and call the library:
# Installing ggthemes package:
install.packages("ggthemes")
# Calling the library:
library(ggthemes)
Except for all the ready themes, you can create your theme through theme(). The theme() is simple to handle. First, you indicate what would you like to modify (e.g. axis.title, axis.ticks, panel.background, axis.line.x, legend.background, etc.), then a specific element for modification, (e.g. line, text, rect), and finally - how would you like to change the settings. Take a look at the scheme below to understand theme() better:
Note: If you would like to remove an element of the ggplot2 theme, use element_blank(). Let's customize our plot using theme():
As you see, the theme() allows for high flexibility in adjusting the non-data-related objects in the plot. In one code we removed the gray plot background and modified major and minor grid lines, x-y axes, x-y-axes' ticks, x-y axes' texts, and titles. The x-axis text labels are rotated and the angle is set to 45°. This is particularly useful if your x-axis text labels are long and they will likely overlay. We faced such a problem, e.g. in the case of the ggplot-2 correlation heat maps:
Now, that you have a basic knowledge about theme(), let's use it to fix this situation. The problem concerns text associated with the x-axis, let's access it then:
# Selecting theme element for modification
ggplot(long, aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gsea() +
theme(axis.text.x = element_text(.......))
To solve this problem, we can rotate labels (let's set the angle to 90°), and additionally - we need to adjust the horizontal and vertical positions of labels, so the labels are attached to ticks:
# Selecting theme element for modification
ggplot(long, aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gsea() +
theme(axis.text.x = element_text(angle = 90,
hjust = 1,
vjust = 0.5)))
The final plot:
Modifications in the plot themes (legends)
Through the theme() we can introduce changes in the legends appearing around the ggplot2. Here, we will again use the ccRCC data set:
Now, let's introduce some changes in these legends. First - you can remove the legend in a selected layer through one of the geom_...(). Set show.legend to FALSE as in the examples below:
# Removing the violin plots' legend and box plots' legend:
ggplot(data.paired.long,
aes(x = `Tissue part`,
y = `Concentrations`,
fill = `Tissue part`)) +
geom_violin(aes(color = `Lipids`,),
fill = 'white',
linewidth = 1,
show.legend = F) + # Set show.legend to F.
geom_boxplot(outlier.shape = NA,
width = 0.3,
show.legend = F) + # Set show.legend to F.
geom_point(position = position_dodge(width = 0.75), shape = 21) +
geom_line(aes(group = `Sample code`,
linetype = `Gender`),
color = '#555555')+
scale_fill_manual(values = c('royalblue', 'red2'))+
facet_grid(. ~ Lipids, switch = "x") +
theme(strip.placement = "outside",
strip.background = element_blank(),
panel.spacing.x = unit(0, 'cm')) +
scale_linetype_manual(values = c(2,3,4)) +
scale_y_log10()
The position of legends can be changed through the legend.position in theme(). In legend.position you can specify "bottom", "left", "right", "up", or a vector with two numeric values if you want to put a legend in the chart. Additionally, you can specify if all legends should be arranged horizontally or vertically using legend.box:
The theme(), however, applies changes to all legends. What if we want to modify only two legends of all three? In this case, use the guide argument in the appropriate scale_fill_manual() / scale_color_manual(). In the guide_legend() new parameters can be set:
Finally, we will show you how to split legends, e.g. we place two of the legends at the bottom of the plot, while one (e.g. color-related legend), remains on the right. First, we need to install the cowplot package (ggplot2 extension):
# Installing cowplot package:
install.packages("cowplot")
# Calling the library:
library(cowplot)
Now, using the cowplot package, we need to extract the legend of interest (via get_legend()):
colors.legend <- get_legend(Plot.1 + guides(linetype = "none",
fill = "none"))
Next, using plot_grid() - we can create two columns. In one of them, we will place our plot with legend position at the bottom. In the second column, we will put the extracted legend. Through rel_widths() - we will optimize the amount of space for our plot (column 1), and for the legend (column 2) - here 0.87 for the plot and 0.13 for the color legend. The final code:
Using labs(), xlabs(), ylabs(), a title, subtitle, caption, legend title, and x-y axes titles can be changed. See the example below and the obtained output:
# Changing axes, legend, and plot labels with labs():
ggplot(data.paired.long,
aes(x = `Tissue part`,
y = `Concentrations`,
fill = `Tissue part`)) +
geom_violin(aes(color = `Lipids`,),
fill = 'white',
linewidth = 1) +
geom_boxplot(outlier.shape = NA,
width = 0.3) +
geom_point(position = position_dodge(width = 0.75), shape = 21) +
geom_line(aes(group = `Sample code`,
linetype = `Gender`),
color = '#555555')+
scale_fill_manual(values = c('royalblue', 'red2'))+
scale_linetype_manual(values = c(2,3,4)) +
facet_grid(. ~ Lipids, switch = "x") +
theme_bw() +
theme(strip.placement = "outside",
strip.background = element_blank(),
panel.spacing.x = unit(0, 'cm')) +
labs(title = "Alterations in plasma SM profiles in PDAC",
subtitle = "Measured using SFC/MS",
caption = "Customized ggplot2 box plots",
x = "Tissue part collected",
y = "Lipid concentration, [nmol/mL]",
color = "Lipid species",
fill = "Status",
linetype = "Gender - F/M")
In labs(), use title, subtitle, and caption to add the main plot labels. Deliver strings directly to these arguments. The x-y axes can be renamed through x and y arguments (again, deliver new names as strings). To modify the title of a legend, indicate in what legend are you interested first, e.g. color, fill, linetype, linewidth, and then deliver a new title as a string.
More about labs can be found here:
Scaling the ggplot2 axes. The difference between scale_y_log10() vs coord_trans(y = 'log10')
If high and low values of concentrations are plotted in one x-y plane, a scaling may be applied to improve the plot readability. There are two types of scaling functions one can apply to the y-axis, e.g. in the case of box plots:
The differences between the two plots are visible at first glance, e.g. the straight vs skewed lines connecting paired observations. It is important to understand the difference between these two methods before using them:
1) In scale_y_log10(), your data are first log-transformed, next using the transformed data, all statistics are computed, and finally, the data are back-transformed. Hence, if you compute a mean value for a biological group (x1) vs you apply scale_y_log10() and compute the mean value of the same biological group (x2) - these two values will be different. The mean of logs is not the same as the mean of the non-transformed data. The effect of the data transformation through scale_y_log10() is that the large values will have less impact on the computed statistics in general.
2) In coord_trans(y = 'log10), only the y-axis coordinates are affected. This means, that the arithmetic mean computed for a biological group is still going to be the same. However, as you see, the y-axis shrinks in the effect of coord_trans(y = 'log10'). Therefore, ggplot2 cannot guarantee the lines will remain straight.
IMPORTANT: If you would like to use any of these scaling methods, read carefully the documentation and vignettes to understand the consequences - particularly in the case of the scale_y_log10().
Merging multiple ggpubr or ggplot2 plots into one image
A ggpubr function called ggarrange() can be used to merge multiple charts into one image. The ggarrange() is a very handy tool, allowing the arrangement and alignment of plots while preparing the image and combining all legends into one common image legend. Below you will find an example of how to use it:
# Example 1: merging 4 different plots into 1 image:
library(ggpubr)
library(tidyverse)
Plot.1 <-
data %>%
filter(Label != "PAN") %>%
ggdensity(x = "SM 38:1;O2",
fill = "Label",
color = "Label",
palette = c("royalblue",
'red2'),
rug = T)
Plot.2 <-
ggboxplot(data,
x = "Label",
y = "SM 39:1;O2",
fill = "Label",
add = "jitter",
add.params = list(shape = 23)) +
scale_fill_cosmic()
Plot.3 <-
ggviolin(data,
x = "Label",
y = "SM 38:1;O2",
fill = "Label",
add = c("boxplot", "point"),
add.params = list(size = 0.5))
Plot.4 <-
ggbarplot(data,
x = "Label",
y = "SM 39:1;O2",
color = "Label",
size = 1,
add = c("mean_sd", "jitter"),
add.params = list(shape = 25,
size = 1))
# Example 1:
ggarrange(Plot.1, Plot.2, Plot.3, Plot.4, ncol = 2, nrow = 2, align = c("hv"))
The output:
We easily connected four different plots into one image. You can indicate plot names separately to the ggarrange() or deliver a list. Using ncol and nrow - you can select how many columns and rows should be created. The align argument will help you align plots vertically and horizontally. Take a look at the example number 2:
# Example 2: merging 2 different plots into 1 image and creating a common legend:
# Creating plots:
Plot.1 <-
ggboxplot(data,
x = "Label",
y = "SM 39:1;O2",
fill = "Label",
add = "jitter",
add.params = list(shape = 23),
palette = c('royalblue', 'orange', 'red2'))
Plot.2 <-
ggbarplot(data,
x = "Label",
y = "SM 39:1;O2",
color = "Label",
size = 1,
add = c("mean_sd", "jitter"),
add.params = list(shape = 25, size = 1),
palette = c('royalblue', 'orange', 'red2'))
# Merging plots and adding title to the ggarrange image:
ggarrange(Plot.1, Plot.2, common.legend = T, legend = 'bottom') %>%
annotate_figure("Box plots vs bar plots")
The output:
To create a common legend for the image - first, set common.legend to TRUE, then using the legend argument specify the common legend position in the image. You can also add a title to the ggarrange object through the annotate_figure() function from the ggpubr package.
Data set published by Jirásko et al. in Cancers. DOI: 10.3390/cancers14194622.
Raw ggpubr and ggplot2 box plots.
The ggpubr and ggplot2 box plots colored in violet and filled with pink.
Changing opacity of the box plots filling for ggpubr/ggplot2 charts.
No fill color used - examples of box plots with emtpy boxes.
Coloring outline by a factor column Label.
Filling by biological group (Label column).
Changing the colors of box plots' outlines according to biological groups - with selected colors.
Changing the colors of box plots' fillings according to biological groups - with selected colors.
Using scale_color_hue() to modify the color of box plots' filling.
Using grey scales to fill box plots (scale_fill_grey()).
A/ Palettes available in the RColorBrewer. B/ Colorblind-friendly palettes in RColorBrewer.
Box plots filled with colors from RColorBrewer: ggpubr box plots filled with "Oranges", ggplot2 box plots filled with "Dark2", and ggplot2 box plots filled with user-picked colors.
The examples of ggsci journal palettes.
The examples of the ggsci TV shows and series palettes.
Colors with color codes from the Frontiers palette (ggsci R package).
The "Oranges" palette from RColorBrewer library.
The FantasticFox1 palette from the wesanderson library.
The ggpubr and ggplot2 box plots filled with colors from the GrandBudapest1 palette from the wesanderson package.
The ggpubr and ggplot2 box plots filled with colors from the Zissou1 palette (wesanderson package).
The ggpubr and ggplot2 box plots filled with colors from the viridis package.
The overview of palettes and color codes (viridis package).
Continuous fill color scaling - according with the increase of SM 41:1;O2 concentration.
A histogram filled using scale_fill_gradient().
Melting wide correlation matrix into a long correlation matrix with melt() function from the reshape2 package.
A correlation heat map generated using ggplot2 package and colored using scale_fill_gradient2().
A correlation heat map produced using ggplot2 library and colored using scale_fill_gradientn().
A correlation heat map produced using ggplot2 library and colored using scale_fill_gsea().
The output from the CVD simulator. Our selection of colors could be kept (it is possible to distinguish groups).
Example of a wrong color selection - as you see based on the simulation.
Changing box plot width (ggpubr, ggplot2).
Changing shape types of all points (left panel). Changing point size - all points (right panel).
Changing point shapes according to biological groups (Label column) - ggplot2 library.
The customization of shape and size of points in ggpubr - example - 1.
The customization of the shape and size of points in ggpubr - example 2.
Customizing ggpubr box plots with ggplot2 commands. Here - adding points and changing the fill color.
Changing outline type for ggpubr box plots (Plot 1), ggplot2 box plots (Plot 2), outline thickness (ggplot2).
Different line types depending on the lipid (Lipids column in the long tibble). Colors were applied to highlight different types of lines.
Releveling of factor variables through mutate() with fct_relevel() - tidyverse collection.
Changing the order of factor variables with mutate() and fct_relevel() - summary.
Examples of classic, default ggplot2 themes.
Testing different themes from the ggthemes package.
Using theme() to modify a ggplot2 themes' look.
The ggplot2 theme modified using theme().
The x-axis text labels overlaid - an issue that can be resolved through theme().
The final plot with corrected x-axis labels using theme().
Plot with three legends obtained through different layers of the ggplot2: geom_violin(), geom_boxplot(), geom_point(), and geom_line()
Removing legends in selected layers through show.legend = F.
Different examples of the legend positions. Plot 1 - bottom + vertical, Plot 2 - bottom + horizontal.
Customizing ggplot2 legends through theme().
Customizing two of three legends using guide arguments of scale_fill_manual()/scale_color_manual() functions.
Splitting legends with cowplot package.
Creating plot annotations using labs().
Effects of using different scaling methods: scale_y_log10() (left plot), coord_trans(y = 'log10') (right plot).
Four plots merged into one image using ggarrange() from the ggpubr package.
Creating a common legend for plots describing the same biological groups in ggarrange().
Reordering a factor variable with forcats package.