12 types of plots that are easily made in R

In this blog post, I will cover 12 of the most common types of plots in R using ggplot2. To begin, we will load the ggplot2 package and the iris dataset, which is one of the many built-in datasets in R. Then, we will review 12 types of graphs.

Load the ggplot2 package

library(ggplot2)

Load the iris dataset

data("iris")

The iris data set contains 150 observations of 5 variables. Four of them are continuous (sepal length, sepal width, petal length and petal width) and one of them is categorical (species):

content of the iris built-in dataset

To choose the appropriate plot for your analysis, it is important to know what kind of variables you have: continuous or categorical variables?

Continuous variable

1) Histogram

ggplot(iris, aes_string(x = "Petal.Length", fill = "Species"))+
  geom_histogram()
histogram produced in R using the ggplot2 library

2) Density plot

ggplot(iris, aes_string(x = "Petal.Length", fill = "Species"))+
  geom_density()
density plot produced in R using the ggplot2 library

Continuous vs continuous variables

3) Scatter plot

ggplot(iris, aes_string(x = "Sepal.Length", y = "Petal.Length", color = "Species"))+
  geom_point()
scatter plot produced in R using the ggplot2 library

4) Line plot

ggplot(iris, aes_string(x = "Sepal.Length", y = "Petal.Length", color = "Species"))+
  geom_line()
line plot produced in R using the ggplot2 library

Continuous vs categorical variables

5) Scatter plot

ggplot(iris, aes_string(x = "Species", y = "Petal.Length", color = "Species"))+
  geom_point()
scatter plot produced in R using the ggplot2 library

6) Jitter plot

ggplot(iris, aes_string(x = "Species", y = "Petal.Length", color = "Species"))+
  geom_jitter()
jitter plot produced in R using the ggplot2 library

7) Dot plot

ggplot(iris, aes_string(x = "Species", y = "Petal.Length", fill = "Species"))+
  geom_dotplot()
dot plot produced in R using the ggplot2 library

8) Box plot

ggplot(iris, aes_string(x = "Species", y = "Petal.Length", fill = "Species"))+
  geom_boxplot()
box plot produced in R using the ggplot2 library

9) Violin plot

ggplot(iris, aes_string(x = "Species", y = "Petal.Length", fill = "Species"))+
  geom_violin()
violin plot produced in R using the ggplot2 library

10) Bar plot

ggplot(iris, aes_string(x = "Species", y = "Petal.Length", fill = "Species"))+
  geom_bar(stat = "identity")
bar plot produced in R using the ggplot2 library

11) Pie chart

ggplot(iris, aes_string(x = factor(1), y = "Petal.Length", fill = "Species"))+
  geom_bar(stat = "identity")+
  coord_polar("y", start = 0)
pie chart produced in R using the ggplot2 library

Categorical vs categorical variables

12) Heatmap

To create a heatmap, we need two categorical variables as x and y and a continuous variable as color. But the iris data set only contains one categorical variable. Therefore, I will simulate another categorical variable (random species) and then count the frequency of each combination of “Species” and “Random.Species” variables:

set.seed(10)
iris[, "Random.Species"] = sample(iris[, "Species"])
iris_table = data.frame(table(iris[, c("Species", "Random.Species")]))

Here is the content of the iris_table data frame:

table with frequency of each level of the species variable combined with each level of the randomized species variable
ggplot(iris_table, aes_string(x = "Species", y = "Random.Species",
  fill = "Freq"))+
  geom_tile()
heatmap produced in R using the ggplot2 library

Conclusion

In conclusion, I hope that this summary about some of the most common types of plots in R is helpful to you. Have you found what you were looking for?

Related posts

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply