Adding X Axis Bin Labels and Percents/Counts to Top of Bins

Hello all...working on a project here and my code is below....few questions..

How can I add labels to the X axis bins? Below each bin I'd like for them to say 0.0-1.0, 1.1-2.0, etc...

How can I add percents and total counts to the TOP of these bins on the histogram?

I'm not summing anything up on the Y axis. Is there a way to remove the Y axis counts? As you can see from my graph label below I don't want anything there but count is being added my default.

library(ggplot2)
library(dplyr)
library(base)
library(dplyr)
library(tidyverse)
WAR <- read.csv("WAR.csv")
View(WAR)
## Only Pitchers displayed in a DF
pitchers <- filter(WAR, Type == "Pitcher")
View(pitchers)
##2020 pitchers only
pitchers20 <- filter(pitchers, year == 2020)
View(pitchers20)
##2021 pitchers only
pitchers21 <- filter(pitchers, year == 2021)
View(pitchers21)
##Only Hitters displayed in a DF
hitters <- filter(WAR, Type == "Hitter")
##2020 hitters only
hitters20 <- filter(hitters, year == 2020)
##2021 hitters only
hitters21 <- filter(hitters, year == 2021)
##2020 all WAR
WAR20 <- filter (WAR, year ==2020)
#2021 all WAR
WAR21 <- filter( WAR, year == 2021)

#2021 WAR histogram with custom bins and scales
ggplot(WAR21, aes(x=WAR))+
  geom_histogram(fill='steelblue', col='black', bins=12)+
  labs(title = "2021 MLB fWAR Distribution, No PA/IP Minimums")+
  scale_x_continuous(breaks = seq(-2.,8.5, by = 1.00))+
  ylab ("NA dont want a Y axis")+
  xlab("fWAR")+
  theme(axis.title.y = element_text(color="#993333", size=13, face="bold"))+
  theme(axis.title.x = element_text(color="#993333", size=13, face="bold"))+
  theme(plot.title = element_text(color="Dark Red", size=14, face="bold.italic"))+
  theme(axis.text.x = element_text(color = "dark red", size = 9, face ="bold"))

##above how do I label them 0-1, 1-2, etc. on the X axis?```

Since WAR.csv was not shared, I substituted the mtcars dataset in the example below, preserving as much of your code as possible. My updates are marked with # update:

  • Counts above the bars were added using a summary data set, mtcars_labels, within the geom_text() function
  • The y axis label was removed using ylab("")
  • Bin labels were added within scale_x_continuous via the labels argument. Note, this vector needs to be the same length as the breaks.
  • y axis counts and ticks were removed within theme() with axis.text.y and axis.ticks.y
library(tidyverse)

mtcars_labels = mtcars %>%
  count(cyl)

ggplot(mtcars, aes(x=cyl))+
  geom_histogram(fill='steelblue', col='black', bins=3)+
  # update: adds counts above bars
  geom_text(data = mtcars_labels, aes(x = cyl, y = n, label = n), vjust = -0.8, size = 3) +
  labs(title = "2021 MLB fWAR Distribution, No PA/IP Minimums")+
  scale_x_continuous(breaks = seq(4, 8, 2),
                     # update: bin labels (same length as breaks)
                     labels = paste0('bin_', seq(1:3)) 
                     )+
  # update: removes y-axis label
  ylab ("")+ 
  xlab("fWAR")+
  # update: removes y-axis counts
  theme(axis.text.y = element_blank()) + 
  # update: removes y-axis ticks
  theme(axis.ticks.y = element_blank()) + 
  theme(axis.title.y = element_text(color="#993333", size=13, face="bold"))+
  theme(axis.title.x = element_text(color="#993333", size=13, face="bold"))+
  theme(plot.title = element_text(color="Dark Red", size=14, face="bold.italic"))+
  theme(axis.text.x = element_text(color = "dark red", size = 9, face ="bold"))

image

Thanks for this. Very helpful. I've made amendments and here is my code:

#Summary counts of the datasets
WAR21_labels = WAR21 %>% 
  count(WAR)
ggplot(WAR21, aes(x=WAR))+
  geom_histogram(fill='steelblue', col='black', bins=12)+
  geom_text(data = WAR21_labels, aes(x = WAR, y = n, label = n), vjust = -0.8, size = 3) +
  labs(title = "2021 MLB fWAR Distribution, No PA/IP Minimums")+
  scale_x_continuous(breaks = seq(4, 8, 2),
                     # updating bin labels (same length as breaks)
                     labels = paste0('bin_', seq(1:3)) 
  )+
  ylab ("")+
  xlab("fWAR")+
  # updating to removes y-axis counts and ticks
  theme(axis.text.y = element_blank()) + 
  theme(axis.ticks.y = element_blank()) + 
  theme(axis.title.y = element_text(color="#993333", size=13, face="bold"))+
  theme(axis.title.x = element_text(color="#993333", size=13, face="bold"))+
  theme(plot.title = element_text(color="Dark Red", size=14, face="bold.italic"))+
  theme(axis.text.x = element_text(color = "dark red", size = 9, face ="bold"))

This is my resulting viz:

I've eliminated Y axis counts and ticks. How do I edit the bins? It's only showing two bins but I need 12 and to sum all those numbers in each bin instead of all the extra numbers.

I am confused when you changed scale_x_continuous breaks...I had this originally below denoting to start at -2.0 all the way up to 8.5 and separated by 1.00. Why did you change to 4,8, 2?

scale_x_continuous(breaks = seq(-2.,8.5, by = 1.00))+

Since WAR.csv was not shared, I used the mtcars data set and altered the ggplot code to fit this data (plot was of cyl, which only has options of 4/6/8, so that's why I chose seq(4,8,2) for breaks).

In your case, since you need 12 bins, try the following code. I changed the breaks to go to 9 to create a vector of length 12. Change the labels to be a vector of length 12 of your desired outputs ("bin_1" etc. is used for illustration). These lengths need to be in sync.

scale_x_continuous(breaks = seq(-2, 9, by = 1.00),
                   labels = paste0('bin_', seq(1:12))                      

As for WAR21_labels, since the length of your bins is 12, you will need to group by and aggregate in a way that produces the 12 values you want displayed above the bars.

1 Like

Thanks,. That makes sense about the breaks and lengths needing to be in sync.

This topic was automatically closed 42 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.