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):
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()
2) Density plot
ggplot(iris, aes_string(x = "Petal.Length", fill = "Species"))+
geom_density()
Continuous vs continuous variables
3) Scatter plot
ggplot(iris, aes_string(x = "Sepal.Length", y = "Petal.Length", color = "Species"))+
geom_point()
4) Line plot
ggplot(iris, aes_string(x = "Sepal.Length", y = "Petal.Length", color = "Species"))+
geom_line()
Continuous vs categorical variables
5) Scatter plot
ggplot(iris, aes_string(x = "Species", y = "Petal.Length", color = "Species"))+
geom_point()
6) Jitter plot
ggplot(iris, aes_string(x = "Species", y = "Petal.Length", color = "Species"))+
geom_jitter()
7) Dot plot
ggplot(iris, aes_string(x = "Species", y = "Petal.Length", fill = "Species"))+
geom_dotplot()
8) Box plot
ggplot(iris, aes_string(x = "Species", y = "Petal.Length", fill = "Species"))+
geom_boxplot()
9) Violin plot
ggplot(iris, aes_string(x = "Species", y = "Petal.Length", fill = "Species"))+
geom_violin()
10) Bar plot
ggplot(iris, aes_string(x = "Species", y = "Petal.Length", fill = "Species"))+
geom_bar(stat = "identity")
11) Pie chart
ggplot(iris, aes_string(x = factor(1), y = "Petal.Length", fill = "Species"))+
geom_bar(stat = "identity")+
coord_polar("y", start = 0)
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:
ggplot(iris_table, aes_string(x = "Species", y = "Random.Species",
fill = "Freq"))+
geom_tile()
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?