how to show average/mean line in ggplot

I was working with the Indian Premier League dataset on Kaggle. The d1 dataset is shown in the picture below.


Then i went to draw plot using ggplot.

library(ggplot2)
d1 %>% ggplot(mapping = aes(x = runs ,y = strikeRate)) +
geom_point(aes(size = strikeRate, color = strikeRate)) + 
labs(
    title = "Runs made with respective strikerates",
    x = "Runs",
    y = "Strike rate",
    caption = 'Data Source credit : https://www.espncricinfo.com/'
  )

Then the plot appeared. But i also wanted to show the average strikeRate line in the plot. Please guide me how to do that.
Thanks a lot.

Use geom_hline()

Very crude example as I am rushed for time.

dat1  <- data.frame(xx = 1: 20, yy = sample(1:5, 20, replace = TRUE))
mm  <- mean(dat1$yy)
ggplot(dat1, aes(xx, yy)) + geom_point() + geom_hline(yintercept = mm)
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.