Many lives of ggplot!
ggplot has been a staple in the data visualization world since its creation. It has grown to become an incredibly powerful tool over the years, allowing users to create intricate visualizations with ease. I have been using ggplot for about 10 years now, and it continues to enchant me, even more, every single time!
In this post, I’d like to present 10 quick examples that beginners may find particularly useful.
Let’s first get the libraries right:
# libraries for plotting
library(plyr)
library(ggplot2)
library(ggridges)
1. Histogram
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_histogram()
2a. Barplot (ordered)
mtcars %>%
select(car, mpg, cyl) %>%
arrange(desc(mpg)) %>%
ggplot(aes(y=reorder(car, mpg), x=mpg)) +
geom_bar(stat='identity', aes(fill=factor(cyl)))
2b. Barplot (unordered)
ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl))) +
geom_bar() +
scale_fill_brewer(type = "qual", palette = "Set2") +
labs(x = "Cylinders", y = "Number of Cars", title = "Number of Cars by Cylinder")
3. Pie
ddply(mtcars, .(gear), summarise, count = length(gear)) %>%
ggplot(aes(x = "", y = count, fill = factor(gear))) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
scale_fill_brewer(type = "qual", palette = "Set2") +
labs(fill = "Number of Gears", title = "Number of Cars by Gear"
4. Density
ggplot(iris, aes(x = Sepal.Width, fill = Species)) +
geom_density(alpha = 0.5) +
ggtitle("Density Plot of Sepal Width by Species") +
xlab("Sepal Width") +
ylab("Density")
5. Box
ggplot(data = iris, aes(x = Species, y = Petal.Width, fill = Species)) +
geom_boxplot() +
scale_fill_brewer(palette = "Set2") +
labs(title = "Boxplot of Petal Width by Species")
6. Scatter
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point()
7. Ridge
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = Species)) +
geom_density_ridges(alpha = 0.5, scale = 1.2) +
scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73")) +
labs(title = "Ridge Plot of Sepal Length by Species",
x = "Sepal Length",
y = "Species")
8. Bubble
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, size = Petal.Width, color = Species)) +
geom_point(alpha = 0.7) +
scale_size(range = c(2, 10)) +
labs(title = "Iris dataset - Bubble plot",
x = "Sepal length",
y = "Petal length",
size = "Petal width",
color = "Species")
9. heatmap
corr_matrix <- cor(mtcars)
ggplot(reshape2::melt(as.matrix(corr_matrix)), aes(x = Var1, y = Var2, fill = value)) + geom_tile() +
scale_fill_gradient(low = "white", high = "steelblue") +
labs(title = "Correlation Heatmap of mtcars") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
10. Violin
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_violin(trim = FALSE) +
geom_boxplot(width = 0.1, fill = "white") +
labs(title = "Violin Plot of Sepal Length by Species", x = "Species", y = "Sepal Length") + scale_fill_brewer(palette = "Set1")
11. stacked bar
ggplot(diamonds, aes(x = cut, fill = clarity)) +
geom_bar(position = "stack") +
labs(title = "Stacked Bar Plot of Cut and Clarity in Diamonds", x = "Cut", y = "Count", fill = "Clarity")
12. Line
ggplot(mtcars, aes(x = rownames(mtcars), y = mpg, group = cyl, color = factor(cyl))) +
geom_line() +
labs(title = "Miles per Gallon by Car Model", x = "Car Model", y = "Miles per Gallon") +
scale_color_discrete(name = "Cylinders")
13. Lollipop
ggplot(mtcars, aes(x = rownames(mtcars), y = mpg)) +
geom_segment(aes(xend = rownames(mtcars), yend = 0, color = factor(cyl)),
size = 1.2) +
geom_point(aes(color = factor(cyl)), size = 3) +
scale_color_brewer(type = "qual", palette = "Set2") +
labs(title = "Miles per Gallon by Car Model", x = "Car Model", y = "Miles per Gallon")+theme(axis.text.x = element_text(angle = 90))
Happy ggplotting!