#Loading Libraries
library(dslabs)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.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(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
##
## Attaching package: 'highcharter'
##
## The following object is masked from 'package:dslabs':
##
## stars
library(RColorBrewer)
#Loading Dataset
data(mice_weights)
## Warning in data(mice_weights): data set 'mice_weights' not found
#Renaming Dataset
data <- mice_weights
#Plotting Bone Density and Body Weight
#Plotting
plot1 <- ggplot(data, aes(x = bone_density, y = body_weight, color = diet)) +
geom_point(alpha = 0.4) +
scale_color_brewer(palette = "Set1", name = "Diet", labels=c('Chow', 'High Fat')) + #Renaming labels and adding color
geom_smooth(method = "loess", span = 0.15) +
labs(
x = "Bone Density",
y = "Body Weight in Grams",
title = "Comparison of Body Weight and Bone Density based on Diet",
caption = "Mice weights based on diets collected by Karen Svenson from Jackson Laboratories."
) + theme_classic() #Changing theme
plot1
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 4 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 4 rows containing missing values (`geom_point()`).
ggplotly(plot1)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 4 rows containing non-finite values (`stat_smooth()`).
I used the mice weights data set. This dataset shows the results of body weight, bone density, and percent fat for mice on a chow diet or a high fat diet. I created my scatterplot using ggplot. I used bone density for the x-axis and body weight for the y-axis. I thought it would be interesting to color by diet to see which diet produced the biggest mice. According to the graph, mice with high fat diets were the biggest. I put x and y axis labels and modified the labels for the Diet legend to make it more readable. Lastly, I used plotly to create interactivity. I tried to use highcharter but it looked wild because I could not properly arrange the data.