Home > Back-end >  How can I create unique vertical lines for each facet in my histogram
How can I create unique vertical lines for each facet in my histogram

Time:12-26

I want to display two histograms for scores on a survey, one for female and one for male. Now I want to add vertical lines to display their respective means, but my problem is that with my first approach I have both means displayed in each facet. So i tried to look up solutions and what I have found so far is that I should create a second data frame which contains the means, so I tried using the code I originally used for calculating the means in the first place

demo_joined%>%
  filter(!is.na(overall_score))%>%
  group_by(gender)%>%
  summarize(mean_score = mean(overall_score)) -> means

Plugging this into the rest of my code for the plot looked like this for me:

demo_joined%>%
  filter(!is.na(overall_score))%>%
  ggplot   geom_histogram(aes(overall_score))    facet_grid(.~gender)   
  geom_vline(data = means, xintercept = mean_score)

However geom_vline doesnt seem to understand that I want it to look at my means data frame, as the error message is this:

Error in new_data_frame(list(xintercept = xintercept)) : 
  object 'mean_score' not found
In addition: Warning message:
geom_vline(): Ignoring `data` because `xintercept` was provided.

Thanks a lot in advance for any help

Oke sorry this is my first StackOverflow post already wrote everything into the other category, I hope this is fine

CodePudding user response:

Using additional data for the mean is not advised, as complicated things unnecessarily. Instead, you should calculate the mean by using mutate() so every observation also shows the group mean. Then you can plot the mean within one command. I made an example with the iris dataset. This should work for you.

iris %>%
  group_by(Species) %>%
  mutate(mean= mean(Sepal.Length)) %>%
  ungroup %>%
  ggplot(aes(x=Sepal.Length)) 
  geom_histogram() 
  geom_vline(aes(xintercept = mean), color="red") 
  facet_wrap(~Species)

enter image description here

CodePudding user response:

Here is a base version of what Nick made.

library(ggplot2)

species.means <- aggregate(Sepal.Length ~ Species, data = iris, FUN = mean)

ggplot(iris, aes(x = Sepal.Length))  
  theme_bw()  
  geom_histogram()  
  geom_vline(data = species.means, mapping = aes(xintercept = Sepal.Length), color = "red")  
  facet_wrap(~ Species)
  • Related