── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.2 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs) #This is where the dataset is locatedlibrary(patchwork) #This is used to help make the final plot#https://www.rdocumentation.org/packages/patchwork/versions/0.0.1
View raw data
#Use head to view the datahead(mice_weights)
body_weight bone_density percent_fat sex diet gen litter
1 27.60 0.6163850 7.255468 F chow 4 1
2 23.03 0.7693496 4.951037 F chow 4 1
3 28.72 0.6842564 6.020849 F chow 4 1
4 32.57 0.6436947 9.536251 F chow 4 1
5 28.61 0.5297713 6.987331 F chow 4 1
6 28.16 0.5649217 6.767774 F chow 4 1
Filter for both sexes of mice
#Use filter to sort the data by sexfemale_mice <- mice_weights |>filter(sex =="F")male_mice <- mice_weights |>filter(sex =="M")
View the sorted data
#Use head to view the datahead(female_mice)
body_weight bone_density percent_fat sex diet gen litter
1 27.60 0.6163850 7.255468 F chow 4 1
2 23.03 0.7693496 4.951037 F chow 4 1
3 28.72 0.6842564 6.020849 F chow 4 1
4 32.57 0.6436947 9.536251 F chow 4 1
5 28.61 0.5297713 6.987331 F chow 4 1
6 28.16 0.5649217 6.767774 F chow 4 1
head(male_mice)
body_weight bone_density percent_fat sex diet gen litter
1 46.96 0.8422064 9.347382 M chow 4 1
2 31.13 0.6111998 6.975539 M chow 4 1
3 37.23 0.6727096 7.576436 M chow 4 1
4 27.01 0.5779432 5.445223 M chow 4 1
5 36.13 0.7266646 5.942395 M chow 4 1
6 31.08 0.7906264 4.659415 M chow 4 1
Create plots for male and mice, comparing different data columns (Bone density and Percent fat)
#Use ggplot and geom_point to make a scatterplot female_bd <-ggplot(female_mice, aes(x = body_weight, y = bone_density, color = diet)) +scale_color_brewer(palette ="Set1")+#Custom colorgeom_point(alpha =0.5, size =0.5) +#adjusting point sizegeom_smooth(aes(color = diet), method ="lm", se =FALSE, linetype ="dashed")+#Line to show correlationlabs(title ="Female Bodyweight/Bone Density",x ="Body Weight",y ="Bone Density",color ="Diet" )+theme_minimal() #custom themefemale_bd
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 1 row containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 1 row containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_point()`).
Make Two plots based on the generation of mice
gen_bd <-ggplot(mice_weights, aes(x = body_weight, y = bone_density, color = gen)) +scale_color_brewer(palette ="Set1")+geom_point(alpha =0.4, size =0.5) +geom_smooth(aes(color = gen), method ="lm", se =FALSE, linetype ="dashed")+labs(title ="Generational Body Weight/Bone Density",x ="Body Weight",y ="Bone Density",color ="Generation" )+theme_minimal() gen_bd
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 4 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
gen_pf <-ggplot(mice_weights, aes(x = body_weight, y = percent_fat, color = gen)) +scale_color_brewer(palette ="Set1")+geom_point(alpha =0.4, size =0.5) +geom_smooth(aes(color = gen), method ="lm", se =FALSE, linetype ="dashed")+labs(title ="Generational Body Weight/Percent Fat",x ="Body Weight",y ="Percent Fat",color ="Generation" )+theme_minimal() gen_pf
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 4 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Combine all plots into two Multivariable Graphs
#Use patchwork to combine all the plots for easy comparison# + is used to put plots side by side, / is used to put one plot on top of the other, () is used as a couplerfinal_plot_gender <- (male_pf + male_bd) / (female_pf + female_bd)final_plot_gen <- (gen_pf + gen_bd) /plot_spacer()final_plot_gender +plot_annotation( #another function of patchwork, this helps add context to the patchwork combined plottitle ="Correlation of Body Wieght Between Bone Density and Percent Fat Male and Female" )
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 3 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 3 rows containing non-finite outside the scale range (`stat_smooth()`).
Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 1 row containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_point()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 1 row containing non-finite outside the scale range (`stat_smooth()`).
Removed 1 row containing missing values or values outside the scale range
(`geom_point()`).
final_plot_gen +plot_annotation(title ="Correlation of Body Wieght Between Bone Density and Percent Fat Generational" )
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 4 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 4 rows containing non-finite outside the scale range (`stat_smooth()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Conclusion
I used the mice_weights dataset for this visualization, I liked the data since there were a lot of different variables to compare. I used the body weight values as an anchor tying everything together, so that comparisons between different values like bone density, percent fat, sex, and generation could be made. The filter command really helped me in this instance as it allowed me to sort the data based on sex, as well as the patchwork package allowing me to combine plots together. The final plot shows 6 different graphs based on the sex of the mice, their bodily fat percentage and bone density in respect to their body weight, and their generation. The results are interesting for some but not unexpected for others. Mice who weight more, both male and female, have higher fat percentages in their body no matter what they ate. However what intrigued me was that mice who weighed more also had slightly higher bone densities. The correlation of body weight to bone density is pretty weak, but there is still a slight trend. Looking at the color coded points for each mouse based on diet, we can also observe that surprisingly, mice who eat high fat diets mostly don’t weigh much more than those who eat chow diets in both male and female mice, however on average high fat diet mice do weigh more. Also seen in the graphs, mice who eat high fat diets have higher body fat percentages, but not denser bones. On average the chow diet mice actually had higher bone density. These comparisons are gender selective, since as seen in the graphs and many studies, male mice tend to have naturally denser bones, weigh more, and store less body fat than females. This means that a high fat diet will lead to a mouse having higher body fat percentages and slightly weaker bones as compared to a mouse of similar weight and same gender eating chow. In the generational graphs the same patterns emerge for fat percentage, however bone density shows something interesting. As newer generations come, their bone density increases with their weight, at generation 4 the line is much flatter, showing little correlation of body weight to bone density. However each subsequent generation the correlation gets somewhat stronger each time, meaning as newer generations come, they get denser bones. However this is a stretch since looking at the points on the scatterplot doesn’t reveal any obvious patterns in the data.