SecretIndividual
SecretIndividual

Reputation: 2519

Adding mean to a histogram in ggplot

I created a histogram for the distance variable of the flights data frame in the nycflights13 package.

Now I am trying to add a vertical line to the histogram that marks the mean, like so:

library(nycflights13)
attach(flights)
ggplot(flights, aes(x = distance)) +
  geom_histogram(aes(y = ..density..), 
                 color = "darkblue", 
                 fill = "lightblue", 
                 binwidth = 250) +
  labs(x = "Departure delay (minutes)") +
  geom_vline(xintercept = mean(distance, color = black, size = 1.5)) +
  geom_density(color = "red", size = 1)

This gives me the output:

Warning messages:
1: In mean.default(distance, color = black, size = 1.5) :
  argument is not numeric or logical: returning NA
2: Removed 1 rows containing missing values (geom_vline).

How do I fix this? I tried removing the NA values but I guess not in the correct way.

Upvotes: 2

Views: 217

Answers (1)

jay.sf
jay.sf

Reputation: 72673

You need to put in the mean as a scalar. Thus we can use $ here. Put in the color as a string "black".

library(nycflights13)
# attach(flights)  # I strongly recommend not to use attach
library(ggplot2)
ggplot(flights, aes(x = distance)) +
  geom_histogram(aes(y = ..density..), 
                 color = "darkblue", 
                 fill = "lightblue", 
                 binwidth = 250) +
  labs(x = "Departure delay (minutes)") +
  geom_vline(xintercept = mean(flights$distance),
             color="black", size=1.5) +
  geom_density(color = "red", size = 1)

enter image description here

Upvotes: 2

Related Questions