Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split data to plot histograms side-by-side in R

I am learning R with the Australian athletes data set.

By using ggplot, I can plot a histogram like this.

library(DAAG)

ggplot(ais, aes(wt, fill = sex)) + 
  geom_histogram(binwidth = 5)

enter image description here

By using summary(ais$wt), the 3rd Quartile is 84.12. Now I want to split the data by the wt 84.12. and plot 2 similar histograms accordingly (side by side)

The split is:

ais1 = ais$wt[which(ais$wt>=0 & ais$wt<=84.12)]
ais2 = ais$wt[which(ais$wt>84.12)]

But I don’t know how to fit them in the plotting. I tried but it doesn't work:

ggplot(ais1, aes(wt, fill = sex)) +...

How can I plot the histograms (2 similar histograms accordingly, side by side)?

like image 587
Mark K Avatar asked Apr 18 '26 06:04

Mark K


1 Answers

Add the split as a column to your data

ais$wt_3q = ifelse(ais$wt < 84.12, "Quartiles 1-3", "Quartile 4")

Then use facets:

ggplot(ais, aes(wt, fill = sex)) + 
  geom_histogram(binwidth = 5) +
  facet_wrap(~ wt_3q)

enter image description here

The created variable is a factor, if you specify the order of the levels you can order the facets differently (lots of questions on here showing that if you search for them - same as reordering bars for a ggplot barplot). You can also let the scales vary - look at ?facet_wrap for more details.

Generally, you shouldn't create more data frames. Creating ais1 and ais2 is usually avoidable, and your life will be simpler if you use a single data frame for a single data set. Adding a new column for grouping makes it easy to keep things organized.

like image 105
Gregor Thomas Avatar answered Apr 19 '26 21:04

Gregor Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!