Categorizing and rescaling based on value in R

Viewed 17

I have a dataset (from Powerlifting) with columns for weightclass (with 8 unique values) and a score. I'm interested in how the weightclasses affect the score ("TotalKg"). So far I have something like

ggplot(dat[dat["Sex"] == 'M',], aes(x = TotalKg, fill = WeightClassKg)) + geom_histogram(position = "identity", alpha = 0.3, binwidth=5)

which gives me enter image description here The different weightclasses have a very different amount of competitors, e.g. many times more men compete at <93kg than at <59kg. As we can see this results in the Integrals of the various weightclasses to be vastly different and the more popular weightclasses dominate the histogram. I'd like to normalize the various weightclasses such that the y-axis is scaled by the size of the individual weightclass such that the integral of the overall weightclass is the same for all weightclasses. I can iterate over the weightclasses and add seperate subplots that I scale individually based on the size of the current weightclass, but I would assume that there's an more direct way to do this?

1 Answers

I was looking for after_stat(density), e.g.

ggplot(dat[dat["Sex"] == 'M',], aes(x = TotalKg, after_stat(density), fill = WeightClassKg)) + geom_histogram(position = "identity", alpha = 0.3, binwidth=5)

which produces enter image description here

Related