── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── 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)
Warning: package 'dslabs' was built under R version 4.6.1
Check Dataset being analyzed: mice_weights for initial info
head(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
dim(mice_weights)
[1] 780 7
summary(mice_weights)
body_weight bone_density percent_fat sex diet gen
Min. :18.13 Min. :0.2708 Min. : 2.552 F:398 chow:394 4 : 97
1st Qu.:28.09 1st Qu.:0.4888 1st Qu.: 5.566 M:382 hf :386 7 :195
Median :32.98 Median :0.5643 Median : 8.276 8 :193
Mean :34.08 Mean :0.5697 Mean : 8.594 9 : 97
3rd Qu.:39.37 3rd Qu.:0.6373 3rd Qu.:10.926 11:198
Max. :65.15 Max. :0.9980 Max. :22.154
NAs :4 NAs :4
litter
1:442
2:338
Clean Dataset Selects the relevant variables and removes any is.na() rows for the four variables being potentially analyzed: body_weight in grams, bone_density, percent_fat, and sex. Stores cleaned dataset in mice_weights_cleaned.
ggplot(mice_weights_cleaned, aes(x = body_weight, y = bone_density)) +geom_point( size =1.5,alpha =0.7, aes(color = sex)) +theme_bw() +labs(x="Body Weight in Grams", y ="Bone Density", title ="Body Weight Plotted Against Bone Density Scatter Plot" , caption ="From the dslabs dataset, mice_weights", color ="Sex") +scale_color_manual(values =c("F"="gold","M"="purple"))
Final R code for the scatter plot includes ggplot() to define the dataset to draw the defined x and y variables from, geom_point() to plot the graph as a scatter plot with size, alpha and color determining the dots’ size, transparency and color based on sex, theme_bw() to display the plot in a bw theme, labs() to label the x and y axis and to give the plot both a title and caption and finally scale_color_manual() to change the default colors of the legend, such that each specific recorded outcome of sex has its own non-default unique color.
From the graph, it is easily seen that female mice appear to cluster towards lower body weight and bone density on average compared to where male mice cluster towards.