Density and histogram in the same plot using Highcharter

Hello! I'd like to have density and histogram in the same plot. I don't know why the histogram or the density have different sizes.
I've tried this:
Thank you!

``````library(highcharter)
x <- rnorm(500)
hist_x<- hist(x,freq=F,breaks=50,plot = FALSE)
hchart(hist_x,color="blue") %>%
hc_title(text="Hist with density") %>%
hc_add_series(density(x),type = "line", color = "red")
``````

The y-axes differ. The histogram uses counts, which range from 0 to some number, such as 25. The density represents the fraction of the total, which ranges from 0 to 1.

Yes, I agree, and I understand you.
That is what I want to change.
It seems that in: hist_x<- hist(x,freq=F,breaks=50,plot = FALSE)
the parameter freq=F doesn't work

Hi cruddy,

Maybe I am wrong on this, but it seems to me that `freq=F` is not working because you also use `plot=FALSE ` . Below is a reprex in base-R yielding the histograms and densities on top of each other. I broke down the structure of `hist_x` which shows the `density` part. Here it does not sum to 1, only by multiplying by `diff(breaks)`. The multiplication is exactly what `freq=F`does, if I understand correctly. In your code, the assignment throughs the warning `freq=F not used`.
Hope this helps?

JW

``````x <- rnorm(500)
hist_x<- hist(x, breaks=50, plot = FALSE)
str(hist_x)
#> List of 6
#>  \$ breaks  : num [1:60] -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2 -1.9 -1.8 -1.7 ...
#>  \$ counts  : int [1:59] 1 2 0 2 1 4 1 8 6 7 ...
#>  \$ density : num [1:59] 0.02 0.04 0 0.04 0.02 ...
#>  \$ mids    : num [1:59] -2.55 -2.45 -2.35 -2.25 -2.15 -2.05 -1.95 -1.85 -1.75 -1.65 ...
#>  \$ xname   : chr "x"
#>  \$ equidist: logi TRUE
#>  - attr(*, "class")= chr "histogram"
sum(hist_x\$density)
#> [1] 10
sum(diff(hist_x\$breaks)*hist_x\$density)
#> [1] 1
hist(x,
breaks=50,
freq=FALSE,
xlab = "x",
main = "Histogram and density plot",
col="lightgreen",
xlim=c(-5,5),
ylim=c(0,0.50))