eye-catching density plots in R

infoart.ca
Feb 16, 2022

--

A density plot on the fly:

cols <- c(“#2ECBE9”, “#E998D1”, “#F5B7AB”)cols <- c("#2ECBE9", "#E998D1", "#F5B7AB")
ggplot(iris, aes(x = df$Sepal.Length, fill = df$Species)) +
geom_density(alpha = 0.8, color = NA) +
scale_fill_manual(name = "Species",values = cols) + labs(y = "Petal length (cm)", x = "Sepal length (cm)")

The defaults size of the fonts are not good for eyes, so let’s customize the configurations:

mt= theme(
axis.title.x = element_text(size = 16),
axis.text.x = element_text(size = 16),
axis.text.y = element_text(size = 16),
axis.title.y = element_text(size = 16),
legend.text=element_text(size=16))

and append to the plot:

plot + mt

Optionally, the overlapping view can be undone by the ggridges package:

library(ggridges)ggplot(iris, aes(x = iris$Sepal.Length, y = Species, fill = Species)) + geom_density_ridges() + labs(y = "Petal length (cm)", x = "Sepal length (cm)")->gg

Enjoy!

--

--

infoart.ca
infoart.ca

Written by infoart.ca

Center for Social Capital & Environmental Research | Posts by Bishwajit Ghose, BI consultant and lecturer at the University of Ottawa

No responses yet