Density Plot Tutorial in R

@algoswithamber · 2025-10-13 19:37 · STEMGeeks

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)
  )
#stem #rstats #data #math #statistics #rprogramming
Payout: 0.000 HBD
Votes: 14
More interactions (upvote, reblog, reply) coming soon.