0

i am trying to add a fitted distribution to the histogram, but after I run it, it is just a straight line. How can i get a density line? hist(data$price) lines(density(data$price)), lwd = 2, col ="red")

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Welcome to SO! A very similar question was asked here: https://stackoverflow.com/questions/1497539/fitting-a-density-curve-to-a-histogram-in-r and is one of the top results if you search for add density line (or curve) to histogram either here on SO or on Google. – Shawn Janzen Apr 15 '21 at 08:15
  • You may also find this solution useful, which shows the challenges of a 'flat' density curve. It offers a side by side visual comparison using the same hist() and lines() functions you're already using. https://stackoverflow.com/questions/58527982/my-normal-curve-does-not-plot-correctly-with-histogram So that we can better assist you with your specific question, please provide example data or code to replicate your data. – Shawn Janzen Apr 15 '21 at 08:30

2 Answers2

0

You are using graphics function hist. Use MASS function truehist instead

MASS::truehist(data$price)
lines(density(data$price)), lwd = 2, col ="red")
Chriss Paul
  • 1,101
  • 6
  • 19
0

@Chriss gave a good solution--it does produce a density curve on top of the histogram; however, it changes the y-axis so that you only see the density values (losing the count values).

Here is an alternate solution that will place the frequency counts on the left-side y-axis and add density as a right-side y-axis. Tweak code as needed for things like bins, color, etc. I'm using the mtcars data as an example since there was no code or data provided in the question to replicate. In addition to the two libraries used here (ggpubr and cowplot), you may need to use some ggplot functions to better customize these plot options.

Code for this solution was modified from https://www.datanovia.com/en/blog/ggplot-histogram-with-density-curve-in-r-using-secondary-y-axis/

# packages needed
library(ggpubr)
library(cowplot)

# load data (none provided in the original question)
data("mtcars")

# create histogram (I have 10 bins here, but you may need a different amount)
phist <- gghistogram(mtcars, x="hp", bins=10, fill="blue", ylab="Count (blue)") + ggtitle("Car Horsepower Histogram")

# create density plot, removing many plot elements
pdens <- ggdensity(mtcars, x="hp", col="red", size=2, alpha = 0, ylab="Density (red)") +
scale_y_continuous(expand = expansion(mult = c(0, 0.05)), position = "right")  +
  theme_half_open(11, rel_small = 1) +
  rremove("x.axis")+
  rremove("xlab") +
  rremove("x.text") +
  rremove("x.ticks") +
  rremove("legend")

# overlay and display the plots
aligned_plots <- align_plots(phist, pdens, align="hv", axis="tblr")
ggdraw(aligned_plots[[1]]) + draw_plot(aligned_plots[[2]])

Histogram with frequency counts on left y-axis & density on right y-axis.

Shawn Janzen
  • 369
  • 3
  • 15
  • If your question was answered by one of us. Please close your question by clicking the checkmark to the left of the answer that helped you most. Thank you. – Shawn Janzen Apr 15 '21 at 09:26