Graphics in R (Gallery with Examples)

 

This page shows an overview of (almost all) different types of graphics, plots, charts, diagrams, and figures of the R programming language.

Here is a list of all graph types that are illustrated in this article:

Each type of graphic is illustrated with some basic example code. These codes are based on the following data:

set.seed(123)                        # Set seed for reproducibility
x <- rnorm(30)                       # Create x variable
y <- x + rnorm(30)                   # Create correlated y variable

In each section, you can find additional resources on how to create and modify these graphic types yourself (including reproducible R syntax and many examples).

In addition, this article contains a list of tutorials for general plot modifications in:

So without further ado, let’s dive in!

 

Barplot

Barplot Definition: A barplot (or barchart; bargraph) illustrates the association between a numeric and a categorical variable. The barplot represents each category as a bar and reflects the corresponding numeric value with the bar’s size.

The following R syntax shows how to draw a basic barplot in R:

barplot(x)                           # Draw barplot in R

Our example barplot looks a follows:

Example Barchart in R

Advanced Barplots: Find some advanced barplots below. Click on the images to get more information and example R codes for each of the barplots.

Stacked Bargraph in Base R
Grouped Bargraph in Base R
Horizontal Barchart in Base R
ggplot2 Barchart
Bargraph in ggplot2
Bargraph in plotly

Barplot Resources: Find some further resources on the creation of barplots below.

Barplot Video Tutorial: The following video shows a tutorial on creating barplots in R.

 

Boxplot

Boxplot Definition: A boxplot (or box-and-whisker plot) displays the distribution of a numerical variable based on five summary statistics: minimum non-outlier; first quartile; median; third quartile; and maximum non-outlier. Furthermore, boxplots show the positioning of outliers and whether the data is skewed.

The following R syntax shows how to draw a basic boxplot in R:

boxplot(x)                           # Draw boxplot in R

Example Boxplot in R

Advanced Boxplots: Find some advanced boxplots below. Click on the images to get more information and example R codes for each of the boxplots.

R Boxplot of Integer Variable with jitter R Function Nice Color
Boxplot with Groups
Boxplot with Colors
Horizontal Boxplots
Boxplot in ggplot2

 

Boxplot Resources: Find some further resources on the creation of boxplots below.

Boxplot Video Tutorial: The following video shows a tutorial on creating boxplots in R.

 

Density Plot

Density Plot Definition: A density plot (or kernel density plot; density trace graph) shows the distribution of a numerical variable over a continuous interval. Peaks of a density plot visualize where the values of numerical variables are concentrated.

The following R syntax shows how to draw a basic density plot in R:

plot(density(x))                     # Draw density plot in R

Example Density Plot in R

Advanced Density Plots: Find some advanced density plots below. Click on the images to get more information and example R codes for each of the density plots.

Multiple Normally Distributed Density Plots in R
Multiple Kernel Density Plots in Same Graph

 

Density Plot Resources: Find some further resources on the creation of density plots below.

 

Heatmap

Heatmap Definition: A heatmap (or shading matrix) visualizes individual values of a matrix with colors. More common values are typically indicated by brighter reddish colors and less common values are typically indicated by darker colors.

The following R syntax shows how to draw a basic heatmap in R:

heatmap(cbind(x, y))                 # Draw heatmap in R

Example Heatmap in R

Advanced Heatmaps: Find some advanced heatmaps below. Click on the images to get more information and example R codes for each of the heatmaps .

Heatmap Base R
Heatmap ggplot2 Package
Heatmap plotly Package

Heatmap Resources: Find some further resources on the creation of heatmaps below.

Heatmap Video Tutorial: The following video shows a tutorial on creating heatmaps in R.

 

Line Plot

Line Plot Definition: A line plot (or line graph; line chart) visualizes values along a sequence (e.g. over time). Line plots consist of an x-axis and a y-axis. The x-axis usually displays the sequence and the y-axis the values corresponding to each point of the sequence.

The following R syntax shows how to draw a basic line plot in R:

plot(1:length(y), y, type = "l")     # Draw line plot in R

Example Line Plot in R

Advanced Line Plots: Find some advanced line plots below. Click on the images to get more information and example R codes for each of the line plots.

Multiple Lines in Graph
Thick Line
ggplot2 Line Graph
lowess R function example - graphic 3 - after application of different smoother spans
cumsum graph R

Line Plot Resources: Find some further resources on the creation of line plots below.

 

Histogram

Histogram Definition: A histogram groups continuous data into ranges and plots this data as bars. The height of each bar shows the amount of observations within each range.

The following R syntax shows how to draw a basic histogram in R:

hist(x)                              # Draw histogram in R

Example Histogram in R

Advanced Histograms: Find some advanced histograms below. Click on the images to get more information and example R codes for each of the histograms.

Histogram with Colors
Histogram with Density Plot
Histogram with Non-Unified Bars
ggplot2 Histogram with Colors
Histogram with Density Plot in ggplot2
Two Histograms in Same ggplot2 Plot

Histogram Resources: Find some further resources on the creation of histograms below.

Histogram Video Tutorial: The following video shows a tutorial on creating histograms in R.

 

Pairs Plot

Pairs Plot Definition: A pairs plot is a plot matrix, consisting of scatterplots for each variable-combination of a data frame.

The following R syntax shows how to draw a basic pairs plot in R:

pairs(data.frame(x, y))              # Draw pairs plot in R

basic pairs plot in r

Advanced Pairs Plots: Find some advanced pairs plots below. Click on the images to get more information and example R codes for each of the pairs plots.

R Programming Plot via Pairs Function with Modified Color by Group
ggpairs R plot via ggplot2 & GGally

 

Pairs Plot Resources: Find some further resources on the creation of pairs plots below.

 

Polygon Plot

Polygon Plot Definition: A polygon plot displays a plane geometric figure (i.e. a polygon) within the plot.

The following R syntax shows how to draw a basic polygon plot in R:

plot(1, 1,                           # Draw polygon plot in R
     col = "white",
     xlab = "X", ylab = "Y")
polygon(x = c(0.7, 1.3, 1.3, 0.8),
        y = c(0.6, 1.0, 1.4, 1.3),
        col = "#353436")

basic polygon plot in r

Advanced Polygon Plots: Find some advanced polygon plots below. Click on the images to get more information and example R codes for each of the polygon plots.

Figure 2 Red Border Polygon in R Programming
Figure 3 Frequency Polygon in R
Figure 5 Polygon of Density for Specific Range
Figure 6 Polygon Between Two Densities

 

Polygon Plot Resources: Find some further resources on the creation of polygon plots below.

 

QQplot

QQplot Definition: A QQplot (or Quantile-Quantile plot; Quantile-Quantile diagram) determines whether two data sources come from a common distribution. QQplots draw the quantiles of the two numerical data sources against each other. If both data sources come from the same distribution, the points fall on a 45 degree angle.

The following R syntax shows how to draw a basic QQplot in R:

qqplot(x, y)                         # Draw QQplot in R

Example QQ Plot in R

Advanced QQplots: Find some advanced QQplots below. Click on the images to get more information and example R codes for each of the QQplots.

QQplot in Base R
QQplot in ggplot2

QQplot Resources: Find some further resources on the creation of QQplots below.

 

Scatterplot

Scatterplot Definition: A scatterplot (or scatter plot; scatter graph; scatter chart; scattergram; scatter diagram) displays two numerical variables with points, whereby each point represents the value of one variable on the x-axis and the value of the other variable on the y-axis.

The following R syntax shows how to draw a basic scatterplot in R:

plot(x, y)                           # Draw scatterplot in R

Example Scatterplot in R

Advanced Scatterplots: Find some advanced scatterplots below. Click on the images to get more information and example R codes for each of the scatterplots.

multiple plots combined
Scatterplot with Legend
Scatterplot in ggplot2
Scatterplot in lattice
Empirical Cumulative Distribution Function

 

Scatterplot Resources: Find some further resources on the creation of scatterplots below.

 

Venn Diagram

Venn Diagram Definition: A venn diagram (or primary diagram; set diagram; logic diagram) illustrates all possible logical relations between certain data characteristics. Each characteristic is represented as a circle, whereby overlapping parts of the circles illustrate elements that have both characteristics at the same time.

The following R syntax shows how to draw a basic venn diagram in R:

install.packages("VennDiagram")      # Install VennDiagram package
library("VennDiagram")               # Load VennDiagram package
plot.new()                           # Draw empty plot
draw.single.venn(area = 10)          # Draw venn diagram

Example Venn Diagram in R

Advanced Venn Diagrams: Find some advanced venn diagrams below. Click on the images to get more information and example R codes for each of the venn diagrams.

pairwise venn diagram
Venn Diagram with Three Sets
Venn Diagram with Several Color Codes

Venn Diagram Resources: Find some further resources on the creation of venn diagrams below.

Venn Diagram Video Tutorial: The following video shows a tutorial on creating venn diagrams in R.

 

General Modification of Plots

In the previous part of this article, I have shown you many different types of plots. However, there are plenty of programming tricks for the modification of plots in general. In the following, you will find a list of tutorials that explain such general modifications of plots in R.

Base R Plots

ggplot2

 

Learn More About Plots in R

This tutorial showed an overview of many different graphics and plots of the R programming language. If you want to learn more details about the creation of plots in R, I can recommend the following YouTube video of the DataCamp YouTube channel:

 

 

If you want to learn more about the R programming language in general, you could have a look at the following two links. They show a list of useful R functions…

… and give an overview of all R programming tutorials on this website:

I hope you liked this gallery of R graphics! If you have further questions or any kind of feedback, don’t hesitate to let me know in the comments below.

Also, don’t forget to subscribe to my free statistics newsletter for regular updates on programming and statistics!

 



 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top