Introduction to the
The **ggplot2** by Hadley Wickham is a handy package, a must have for visual tools. However, need to know ggPLOT2 certain theory and principle, for beginners, the threshold of entry is relatively high.
The GGPUBR package provides some easy to use functions, written based on ggploT2, the syntax is very simple icon. This is great for people who are new to R and want to make publishable charts out of R!
The article ggpubr: Publication Ready Plots will be explained below.
The installation
- Install from CRAN:
install.packages("ggpubr")
Copy the code
- Install the latest version from GitHub:
if(! require(devtools))install.packages("devtools") devtools :: install_github("kassambara / ggpubr")Copy the code
Summary of Visual functions
graphics | The command |
---|---|
Density figure | ggdensity() |
Box figure | ggboxplot() |
A histogram | gghistogram() |
The violin figure | ggdotchart() |
The bar chart | ggdotchart() |
Lollipop chart, Cleveland chart | ggdotchart() |
Density figure
Seed (1234) wdata = data.frame(sex = factor(rep(c("F", "M"), each=200)), weight = c(rnorm(200, 55), rnorm(200, 58))) head(wdata, 4)Copy the code
- Density diagram (
ggdensity
) and average (add = “mean”); According to the gender("sex")
Color fill; Add edge carpet(rug = TRUE)
And use custom panels(palette = c("#00AFBB", "#E7B800"))
.
ggdensity(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
Copy the code
The following is the drawing of the HISTOGRAM, with other parameters similar to the above.
gghistogram(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
Copy the code
Box figure
# Load data
data("ToothGrowth")
df <- ToothGrowth
head(df, 4)
Copy the code
Color filling according to dose (” Dose “); Add a jitter point and change the shape by dose (“dose”).
p <- ggboxplot(df, x = "dose", y = "len",
color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
add = "jitter", shape = "dose")
p
Copy the code
You can also compare the relationship between the different group means(stat_compare_means(label.y = 50)) and add the P value (stat_compare_means(label.y = 50)).
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-valueCopy the code
The violin figure
The following figure is the combination of ggviolin and boxplot (add = “boxplot”), and the color is filled according to the dose. Add a box diagram with white fills (add.params = list(fill = “white”)) and add the p values for each group comparison.
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
Copy the code
The bar chart
The data set
This is plotted using the MTCARS dataset.
data("mtcars")
dfm <- mtcars
dfm$cyl <- as.factor(dfm$cyl)
dfm$name <- rownames(dfm)
head(dfm[, c("name", "wt", "mpg", "cyl")])
Copy the code
Ordered bar chart
In a ggbarplot, sort. Val = “desc” and sort not within groups (sort.by.groups = FALSE), but all data. Rotate the X-axis label (x.ext.angle = 90). Note that you used the JCO magazine color palette (” JCO “).
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 )Copy the code
If sorted within groups (sort.by.groups = TRUE), this would be the case.
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 )Copy the code
Deviation map
The deviation diagram shows the deviation of quantitative values from reference values. In the R code below, we plot the mpGZ-Score changes (a standardized one) from the MTCars data set.
# 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"))
head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])
Copy the code
From the above data, create an ordered box diagram and sort the values in ascending order (sort.val = “asc”). Here, unlike in the previous box diagram, we use the mpg_grp variable we just built as the fill parameter, which is the factor (with two levels, levels = c(“low”, “high”)).
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" )Copy the code
Rotate x, y (TRUE) and sort in descending order (sort.val = “desc”), as shown in the image below.
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() )Copy the code
A scatter diagram
Lollipop figure
The lollipop chart is an alternative to the bar chart, and the final image looks like a lollipop.
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
)
Copy the code
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)Copy the code
Deviation map
Again, using the data set above, construct the deviation diagram of the lollipop series. 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")Copy the code
Cleveland scatter plot
Just add the following code to get the Cleveland scatter plot (theme_Cleveland ()).
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
rotate = TRUE, # Rotate vertically
dot.size = 2, # Large dot size
y.text.col = TRUE, # Color y text by groups
ggtheme = theme_pubr() # ggplot2 theme
)+
theme_cleveland() # Add dashed grids
Copy the code