Open In App

Ggpubr in R

Last Updated : 30 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Data visualization: Ggpubr is a package that allows users to create various types of plots and charts in R, such as scatter plots, bar charts, and line graphs.
  • Grouped data: Ggpubr allows users to group data by different categories, such as by color, shape, or size, in order to better visualize patterns and trends in the data.
  • Customization: Ggpubr offers a wide range of options for customizing the appearance of plots and charts, including the ability to change colors, labels, axis scales, and more.
  • Data manipulation: Ggpubr also includes functions for manipulating and transforming data, such as for summarizing and aggregating data, or for reordering or filtering data.
  • Statistical analysis: Ggpubr can be used in conjunction with other R packages for statistical analysis, such as for creating box plots, histograms, or other types of plots to visualize data distributions or patterns.
  • Publication-quality graphics: Ggpubr is designed to create high-quality plots and charts that can be easily exported for use in publications, presentations, or other types of documents.

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. 

R




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.

R




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


Output:

 

Violin plots:

R




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:

R




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

R




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.

R




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:

 



Like Article
Suggest improvement
Share your thoughts in the comments