0

I am trying to draw a density curve over histogram using ggplot but to no avail. dlist is a vector with numeric values.

Here is my code:

ggplot() +
  geom_histogram(aes(x=dlist), bins = 30, fill = "#B3E4F7") +
  geom_density() +
  geom_vline(aes(xintercept = mean(dlist)),
             color="#D2091F", linetype="dashed",size=1)

enter image description here

user9532692
  • 584
  • 7
  • 28

2 Answers2

2

You need to set y to ..density... For example:

ggplot(data.frame(dlist), aes(x=dlist, y = ..density..)) +
        geom_histogram(bins = 30, fill = "#B3E4F7") +
        geom_density() +
        geom_vline(aes(xintercept = mean(dlist)),
                   color="#D2091F", linetype="dashed",size=1)

A reproducible example:

library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = ..density..)) +
        geom_histogram(bins = 30,  fill = "#B3E4F7") +
        geom_density()

enter image description here

bird
  • 2,938
  • 1
  • 6
  • 27
  • dlist is a vector not a dataframe and therefore your code does not relate to my example. – user9532692 May 23 '21 at 11:55
  • @user9532692 this is just a reproducible example since you have not provided reproducible data (which is strongly recommended). This answer means that your problem should be solved by just specifying `y = ..density..` in your `aes` – bird May 23 '21 at 11:58
  • @user9532692 see the updated code (the beginning of my answer) and let me know if it works for you. If not, please share some reproducible data by using `dput()` – bird May 23 '21 at 12:00
0

The geom_desntity has no data. Put the data in the ggplot() or in all functions.

ggplot(aes(x=dlist)) +
  geom_histogram(bins = 30, fill = "#B3E4F7") +
  geom_density() +
  geom_vline(aes(xintercept = mean(dlist)),color="#D2091F", linetype="dashed",size=1)

However, if you want to compare both, you may want to plot the histogram with a density stat:

ggplot(aes(x=dlist)) +
  geom_histogram(aes(y = ..density..),bins = 30, fill = "#B3E4F7") +
  geom_density() +
  geom_vline(aes(xintercept = mean(dlist)),color="#D2091F", linetype="dashed",size=1)

If you have a numeric vector dlist, you can create a data.frame before ggplot as follows:

dlist <- rnorm(1000)
tibble(dlist = dlist) %>% 
    ggplot(aes(x=dlist)) +
      geom_histogram(aes(y = ..density..),bins = 30, fill = "#B3E4F7") +
      geom_density() +
      geom_vline(aes(xintercept = mean(dlist)),
                 color="#D2091F", linetype="dashed",size=1)
xaviescacs
  • 309
  • 1
  • 5
  • Thanks for your response. However, I get the following error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval Did you accidentally pass `aes()` to the `data` argument? – user9532692 May 23 '21 at 11:53
  • Well, ggplot expect a data.frame. If you have a vector dlist, you can create one as follows: tibble(dlist = dlist) %>% ggplot(aes(x=dlist)) + geom_histogram(aes(y = ..density..),bins = 30, fill = "#B3E4F7") + geom_density() + geom_vline(aes(xintercept = mean(dlist)), color="#D2091F", linetype="dashed",size=1) – xaviescacs May 23 '21 at 11:57