Open In App

Ggpubr in R

ggpubr is an R package that provides a convenient interface for creating publication-ready plots using the ggplot2 package. It extends the functionality of ggplot2 by providing additional themes, scales, and geoms that can be used to create more complex and visually appealing plots. Additionally, ggpubr also includes tools for creating complex figures by combining multiple plots and adding annotations and captions. Overall, ggpubr is a useful tool for data scientists and researchers looking to create high-quality plots for publication and presentation purposes.

Concepts related to Ggpubr

To install Ggpubr in R, you can use the following command:



install.packages("ggpubr")

Once the package is installed, you can load it into your R session by running:

library(ggpubr)

You can then start using the functions provided by Ggpubr to create various types of plots. 






library(ggplot2)
  
# Create data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)
  
# Create plot
ggplot(data = data.frame(x, y), aes(x, y)) +
  geom_point() +
  ggtitle("Simple Scatter Plot") +
  xlab("X-axis") +
  ylab("Y-axis")

Output:

 

Boxplot:

Here we are going to create boxplot with ggboxplot() method.




library(ggpubr)
data(mtcars)
ggboxplot(mtcars, x = "gear", y = "mpg", color = "gear")

Output:

 

Violin plots:




library(ggplot2)
  
# Create some sample data
set.seed(123)
data <- data.frame(group = rep(c("A", "B"), each = 100),
                   value = c(rnorm(100, mean = 5),
                             rnorm(100, mean = 10)))
  
# Create the violin plot
ggplot(data, aes(x = group, y = value)) +
  geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))

In this example, the data data frame contains a column for the group variable (group) and a column for the value variable (value). The ggplot() function is used to create a new plot using the data frame, and the aes() function is used to specify that the x-axis should be mapped to the group variable and the y-axis should be mapped to the value variable. The geom_violin() function is used to add a violin plot to the plot, and the draw_quantiles option is used to specify that the quartiles should be plotted within the violin.

Output:

 

Density plots:




library(ggplot2)
  
# Create some sample data
set.seed(123)
data <- data.frame(x = rnorm(1000))
  
# Create the density plot
ggplot(data, aes(x = x)) + 
  geom_density()

In this example, the data data frame contains a single column of data (x) that will be plotted as the density. The ggplot() function is used to create a new plot using the data frame, and the aes() function is used to specify that the x-axis should be mapped to the x variable. The geom_density() function is used to add a density plot to the plot.

Output:

 

Histograms of joint violin and boxplots




library(ggplot2)
  
# Create some sample data
set.seed(123)
data <- data.frame(group = rep(c("A", "B"), each = 100),
                   value = c(rnorm(100, mean = 5), 
                             rnorm(100, mean = 10)))
  
# Create the histogram
ggplot(data, aes(x = value)) + 
  geom_histogram(binwidth = 0.5)
  
# Create the joint violin and boxplot
ggplot(data, aes(x = group, y = value)) + 
  geom_violin(draw_quantiles = c(0.25, 0.5, 0.75)) +
  geom_boxplot(width = 0.2)

Output:

 

Histograms

Histograms are used to display the distribution of a single variable. In order to visualize the distribution of two variables, we can use a 2D histogram (also known as a hexbin plot) or a scatter plot with a density plot.




library(ggplot2)
  
# Create some sample data
set.seed(123)
data <- data.frame(x = rnorm(1000),
                   y = rnorm(1000, mean = 5, sd = 2))
  
# Create the 2D histogram
ggplot(data, aes(x = x, y = y)) + 
  geom_hex(binwidth = c(0.5, 0.5))

Output:

 


Article Tags :