library(ggplot2)
library(dplyr)
library(scales)
mlbbat10_200 <- mlbbat10 %>%
filter(mlbbat10$at_bat > 200)
# On-base percentage across positions
ggplot(mlbbat10_200, aes(x = position, y = obp, fill = position)) +
geom_boxplot(show.legend = FALSE) +
scale_y_continuous(labels = label_number(suffix = "%", accuracy = 0.01)) +
labs(
title = "On-base percentage across positions",
y = "On-base percentage across positions",
x = "Position"
)
# Batting average across positions
ggplot(mlbbat10_200, aes(x = bat_avg, fill = position)) +
geom_density(alpha = 0.5) +
labs(
title = "Batting average across positions",
fill = NULL,
y = "Batting average",
x = "Position"
)
# Mean number of home runs across positions
mlbbat10_200 %>%
group_by(position) %>%
summarise(mean_home_run = mean(home_run)) %>%
ggplot(aes(x = position, y = mean_home_run, fill = position)) +
geom_col(show.legend = FALSE) +
labs(
title = "Mean number of home runs across positions",
y = "Home runs",
x = "Position"
)
# Runs batted in across positions
ggplot(mlbbat10_200, aes(x = run, y = obp, fill = position)) +
geom_boxplot(show.legend = FALSE) +
labs(
title = "Runs batted in across positions",
y = "Runs",
x = "Position"
)
Run the code above in your browser using DataLab