library(tidyverse)
library(plotly)
library(scales)
library(htmltools)

How much sleep are Australians getting?

sleep_duration <- tibble(
  sleep_group = c(
    "Less than 6 hours",
    "6 to less than 7 hours",
    "7 to less than 8 hours",
    "8 to less than 9 hours",
    "9 hours or more"
  ),
  percentage = c(9.1, 17.7, 35.1, 27.4, 10.7)
)

p1 <- ggplot(
  sleep_duration,
  aes(
    x = reorder(sleep_group, percentage),
    y = percentage,
    text = paste0(
      "Sleep group: ", sleep_group,
      "<br>Percentage: ", percentage, "%"
    )
  )
) +
  geom_col() +
  coord_flip() +
  labs(
    title = "How much sleep are Australians getting?",
    subtitle = "Share of Australian adults by nightly sleep duration",
    x = "Sleep duration",
    y = "Percentage of adults",
    caption = "Source: ABS Measured Physical Activity and Sleep, 2023"
  ) +
  theme_minimal()

ggplotly(p1, tooltip = "text")

How much sleep are Australian children getting?

children_sleep <- tibble(
  group = c(
    "Children aged 5 to 13",
    "Teenagers aged 14 to 17",
    "Children aged 12 to 17 with screens in bedroom",
    "Children aged 12 to 17 without screens in bedroom"
  ),
  sleep_measure = c(
    "Recommended sleep range",
    "Recommended sleep range",
    "Screen in bedroom",
    "No screen in bedroom"
  ),
  sleep_hours = c(9, 8, 7.6, 8.1)
)

p2 <- ggplot(
  children_sleep,
  aes(
    x = reorder(group, sleep_hours),
    y = sleep_hours,
    fill = sleep_measure,
    text = paste0(
      "Group: ", group,
      "<br>Measure: ", sleep_measure,
      "<br>Sleep: ", sleep_hours, " hours per night"
    )
  )
) +
  geom_col() +
  coord_flip() +
  labs(
    title = "How much sleep are Australian children getting?",
    subtitle = "Recommended sleep compared with screen related sleep differences",
    x = "Group",
    y = "Sleep hours per night",
    fill = "Measure",
    caption = "Source: ABS Measured Physical Activity and Sleep, 2023"
  ) +
  theme_minimal()

ggplotly(p2, tooltip = "text")

The teenage sleep gap

teen_sleep <- tibble(
  category = c(
    "Teen average sleep lower estimate",
    "Teen average sleep upper estimate",
    "Recommended minimum",
    "Recommended maximum"
  ),
  type = c("Actual sleep", "Actual sleep", "Recommended sleep", "Recommended sleep"),
  hours = c(6.5, 7.5, 8, 10)
)

p4 <- ggplot(
  teen_sleep,
  aes(
    x = category,
    y = hours,
    fill = type,
    text = paste0(
      "Category: ", category,
      "<br>Type: ", type,
      "<br>Hours: ", hours
    )
  )
) +
  geom_col() +
  labs(
    title = "The teenage sleep gap",
    subtitle = "Australian teenagers are sleeping below the recommended range",
    x = "Sleep measure",
    y = "Hours per night",
    fill = "Type",
    caption = "Source: Sleep Health Foundation and VicHealth"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 25, hjust = 1))

ggplotly(p4, tooltip = "text")

Sleep problems start early

sleep_problems <- tibble(
  group = c("Adults", "12 to 13 year olds", "16 to 17 year olds"),
  sleep_issue = c(
    "At least two sleep related problems",
    "Do not meet sleep guidelines on school nights",
    "Do not meet sleep guidelines on school nights"
  ),
  percentage = c(48, 27, 52)
)

p3 <- ggplot(
  sleep_problems,
  aes(
    x = reorder(group, percentage),
    y = percentage,
    text = paste0(
      "Group: ", group,
      "<br>Issue: ", sleep_issue,
      "<br>Percentage: ", percentage, "%"
    )
  )
) +
  geom_col() +
  coord_flip() +
  labs(
    title = "Sleep problems start early",
    subtitle = "Sleep issues among adults and young people in Australia",
    x = "Group",
    y = "Percentage",
    caption = "Source: AIHW Sleep problems as a risk factor for chronic conditions"
  ) +
  theme_minimal()

ggplotly(p3, tooltip = "text")

The cost of poor sleep

sleep_cost <- tibble(
  cost_category = c(
    "Health system costs",
    "Productivity losses",
    "Informal care costs",
    "Other financial costs",
    "Loss of wellbeing"
  ),
  cost_type = c(
    "Financial",
    "Financial",
    "Financial",
    "Financial",
    "Wellbeing"
  ),
  cost_billion_aud = c(1.8, 17.9, 0.6, 5.9, 40.1)
)

p5 <- ggplot(
  sleep_cost,
  aes(
    x = reorder(cost_category, cost_billion_aud),
    y = cost_billion_aud,
    fill = cost_type,
    text = paste0(
      "Cost category: ", cost_category,
      "<br>Cost type: ", cost_type,
      "<br>Cost: $", cost_billion_aud, " billion"
    )
  )
) +
  geom_col() +
  coord_flip() +
  labs(
    title = "The $66.3 billion cost of poor sleep",
    subtitle = "Estimated cost of inadequate sleep in Australia, 2016 to 2017",
    x = "Cost category",
    y = "Cost in billion AUD",
    fill = "Cost type",
    caption = "Source: Sleep Health Foundation and Deloitte Access Economics"
  ) +
  theme_minimal()

ggplotly(p5, tooltip = "text")