Intro

Step 1: Define Your Story Core Question: How does screen time influence mental health and productivity?

Subtopics:

The distribution of screen time across demographics (Who is most affected?). The relationship between screen time and mental health indicators (Is there a tipping point?). The impact of screen time on productivity and daily routines (Does type or timing matter?).

Setup

1

library(shiny)
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
# Create the dataset
screen_time_data <- data.frame(
  Category = c("12-14", "15-17", "Boys", "Girls", "Asian", "Black", "White", "Hispanic", "Total"),
  Group = c("Age Group", "Age Group", "Sex", "Sex", "Race", "Race", "Race", "Race", "Total"),
  Percent = c(45.6, 55.0, 48.3, 52.5, 43.5, 60.4, 47.9, 52.8, 50.4)
)

# Define UI
ui <- fluidPage(
  titlePanel("Grouped Bar Plot for Screen Time by Demographics"),
  sidebarLayout(
    sidebarPanel(
      selectInput("group_filter", "Select Category:", 
                  choices = unique(screen_time_data$Group[screen_time_data$Group != "Total"]),
                  selected = "Age Group")
    ),
    mainPanel(
      plotOutput("groupedPlot")
    )
  )
)

# Define Server
server <- function(input, output, session) {
  # Generate the grouped bar plot
  output$groupedPlot <- renderPlot({
    # Filter data to include the selected group and the total percentage
    filtered_data <- screen_time_data %>%
      filter(Group == input$group_filter | Group == "Total")
    
    # Plot grouped bar chart
    ggplot(filtered_data, aes(x = Group, y = Percent, fill = Category)) +
      geom_bar(stat = "identity", position = position_dodge2(preserve = "single"), width = 0.7) +
      labs(title = paste("Screen Time by", input$group_filter, "and Total"),
           x = "Category", y = "Percentage") +
      scale_fill_brewer(palette = "Set3") +
      theme_minimal() +
      theme(legend.title = element_blank()) +
      guides(fill = guide_legend(title = NULL))
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents
library(shiny)
library(ggplot2)
library(dplyr)

# Data for Figure 1: Distribution of daily screen time
figure1_data <- data.frame(
  Daily_Screen_Time = c("Less than 1 hour", "1 hour", "2 hours", "3 hours", "4 hours or more"),
  Percent = c(3.0, 6.1, 17.8, 22.8, 50.4)
)

# Data for Figure 2: Screen time by demographics
figure2_data <- data.frame(
  Category = c("12-14", "15-17", "Boys", "Girls", "Asian", "Black", "White", "Hispanic", "Total"),
  Group = c("Age Group", "Age Group", "Sex", "Sex", "Race", "Race", "Race", "Race", "Total"),
  Percent = c(45.6, 55.0, 48.3, 52.5, 43.5, 60.4, 47.9, 52.8, 50.4)
)

# Data for Figure 3: Screen time by family income, parental education, and urbanization
figure3_data <- data.frame(
  Characteristic = c("Family Income < 200%", "Family Income >= 200%", 
                     "Some College or Less (Parents)", "College Degree or Higher (Parents)",
                     "Metropolitan", "Nonmetropolitan"),
  Percent = c(51.7, 49.6, 55.0, 45.2, 51.4, 43.3)
)

# Define UI
ui <- fluidPage(
  titlePanel("Visualizations for Screen Time Study"),
  sidebarLayout(
    sidebarPanel(
      selectInput("figure_select", "Select Figure:", 
                  choices = c("Figure 1: Daily Screen Time Distribution", 
                              "Figure 2: Screen Time by Demographics",
                              "Figure 3: Screen Time by Socioeconomic Factors"),
                  selected = "Figure 1: Daily Screen Time Distribution"),
      uiOutput("figure2_filters") # Dynamically show filters for Figure 2
    ),
    mainPanel(
      plotOutput("dynamicPlot")
    )
  )
)

# Define Server
server <- function(input, output, session) {
  # Conditional UI for Figure 2
  output$figure2_filters <- renderUI({
    if (input$figure_select == "Figure 2: Screen Time by Demographics") {
      selectInput("figure2_filter_group", "Select Category:", 
                  choices = unique(figure2_data$Group[figure2_data$Group != "Total"]),
                  selected = "Age Group")
    }
  })
  
  # Render the plot based on the selected figure
  output$dynamicPlot <- renderPlot({
    if (input$figure_select == "Figure 1: Daily Screen Time Distribution") {
      ggplot(figure1_data, aes(x = Daily_Screen_Time, y = Percent, fill = Daily_Screen_Time)) +
        geom_bar(stat = "identity") +
        labs(title = "Daily Screen Time Distribution",
             x = "Daily Screen Time", y = "Percentage") +
        theme_minimal() +
        scale_fill_brewer(palette = "Blues")
    } else if (input$figure_select == "Figure 2: Screen Time by Demographics") {
      # Dynamically filter the data for Figure 2
      req(input$figure2_filter_group)
      filtered_data <- figure2_data %>%
        filter(Group == input$figure2_filter_group | Group == "Total")
      
      ggplot(filtered_data, aes(x = Group, y = Percent, fill = Category)) +
        geom_bar(stat = "identity", position = position_dodge2(preserve = "single"), width = 0.7) +
        labs(title = paste("Screen Time by", input$figure2_filter_group, "and Total"),
             x = "Category", y = "Percentage") +
        scale_fill_brewer(palette = "Set3") +
        theme_minimal() +
        theme(legend.title = element_blank())
    } else if (input$figure_select == "Figure 3: Screen Time by Socioeconomic Factors") {
      ggplot(figure3_data, aes(x = Characteristic, y = Percent, fill = Characteristic)) +
        geom_bar(stat = "identity", width = 0.7) +
        coord_flip() +
        labs(title = "Screen Time by Socioeconomic Factors",
             x = "Characteristic", y = "Percentage") +
        scale_fill_brewer(palette = "Pastel1") +
        theme_minimal() +
        theme(legend.position = "none")
    }
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents
screen_time_distribution <- data.frame(
  "Daily_Screen_Time" = c("Less than 1 hour", "1 hour", "2 hours", "3 hours", "4 hours or more"),
  "Percent" = c(3.0, 6.1, 17.8, 22.8, 50.4),
  "Confidence_Interval_Lower" = c(2.0, 4.8, 15.8, 20.6, 47.6),
  "Confidence_Interval_Upper" = c(4.2, 7.6, 19.9, 25.1, 53.2),
  "Standard_Error" = c(0.54, 0.71, 1.04, 1.13, 1.40)
)
library(ggplot2)

# Create the dataset
screen_time_distribution <- data.frame(
  Daily_Screen_Time = c("Less than 1 hour", "1 hour", "2 hours", "3 hours", "4 hours or more"),
  Percent = c(3.0, 6.1, 17.8, 22.8, 50.4)
)

# Plot
ggplot(screen_time_distribution, aes(x = Daily_Screen_Time, y = Percent)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "Distribution of Daily Screen Time", x = "Daily Screen Time", y = "Percentage") +
  theme_minimal()

# Create the dataset
demographics_screen_time <- data.frame(
  Characteristic = c("Total", "12–14 years", "15–17 years", "Boys", "Girls",
                     "Asian, non-Hispanic", "Black, non-Hispanic", "White, non-Hispanic", "Hispanic"),
  Percent = c(50.4, 45.6, 55.0, 48.3, 52.5, 43.5, 60.4, 47.9, 52.8)
)

# Plot
ggplot(demographics_screen_time, aes(x = Characteristic, y = Percent, fill = Characteristic)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  labs(title = "Percentage of Teenagers with 4+ Hours of Screen Time by Demographics",
       x = "Demographics", y = "Percentage") +
  theme_minimal()

# Create the dataset
socioeconomic_screen_time <- data.frame(
  Characteristic = c("Family income < 200%", "Family income >= 200%", 
                     "Some college or less (Parents)", "College degree or higher (Parents)",
                     "Metropolitan", "Nonmetropolitan"),
  Percent = c(51.7, 49.6, 55.0, 45.2, 51.4, 43.3)
)

# Plot
ggplot(socioeconomic_screen_time, aes(x = Characteristic, y = Percent, fill = Characteristic)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  labs(title = "Socioeconomic Factors and Screen Time", x = "Characteristic", y = "Percentage") +
  theme_minimal()

# Create the dataset
mental_health_screen_time <- data.frame(
  Outcome = c("Anxiety symptoms (4+ hours)", "Anxiety symptoms (< 4 hours)", 
              "Depression symptoms (4+ hours)", "Depression symptoms (< 4 hours)"),
  Percent = c(27.1, 12.3, 25.9, 9.5)
)

# Plot
ggplot(mental_health_screen_time, aes(x = Outcome, y = Percent, fill = Outcome)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  labs(title = "Anxiety and Depression by Screen Time Category", x = "Outcome", y = "Percentage") +
  theme_minimal()

library(ggplot2)

# Create the dataset
screen_time_distribution <- data.frame(
  Daily_Screen_Time = c("Less than 1 hour", "1 hour", "2 hours", "3 hours", "4 hours or more"),
  Percent = c(3.0, 6.1, 17.8, 22.8, 50.4)
)

# Plot
ggplot(screen_time_distribution, aes(x = Daily_Screen_Time, y = Percent, fill = Daily_Screen_Time)) +
  geom_bar(stat = "identity") +
  labs(title = "Distribution of Daily Screen Time", x = "Daily Screen Time", y = "Percentage") +
  theme_minimal() +
  scale_fill_brewer(palette = "Blues")

# Create the dataset
demographics_screen_time <- data.frame(
  Demographic = c("12–14 years", "15–17 years", "Boys", "Girls", 
                  "Asian, non-Hispanic", "Black, non-Hispanic", 
                  "White, non-Hispanic", "Hispanic"),
  Percent = c(45.6, 55.0, 48.3, 52.5, 43.5, 60.4, 47.9, 52.8)
)

# Plot
ggplot(demographics_screen_time, aes(x = Demographic, y = Percent, fill = Demographic)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  labs(title = "Screen Time by Demographics", x = "Demographic", y = "Percentage") +
  theme_minimal()

# Create the dataset
socioeconomic_screen_time <- data.frame(
  Category = c("Family Income < 200%", "Family Income >= 200%", 
               "Some College or Less", "College Degree or Higher", 
               "Metropolitan", "Nonmetropolitan"),
  Percent = c(51.7, 49.6, 55.0, 45.2, 51.4, 43.3)
)

# Plot
ggplot(socioeconomic_screen_time, aes(x = Category, y = Percent, fill = Category)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  labs(title = "Screen Time and Socioeconomic Factors", x = "Category", y = "Percentage") +
  theme_minimal() +
  scale_fill_brewer(palette = "Set2")

# Create the dataset
anxiety_data <- data.frame(
  Screen_Time = c("4+ Hours", "< 4 Hours"),
  Anxiety_Symptoms = c(27.1, 12.3)
)

# Plot
ggplot(anxiety_data, aes(x = Screen_Time, y = Anxiety_Symptoms, fill = Screen_Time)) +
  geom_bar(stat = "identity") +
  labs(title = "Anxiety Symptoms by Screen Time", x = "Daily Screen Time", y = "Percentage") +
  theme_minimal() +
  scale_fill_manual(values = c("skyblue", "lightcoral"))

# Create the dataset
depression_data <- data.frame(
  Screen_Time = c("4+ Hours", "< 4 Hours"),
  Depression_Symptoms = c(25.9, 9.5)
)

# Plot
ggplot(depression_data, aes(x = Screen_Time, y = Depression_Symptoms, fill = Screen_Time)) +
  geom_bar(stat = "identity") +
  labs(title = "Depression Symptoms by Screen Time", x = "Daily Screen Time", y = "Percentage") +
  theme_minimal() +
  scale_fill_manual(values = c("lightblue", "lightpink"))

# Create the dataset
combined_data <- data.frame(
  Condition = rep(c("Anxiety Symptoms", "Depression Symptoms"), each = 2),
  Screen_Time = c("4+ Hours", "< 4 Hours", "4+ Hours", "< 4 Hours"),
  Percent = c(27.1, 12.3, 25.9, 9.5)
)

# Plot
ggplot(combined_data, aes(x = Condition, y = Percent, fill = Screen_Time)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Combined Anxiety and Depression by Screen Time", x = "Condition", y = "Percentage") +
  theme_minimal() +
  scale_fill_brewer(palette = "Pastel1")