ggpubr
ggpubrIt is specially designed for drawing academic journals. It is said that it can draw the map that editor likes most.
1. Installation and loading
Because this bag is incran
Therefore, the installation is also convenient and directinstall.packages()
.
install.packages("ggpubr")
library("ggpubr")
2. Drawing example
- Gene expression data
- Box plots
- Violin plots
- Stripcharts and dot plots
- Density plots
- Histogram plots
- Empirical cumulative density function
- Quantile – Quantile plot
library(ggpubr)
set.seed(1234)
wdata = data.frame(
sex = factor(rep(c("F", "M"), each=200)),
Weight = C (rnorm (200, 55), rnorm (200, 58)) #200 random numbers, with an average of 55
head(wdata, 4)
# sex weight
#1 F 53.79293
#2 F 55.27743
#3 F 56.08444
#4 F 52.65430
2.1 Density plots
# Density plot with mean lines and marginal rug
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups ("sex")
# Use custom palette
ggdensity(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
Among them,x
Variables to be plotted for the horizontal axis;add = "mean"
Mean value added;rug = TRUE
It means adding a data rug, which can better show the distribution of data;color = "sex", fill = "sex"
, representing the color of the density map insexFor filling, if only color is not filled, only the density curve will be displayed, and there will be no filling. On the contrary, the density curves are represented by black lines;palette
As a palette, specify the drawing color.

2.2 Histogram plot
# Histogram plot with mean lines and marginal rug
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups ("sex")
# Use custom color palette
gghistogram(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
The parameter is similar to density plots, so I won’t repeat it.

2.3 Box plots and violin plots
# Load data
data("ToothGrowth")
df <- ToothGrowth
head(df, 4)
len supp dose
1 4.2 VC 0.5
2 11.5 VC 0.5
3 7.3 VC 0.5
4 5.8 VC 0.5
# Box plots with jittered points
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline colors by groups: dose
# Use custom color palette
# Add jitter points and change the shape by groups
p <- ggboxplot(df, x = "dose", y = "len",
color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
add = "jitter", shape = "dose")
p

For any type of graph, the parameters should be adjusted to the shape of the example, and then further modified. The meaning of the above parameters is to fill different colors according to different doses with dose as the abscissa and Len as the ordinate;add = "jitter"
Indicates that the jumping points are added on the boxplot, indicating the distribution of data;shape = "dose"
Indicates that the shape of the point is based on dose.
Add p value to abscissa group
# Add p-values comparing groups
# Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 50) # Add global p-value
my_comparisons
Indicates the group to which the comparison is added;label.y
Indicates where the total P value is displayed. If the last one is removedstat_compare_means
, the overall p value is not displayed.

Violin picture
# Violin plots with box plots inside
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change fill color by groups: dose
# add boxplot with white fill color
ggviolin(df, x = "dose", y = "len", fill = "dose",
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
add = "boxplot", add.params = list(fill = "white"))+
stat_compare_means(comparisons = my_comparisons, label = "p.signif")+ # Add significance levels
stat_compare_means(label.y = 50) # Add global the p-value

label
There are two ways to represent the p value in. Allowed values include “p.significant” (shows the significance levels) and “p.format” (shows the formatted p value). The default is the second way. Among them,add = "boxplot"
Indicates adding a box diagram on the violin diagram,add.params
expressadd
Add the parameters of the drawing. Here is to add a white box diagram.
2.4 Bar plots
2.4.1Ordered bar plots
data("mtcars")
df2 <- mtcars
df2$cyl <- factor(df2$cyl)
Df2 $name < - rownames (df2) # add a line name
head(df2[, c("name", "wt", "mpg", "cyl")])
df2<-dfm
ggbarplot(dfm, x = "name", y = "mpg",
fill = "cyl", # change fill color by cyl
color = "white", # Set bar border colors to white
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "desc", # Sort the value in dscending order
sort.by.groups = FALSE, # Don't sort inside each group
x.text.angle = 90 # Rotate vertically x axis texts
)

fill = "cyl"
, indicating that the color filling is grouped by another group;color = "white"
Change the boundary of the column, i.e. colorless;sort.val = "desc"
Indicates that the variables are arranged in descending order. Other options are allowed values are “None” (no sorting), “ASC” (for ascending) or “desc” (for descending);sort.by.groups = FALSE
Indicates that the divided groups are not clustered together. If it is t, they are clustered together;x.text.angle = 90
The name of the x-axis rotates 90 degrees counterclockwise.
2.4.2 barplot for internal sorting of groups
By settingsort.by.groups = TRUE。
ggbarplot(dfm, x = "name", y = "mpg",
fill = "cyl", # change fill color by cyl
color = "white", # Set bar border colors to white
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "asc", # Sort the value in dscending order
sort.by.groups = TRUE, # Sort inside each group
x.text.angle = 90 # Rotate vertically x axis texts
)

2.4.3 Deviation graphs
The deviation chart shows the deviation between the quantitative value and the reference value. In the following code, we will plot mpg Z-score from the mtcars data set.
Calculate the z-score of the mpg data:
The setting of factors is worth learning. There is a judgment sentence in the factors.
# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"),
levels = c("low", "high"))
# Inspect the data
head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])
Create an ordered barplot and color it according to the level of MPG factor.
ggbarplot(dfm, x = "name", y = "mpg_z",
fill = "mpg_grp", # change fill color by mpg_level
color = "white", # Set bar border colors to white
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "asc", # Sort the value in ascending order
sort.by.groups = FALSE, # Don't sort inside each group
x.text.angle = 90, # Rotate vertically x axis texts
ylab = "MPG z-score",
xlab = FALSE,
legend.title = "MPG Group"
)

legend.title
Add a name to legend.
Rotate the image. use rotate = TRUE and sort. val = “desc”
ggbarplot(dfm, x = "name", y = "mpg_z",
fill = "mpg_grp", # change fill color by mpg_level
color = "white", # Set bar border colors to white
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "desc", # Sort the value in descending order
sort.by.groups = FALSE, # Don't sort inside each group
x.text.angle = 90, # Rotate vertically x axis texts
ylab = "MPG z-score",
legend.title = "MPG Group",
rotate = TRUE,
ggtheme = theme_minimal()
)

2.5 Dot charts
Lollipop chart
When you have a large number of values to visualize, a bar chart is another option for a bar chart.
2.5.1 general lollipop chart
ggdotchart(dfm, x = "name", y = "mpg",
color = "cyl", # Color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
sorting = "ascending", # Sort value in descending order
add = "segments", # Add segments from y = 0 to dots
ggtheme = theme_pubr() # ggplot2 theme
)

add = "segments"
Add a bar.
2.5.2 horizontal lollipop chart
- Sort in descending order. sorting = “descending”.
- Rotate the plot vertically, using rotate = TRUE.
- Sort the mpg value inside each group by using group = “cyl”.
- Set dot.size to 6.
- Add mpg values as label. label = “mpg” or label = round(dfm$mpg).
ggdotchart(dfm, x = "name", y = "mpg",
color = "cyl", # Color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
sorting = "descending", # Sort value in descending order
add = "segments", # Add segments from y = 0 to dots
rotate = TRUE, # Rotate vertically
group = "cyl", # Order by groups
dot.size = 6, # Large dot size
label = round(dfm$mpg), # Add mpg values as dot labels
font.label = list(color = "white", size = 9,
vjust = 0.5), # Adjust label parameters
ggtheme = theme_pubr() # ggplot2 theme
)

group=
Sort by group;dot.size
The size of the setpoint;label
Write the value on the point whereround()
Indicates rounding;font.label
Sets the font of the point.
2.5.3 deviation chart of lollipop chart type
- Use
y = “mpg_z”
- Change segment color and size:
add.params = list(color = “lightgray”, size = 2)
ggdotchart(dfm, x = "name", y = "mpg_z",
color = "cyl", # Color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
sorting = "descending", # Sort value in descending order
add = "segments", # Add segments from y = 0 to dots
add.params = list(color = "lightgray", size = 2), # Change segment color and size
group = "cyl", # Order by groups
dot.size = 6, # Large dot size
label = round(dfm$mpg_z,1), # Add mpg values as dot labels
font.label = list(color = "white", size = 9,
vjust = 0.5), # Adjust label parameters
ggtheme = theme_pubr() # ggplot2 theme
)+
geom_hline(yintercept = 0, linetype = 2, color = "lightgray")

geom_hline
Draws the specified horizontal line.
2.5.4 Cleveland’s dot plot
To put it bluntly, the color of the Y axis corresponds to the color grouped in the figure.Use y.text.col = TRUE.

<meta charset=”utf-8″>
3. Common basic drawing functions and parameters
Basic drawing function
gghistogram Histogram plot # plot histogram
ggdensity Density plot # plot density
ggdotplot Dot plot # plot points
ggdotchart Cleveland’s dot plots # plot Cleveland points
ggline Line plot # draws a line plot
ggbarplot Bar plot # draw a histogram
ggstripchart Stripcharts # draw a strip chart
ggboxplot Box plot # draw a box plot
ggviolin Violin plot # plot violin
ggpie Pie chart # drawing
ggqqplot QQ plots # draw QQ maps
ggscatter Scatter plot # plot
ggmaplot Ma plot from means and log fold changes
ggpaired Plot paired data # plots a scatter matrix
ggerrorplot Visualizing error # plotting error graphs
Basic parameters
ggtext Text # add text
border Set ggplot panel border line # sets the canvas border
grids Add grids to a ggplot # add gridlines
font Change the appearance of titles and axis labels # set the font type
bgcolor Change ggplot panel background color # change the canvas background color
background_image Add background image to ggplot2 # add background image
facet Facet a ggplot into multiple panels # set facets
ggpar Graphical parameters # add drawing parameters
ggparagraph Draw a paragraph of text # add a paragraph of text
ggtexttable Draw a textual table # add a text table
ggadd Add summary statistics or a geom onto a ggplot # add basic statistics or other geometry
ggarrange Arrange multiple ggplots # layout multiple graphs
gradient_color Set gradient color # sets the continuous color
xscale Change axis scale: log2, log10 and more # change the scale of the axis
add_summary Add summary statistics onto a ggplot # add basic statistics results
set_palette Set color palette # sets the palette color
rotate Rotate a ggplot horizontally # sets the graph rotation
rotate_axis_text Rotate axes text # rotate axis text
stat_stars Add stars to a scatter plot # add stars
stat_cor Add correlation coefficients with p-values to a scatter plot # add correlation coefficients
stat_compare_means Add mean comparison p-values to a ggplot # add mean comparison p-values
theme_transparent Create a ggplot with transparent background # sets the transparent background
theme_pubr Publication ready theme # sets the publication theme
Reference link:
http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/
Link:https://www.jianshu.com/p/f53a05da7745
Reference link:https://www.jianshu.com/p/43967191bb06