10 quick plots in R with ggplot2

With the  ggplot2 package in R it is possible to create almost any type of chart. Sometimes it takes me more time than I want to just create a simple plot, because I forgot what the data structure should look like or the syntax of the formulas. Therefore, I’ve listed 10 plots below with codes to make it as easy as possible to use whenever I need them.

  1. Barchart
  2. Histogram
  3. Line chart
  4. Line chart with confidence interval
  5. Coordinate plot
  6. Donut chart
  7. Scatter plot
  8. Scatter plot with shapes
  9. Boxplot
  10. Correlation plot

 

Data preparation

So let’s start with the prep stuff. First install and unpack the  ggplot2 package. Second create a simple dataframe with some dimensions and measures. I’ve kept it as simple as possible, so this is what the dataframe looks like:

This is all the data we need for the next 10 charts.

 

1. Bar chart

So the very simplest chart is (no surprise) the bar chart.

 

2. Histogram

Next chart is the histogram. I added a grey border around the bins, by default it won’t show space between the bars.

Histogram with ggplot2 in R

 

3. Line chart

Just like the bar chart, the line chart doesn’t need any arguments in the function.

Linechart with ggplot2 in R

 

4. Line chart with confidence interval

Next up is the line chart from above, but then we add a confidence interval.

Linechart with confidence interval with ggplot2 in R

 

5. Coordinate plot

A colorful coordinate plot showing the frequency of the dimensions.

Coordinate plot with ggplot2 in R

 

6. Donut chart

The exact same information as the coordinate plot, but then presented differently. Simply change the theta into  y.

Donut chart with ggplot2 in R

 

7. Scatter plot

A scatter plot with simple dots.

Scatter plot with ggplot2 in R

 

8. Scatter plot with shapes

The same scatter plot as previous plot, but then with shapes.

Scatter plot with shapes with ggplot2 in R

 

9. Boxplot

Multiple boxplots in one chart with the following code:

Boxplot with ggplot2 in R

 

10. Correlation plot

And last but not least, the correlation plot. This is just a simple one, read my other post on how to plot a correlation matrix in 4 different ways.

Correlation plot with ggplot2 in R

 

So these are the 10 easy plots created with ggplot2 . If you want to know more variations, then check out this amazing cheatsheet from Rstudio.

World full of data author

Who I am


Hi! My name is Claudia, a freelance data analyst/scientist. This is my space on the internet where I share knowledge and experience with everyone who wants to become a better analyst. Read more about my work as a freelancer here.

Share this post on

Share this post on

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.