Unable to plot histogram with density curve using ggplot2

r
machine_learning
data_science

#1

Nope, that did not work, again got the same error.But I used plot () and hist() to view the data…:smiley: just to continue with the code. Anyways I am attaching my code.Problem.zip (11.1 KB)


#2

Hi @prernakalra812

Since you’ve posted your question first time, few things to note:

  1. While asking questions related to errror, please share the exact error here. Solutions can be found sometimes by just looking at the error.
  2. The question of post should be the problem you are facing.
  3. Had you tagged me in the question, it was still acceptable.
  4. While sharing code, always share .R file.

Such protocols would help everyone here to understand and answer questions much faster.
Note: For now, I’ve edited the question.

Looking at the code you’ve shared, I found that the function created to plot histogram is slightly incorrect. In your code, you are passing density as a variable, whereas it’s a parameter (..density..) which activates density curve. Use this code:

#write a function
> tr <- function(a){
        ggplot(data = num_train, aes(x= a, y=..density..))+geom_histogram(fill="blue",color="red",alpha = 0.5,bins =10`Preformatted text`0) + geom_density()
        ggplotly()
}; tr(num_train$age)

#3

Thanks alot, It worked.


#4

I will be careful in the future, thanks for spotting out my mistakes.