0

I would like to add a sum of total HR's to my ggplot histogram, but am getting stuck. I was hoping to create a histogram of the 2019, 2018, 2017, etc.. MLB seasons, and show the distribution of HR's hit across the league. I thought it would be helpful to include the total HR's hit each year.

My code is below:

ggplot(Batting_2019, aes(hr2019))+
  geom_histogram(aes(y=..density..),
                 breaks=seq(0,60, by=3), col="white",fill="navy", alpha=.75)+
  labs(title = "Home Runs hit in 2019")+
  xlab("Home Run Totals")+
  ylab("Frequency")+
  geom_density(col=2)
Eduardo
  • 4,282
  • 2
  • 49
  • 63
  • 6
    How can we guess the structure of your dataset ? Can you provide a reproducible example of your dataset by following this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ? – dc37 Mar 29 '20 at 21:16

1 Answers1

0

you can access the ggplot data with the gg_build() - function

I give an minimal example with rnorm(1:100) which has a total count of.. (who guessed?) n = 100

## create random df
df<-as.data.frame(rnorm(1:100))
colnames(df)<-"value"

# create histogram
p <- ggplot(df, aes(value)) + geom_histogram()

# get data out of histogram
pg <- ggplot_build(p)

# calculate total of counts
total <- sum(pg$data[[1]]$count)

> total
[1] 100

### Homerun!! 

you can use this method to get even more insights : just check the pg object

View(pg)

user12256545
  • 2,755
  • 4
  • 14
  • 28