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.

- Barchart
- Histogram
- Line chart
- Line chart with confidence interval
- Coordinate plot
- Donut chart
- Scatter plot
- Scatter plot with shapes
- Boxplot
- 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:

library(ggplot2) dataset <- data.frame(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), dim1 = c("a", "b", "c", "d", "e", "a", "b", "c", "d", "e"), dim2 = c("category 1", "category 1", "category 1", "category 2", "category 3", "category 3", "category 3", "category 4", "category 4", "category 5"), measure1 = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1), measure2 = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5), measure3 = c(4, 2, 3, 1, 4, 2, 3, 1, 4, 2), measure4 = c(5, 4, 3, 2, 1, 5, 4, 3, 2, 1), interval_high = c(1.2, 2.5, 3.3, 1.4, 2.5, 3.4, 1.4, 2.3, 3.3, 1.2), interval_low = c(0.8, 1.5, 2.7, 0.6, 1.5, 2.6, 0.6, 1.7, 2.7, 0.8) )

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.

ggplot(data = dataset, aes(measure1)) + geom_bar()

## 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.

ggplot(data = dataset, aes(measure1)) + geom_histogram(bins = length(unique(dataset$measure1)), col="grey")

## 3. Line chart

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

ggplot(data = dataset, aes(x = id, y = measure1)) + geom_line()

## 4. Line chart with confidence interval

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

ggplot(data = dataset, aes(x = id, y = measure1)) + geom_line() + geom_ribbon(aes(ymax = interval_high, ymin = interval_low), alpha = 0.5)

## 5. Coordinate plot

A colorful coordinate plot showing the frequency of the dimensions.

ggplot(data = dataset, aes(x = dim2, fill = dim2)) + geom_bar(width = 1) + coord_polar(theta = "x")

## 6. Donut chart

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

ggplot(data = dataset, aes(x = dim2, fill = dim2)) + geom_bar(width = 1) + coord_polar(theta = "y")

## 7. Scatter plot

A scatter plot with simple dots.

ggplot(data = dataset, aes(x = id, y = measure1)) + geom_point()

## 8. Scatter plot with shapes

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

ggplot(data = dataset, aes(x = id, y = measure1, shape = dim1, size = 4)) + geom_point()

## 9. Boxplot

Multiple boxplots in one chart with the following code:

ggplot(data = dataset, aes(x = dim1, y = measure1)) + geom_boxplot()

## 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.

library(reshape2) correlation_matrix <- cor(dataset[, c("measure1", "measure2", "measure3", "measure4", "interval_high", "interval_low")]) correlation_matrix[lower.tri(correlation_matrix)] <- 0 diag(correlation_matrix) <- 0 ggplot(melt(correlation_matrix), aes(Var1, Var2, fill = value)) + geom_tile() + scale_fill_gradient2(mid="white") + coord_equal()

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.