Overlay histogram with frquency polygon using ggplot2

Hello, I don't know how to overlay a histogram with a frequency polygon in the same graphic using ggplot2, it's for a work and I can only use ggplot2.
This is my code with the definition of the histogram and the frequency polygon

library(ggplot2)
library(readxl)

data <- countries_of_the_world

#Histogram
qplot(x = data$Deathrate,
geom = "histogram",
xlab = "Deathrate",
ylab = "Frequency",
main = "Histogram of Cumulative Relative Frequencies",
col = I("darkgreen"),
fill = I("green"))

#Frequency polygon
qplot(x = data$Deathrate,
geom = "freqpoly",
xlab = "Deathrate",
ylab = "Frequency",
main = "Histogram of Cumulative Relative Frequencies",
col = I("blue"))

Here is an example of overlaying a histogram and frequency polygon with ggplot using some invented data.

library(ggplot2)
DF <- data.frame(DeathRate = rnorm(200, mean = 1, sd = 0.25))
ggplot(DF, aes(x = DeathRate)) + 
  geom_histogram(fill = "darkgreen") +
  geom_freqpoly(color = "blue") +
  labs(x = "Death Rate", y = "Frequency", title = "Histogram and Freq. Polygon")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2023-12-30 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.