Targel 10
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── 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(readxl)
library(janitor)
##
## Attaching package: 'janitor'
##
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
library(ggpubr)
library(gtsummary)
library(table1)
##
## Attaching package: 'table1'
##
## The following objects are masked from 'package:base':
##
## units, units<-
sleep <- read_excel("DATA/sleep_health_and_lifestyle_dataset.xlsx")
mean(sleep$`Duration of sleep`,na.rm = TRUE)
## [1] 7.046757
sleep_range <- range(sleep$`Duration of sleep`,na.rm = TRUE)
duration_70 <- quantile(sleep$`Duration of sleep`, probs = 0.7, na.rm = TRUE)
sleep <- sleep %>%
filter(!is.na(Occupation)) %>%
mutate(occupation_4cat = factor(ifelse(Occupation == "Doctor", "Doctor",
ifelse(Occupation == "Nurse", "Nurse", "Other"))))
sleeptable <- sleep %>%filter(!is.na(`Duration of sleep`)) %>%
mutate(quantile_group = case_when(
`Duration of sleep` <= quantile(`Duration of sleep`, probs = 1/3, na.rm = TRUE) ~ "0.33",
`Duration of sleep` > quantile(`Duration of sleep`, probs = 1/3, na.rm = TRUE) &
`Duration of sleep` <= quantile(`Duration of sleep`, probs = 2/3, na.rm = TRUE) ~ "0.67",
`Duration of sleep` > quantile(`Duration of sleep`, probs = 2/3, na.rm = TRUE) ~ "1"
))
sleeptable3 <- sleeptable %>%
group_by(occupation_4cat, quantile_group) %>%
summarise(
avg_sleep = mean(`Duration of sleep`, na.rm = TRUE)
)
## `summarise()` has grouped output by 'occupation_4cat'. You can override using
## the `.groups` argument.
library(ggplot2)
graph <- ggplot(sleeptable3)+
aes(x = quantile_group, y = avg_sleep,fill = quantile_group,group = quantile_group) +
geom_bar(stat = "identity") +
theme_minimal()+facet_wrap(vars(occupation_4cat))
graph
sleeptable <- sleeptable %>%clean_names()
sleeptable <- sleeptable %>%
separate(blood_pressure, into = c("systolic", "diastolic"), sep = "/",remove = FALSE)
sleeptable<- sleeptable%>% mutate(
systolic = as.numeric(systolic),
diastolic = as.numeric(diastolic)
)
quantile_values <- c(0.33, 0.67, 1)
# Grouped quantile calculations
df_grouped <- sleeptable %>%
group_by(gender) %>%
reframe(
quantile = quantile_values,
systolic = quantile(systolic, probs = quantile_values, na.rm = TRUE),
diastolic = quantile(diastolic, probs = quantile_values, na.rm = TRUE)
) %>%
ungroup()
df_grouped<- df_grouped%>% filter(!is.na(gender))
BP_longer<- df_grouped%>% pivot_longer(
cols = c(systolic,diastolic),
names_to = "BP_type",
values_to = "Quan"
)
BP_longer<- BP_longer%>% filter(!is.na(gender))
BP_longer <- BP_longer %>%
mutate(Quan = as.numeric(Quan))
BP_longer <- BP_longer %>%
mutate(quantile = as.numeric(quantile))
BP_longer%>%
ggplot (aes(x = quantile, y = Quan,fill=gender)) +
geom_col(position ="dodge") +
theme_minimal() +
facet_wrap(~BP_type,scales ="free")+theme_classic()+
labs(title = "BP QUANTILE PER GENDER",
X="QUANTILE",
Y= " BP",
FILL=NULL)
bpwider<- BP_longer%>%
pivot_wider(
names_from = gender,
values_from = Quan) %>%
arrange(BP_type)
```