FDA_AWAKENING_COUNTS

library(ggplot2)
library(dplyr) 

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
awakening_data <- read.csv("FDA_Awakening_Counts - FDA_Awakening_Counts.csv") #import cleaned data from sheets (filtered for valid days)
awakening_data
   Subject AwakeningCount Nights
1        2             10      1
2        2             16      2
3        2             15      3
4        2             24      4
5        2             17      5
6        2             23      6
7        2             17      7
8        2             16      8
9        2             15      9
10       2              9     10
11       2             18     11
12       2             31     12
13       2             26     13
14       2             16     14
15       4              7      1
16       4              7      2
17       4              6      3
18       4              2      4
19       4              8      5
20       4              7      6
21       4              2      7
22       4             13      8
23       4              7      9
24       4             13     10
25       4              4     11
26       4              4     12
27       4              7     13
28       4              9     14
awakening_data <- awakening_data %>% #check that column names align
  mutate(
    Subject = as.factor(Subject),
    Nights = as.numeric(Nights),
    AwakeningCount = as.numeric(AwakeningCount)
  )
#create data plot for awakening counts between two subjects
ggplot(awakening_data, aes(x= Nights, y= AwakeningCount, color= Subject))+ 
  geom_point()+
  geom_smooth(method = "lm", se = TRUE, linewidth = 1, linetype = "solid", fill= "grey")+ # set SE as TRUE to have standard error shadow on graph
  facet_wrap(~Subject)+ #group graphs by subject
  scale_x_continuous(
    limits = c(1, max(awakening_data$Night)),
    breaks = seq(1, max(awakening_data$Night), 1)
  ) +
  scale_color_brewer(palette = "Dark2")+ #add colors and axis names
  labs(
    title= "Awakening Count Trends Over 2 Weeks By Subject",
    x= "Nights",
    y= "Awakening Count",
    color= "Subject"
  )+
  theme_minimal() +
theme(
    axis.line = element_line(color = "grey40", linewidth = 0.8),  # bold axis lines
    axis.ticks = element_line(color = "grey40", linewidth = 0.8),   # bold tick marks
    theme(panel.grid.major.x = element_blank()),
    strip.background = element_blank(),
    panel.spacing = unit(1, "lines"), 
    panel.border = element_rect(color = "grey40", fill = NA),
    text = element_text(size = 15),
    plot.title = element_text(hjust = 0.5, face= "bold"),
    legend.position = "right"
  ) #orient the graph and clean up the look for the axes, legend, and colors
`geom_smooth()` using formula = 'y ~ x'