Amber K avatar

Density Plot Tutorial in R

algoswithamber

Published: 13 Oct 2025 › Updated: 13 Oct 2025Density Plot Tutorial in R

Density Plot Tutorial in R

Hey everyone, I wanted to share this density plot tutorial that I recently created. I also provided sample code that you can use to follow along with or to modify if you want to change the colors or labels.

install.packages("tidyverse")
library(tidyverse)

head(diamonds)

ggplot(data = diamonds,
       aes(x = price, fill = cut))+geom_density()+
  labs(title = "Diamond Price",
       x = "Price",
       y = "Density",
       fill = "Legend")+
  theme(
    plot.title = element_text(hjust = .5, color = "hotpink"),
    panel.background = element_rect(fill = "black"),
    plot.background = element_rect(fill = "black"),
    axis.title = element_text(color = "hotpink"),
    axis.text = element_text(color = "hotpink"),
    legend.background = element_rect(fill = "black", color = "white"),
    legend.text = element_text(colour = "white"),
    legend.title = element_text(color = "white")
  )+coord_cartesian(
    xlim = c(0,10000),
    ylim = c(.0001,.0003)
  )

Leave Density Plot Tutorial in R to:

Written by

Ph.D.. student who loves finance, programming, and making the world a better place.

Read more #stem posts


Best Posts From Amber K

We have not curated any of algoswithamber's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Amber K