简体   繁体   中英

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)

在此处输入图片说明

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)?

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)

在此处输入图片说明

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.

We can do this with ggarrange to arrange the plot objects for each subset

library(DAAG)
library(ggplot2)
library(ggpubr)
p2 <- ais %>%
         filter(wt>=0,  wt<=84.12) %>%
         ggplot(., aes(wt, fill = sex)) + 
            geom_histogram(binwidth = 5) +
            coord_cartesian(ylim = c(0, 30))  


p1 <- ais %>%
         filter(wt>84.12) %>%
         ggplot(., aes(wt, fill = sex)) + 
            geom_histogram(binwidth = 5) +
            coord_cartesian(ylim = c(0, 30))  



ggarrange(p1, p2, ncol =2, nrow = 1, labels = c("p1", "p2"))

-output

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM