Using ggpubr to create publication ready figures


Load the necessary packages. Load and explore the data you want to use.

library(ggplot2)
library(ggpubr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
gap = read.csv("gapminderData5.csv")
str(gap)
## 'data.frame':    1704 obs. of  6 variables:
##  $ country  : chr  "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
##  $ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ pop      : num  8425333 9240934 10267083 11537966 13079460 ...
##  $ continent: chr  "Asia" "Asia" "Asia" "Asia" ...
##  $ lifeExp  : num  28.8 30.3 32 34 36.1 ...
##  $ gdpPercap: num  779 821 853 836 740 ...

Create a subset of the data set.

gap07 = gap %>% 
  filter(year == 2007 & continent != "Oceania")

Scatter Plots

Create a basic scatter plot using ggplot.

ggplot(gap07, aes(x = gdpPercap, y = lifeExp, col = continent)) + 
  geom_point() + scale_x_log10("GDP per capita ($)") + 
  scale_y_continuous("Life Expectancy (yrs)") + ggtitle("GapMinder Data 2007")

Create a basic scatter plot using ggpubr.

ggscatter(gap07, x = "gdpPercap", y = "lifeExp", col = "continent",
          xlab = "GDP per capita ($)", ylab = "Life expectancy (yrs)", 
          main = "GapMinder Data 2007") + 
  xscale("log10", .format = TRUE)

Add labels to each point. Yikes.

ggscatter(gap07, x = "gdpPercap", y = "lifeExp", col = "continent",
          xlab = "GDP per capita ($)", ylab = "Life expectancy (yrs)", 
          main = "GapMinder Data 2007", label = "country", repel = TRUE) + 
  xscale("log10", .format = TRUE)

Or just select points. Better.

sel_countries = c("United States", "China", "Germany")
ggscatter(gap07, x = "gdpPercap", y = "lifeExp", col = "continent",
          xlab = "GDP per capita ($)", ylab = "Life expectancy (yrs)", 
          main = "GapMinder Data 2007", label = "country", 
          label.select = sel_countries, repel = TRUE) + 
  xscale("log10", .format = TRUE)

We can add a marginal histogram.

library(ggExtra)
p <- ggscatter(gap07, x = "gdpPercap", y = "lifeExp", col = "continent",
          xlab = "GDP per capita ($)", ylab = "Life expectancy (yrs)", 
          main = "GapMinder Data 2007") + 
  xscale("log10", .format = TRUE)
ggMarginal(p, type = "histogram")

Or a regression line.

ggscatter(gap07, x = "gdpPercap", y = "lifeExp", col = "continent",
          xlab = "GDP per capita ($)", ylab = "Life expectancy (yrs)", 
          main = "GapMinder Data 2007", add = "reg.line", conf.int = TRUE) + 
  xscale("log10", .format = TRUE)

Or correlations.

ggscatter(gap07, x = "gdpPercap", y = "lifeExp", col = "continent",
          xlab = "GDP per capita ($)", ylab = "Life expectancy (yrs)", 
          main = "GapMinder Data 2007", add = "reg.line", conf.int = TRUE) + 
  xscale("log10", .format = TRUE) +
  stat_cor(aes(color = continent), method = "spearman")

Or regression line equations.

ggscatter(gap07, x = "gdpPercap", y = "lifeExp", col = "continent",
          xlab = "GDP per capita ($)", ylab = "Life expectancy (yrs)", 
          main = "GapMinder Data 2007", add = "reg.line", conf.int = TRUE) + 
  xscale("log10", .format = TRUE) +
  stat_regline_equation(aes(color = continent))


Histograms

Create a basic histogram.

gghistogram(gap07, x = "lifeExp", main = "GapMinder Life Expectancy")
## Warning: Using `bins = 30` by default. Pick better value with the argument
## `bins`.

We can separate continents by color.

gghistogram(gap07, x = "lifeExp", fill = "continent", 
            main = "GapMinder Life Expectancy")
## Warning: Using `bins = 30` by default. Pick better value with the argument
## `bins`.

We can change the color palette.

gghistogram(gap07, x = "lifeExp", fill = "continent", 
            main = "GapMinder Life Expectancy", palette = "jco")
## Warning: Using `bins = 30` by default. Pick better value with the argument
## `bins`.


Density Plots

Create a basic density plot.

ggdensity(gap07, x = "lifeExp", fill = "continent", 
          main = "GapMinder Life Expectancy", palette = "jco")

Use ggpubr to create faceted plots.

ggdensity(gap07, x = "lifeExp", fill = "continent", 
          main = "GapMinder Life Expectancy", palette = "jco",
          facet.by = "continent")

We can add reference lines (median life expectancy by continent) and a rug.

ggdensity(gap07, x = "lifeExp", fill = "continent", 
          main = "GapMinder Life Expectancy", palette = "jco",
          facet.by = "continent",
          add = "median", rug = TRUE)


Violin Plots

Make a basic violin plot.

ggviolin(gap07, x = "continent", y = "lifeExp")

Color by continent, overlay a box and whisker plot, and add the observed values.

ggviolin(gap07, x = "continent", y = "lifeExp", 
         fill = "continent", palette = "jco",
         add = c("boxplot", "jitter"),
         ylab = "Life expectancy (yrs)")

You can also make them horizontal.

ggviolin(gap07, x = "continent", y = "lifeExp", 
         fill = "continent", palette = "jco",
         add = c("boxplot", "jitter"),
         ylab = "Life expectancy (yrs)",
         rotate = TRUE)


Bar Plots

Make bar plots by country (the height of the bar is the life expectancy).

ggbarplot(gap07,
          x = "country",
          y = "lifeExp")

Color by continent, rotate the country labels and reduce the font size, and add axis labels.

ggbarplot(gap07,
          x = "country",
          y = "lifeExp",
          fill = "continent",
          palette = "jco",
          x.text.angle = 90,
          ylab = "Life expectancy (yrs)",
          xlab = "Country") +
  font("x.text", size = 4)

Reorganize the plot.

ggbarplot(gap07,
          x = "country",
          y = "lifeExp",
          fill = "continent",
          palette = "jco",
          sort.val = "desc",
          sort.by.groups = FALSE,
          x.text.angle = 90,
          ylab = "Life expectancy (yrs)",
          xlab = "Country") +
  font("x.text", size = 4)


Dot Plots / Cleveland Plots

Create a dot plot.

ggdotchart(gap07,
           x = "country",
           y = "lifeExp",
           color = "continent",
           palette = "jco",
           sorting = "descending",
           rotate = TRUE,
           group = "continent",
           add = "segments",
           ylab = "Life expectancy (yrs)",
           xlab = "Country") +
  font("y.text", size = 4)


Adding Comparisons to Plots

Make a new subset of the data.

gap_sub = gap %>% 
  filter(continent %in% c("Asia", "Africa"),
         year %in% c(1957, 1982, 2007))

Make a boxplot of the life expectancy values for the two continents. Colored them by each continent, and overlay jittered observations.

ggboxplot(gap_sub, x = "continent", y = "lifeExp", 
          ylab = "Years", col = "continent", add = "jitter")

Test to see whether the mean life expectancy differs between the two continents. This will use the Wilcoxon rank sum test by default.

ggboxplot(gap_sub, x = "continent", y = "lifeExp", 
          ylab = "Years", col = "continent", add = "jitter") + 
  stat_compare_means(label.y = 90)

You can use a t-test instead.

ggboxplot(gap_sub, x = "continent", y = "lifeExp", 
          ylab = "Years", col = "continent", add = "jitter") + 
  stat_compare_means(method = "t.test", label.y = 90)

We can facet the boxplots by year.

ggboxplot(gap_sub, x = "continent", y = "lifeExp", 
          ylab = "Years", col = "continent", add = "jitter", facet.by = "year") + 
  stat_compare_means(method = "t.test", label.y = 90)

We can also compare multiple groups.

ggboxplot(gap_sub, x = "year", y = "lifeExp") + 
  stat_compare_means(label.y = 80, method = "anova")

First, we have to make a list containing all the pairs of comparisons we want to test.

comps = list( c('1957', '1982'), 
              c('1957', '2007'), 
              c('1982', '2007'))
comps
## [[1]]
## [1] "1957" "1982"
## 
## [[2]]
## [1] "1957" "2007"
## 
## [[3]]
## [1] "1982" "2007"

Then, we can ask for t-tests between all the comparisons in the list.

ggboxplot(gap_sub, x = "year", y = "lifeExp", ylab = "Years") + 
  stat_compare_means(method = "t.test", comparisons = comps, 
                     bracket.size = .6, size = 4)

We can also add the original comparison back in.

ggboxplot(gap_sub, x = "year", y = "lifeExp", ylab = "Years") + 
  stat_compare_means(method = "t.test", comparisons = comps, 
                     bracket.size = .6, size = 4) + 
  stat_compare_means(label.y = 110, method = "anova")

We can also facet the plots by continent.

ggboxplot(gap_sub, x = "year", y = "lifeExp", ylab = "Years", facet.by = "continent") + 
  stat_compare_means(method = "t.test", comparisons = comps, 
                     bracket.size = .6, size = 4) + 
  stat_compare_means(label.y = 110, method = "anova")

Another option is to choose one of the groups as the reference, and compare the other groups to this.

ggboxplot(gap_sub, x = "year", y = "lifeExp", ylab = "Years", facet.by = "continent") + 
  stat_compare_means(method = "t.test", ref.group = "1957") + 
  stat_compare_means(label.y = 110, method = "anova")

We can use asterisks to make this more coherent.

ggboxplot(gap_sub, x = "year", y = "lifeExp", ylab = "Years", facet.by = "continent") + 
  stat_compare_means(label = "p.signif", method = "t.test",
                     ref.group = "1957") + 
  stat_compare_means(label.y = 110, method = "anova")