heart disease and the factors

Introduction

this dataset was take from the cdc heart deasis and risk factors which come from doing verious unhealthy activities the data set has measurement index such as smoking age group sex sleephours state and a host of other factors which shall be examined in the visualization as we look at it

Quesion

What is the likely hood of someone getting a heart attack based on their BMI,height,weight,race,sex sleeping hours and mental health days

loadeding libraries

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
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ 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(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(ggfortify)
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(reshape2)
## 
## Attaching package: 'reshape2'
## 
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(shiny)

calling the data set

setwd("C:/Users/eyong/Downloads/heart_2022_no_nans.csv")
gf<- read_csv("heart_2022_no_nans.csv")
## Rows: 246022 Columns: 40
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (34): State, Sex, GeneralHealth, LastCheckupTime, PhysicalActivities, Re...
## dbl  (6): PhysicalHealthDays, MentalHealthDays, SleepHours, HeightInMeters, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

cleaning columnn names

gf<-janitor::clean_names(gf)

Select relevant columns from your dataframe

# Select relevant columns from your dataframe
ha <- gf %>% select(state, sex, race_ethnicity_category, age_category, weight_in_kilograms, bmi, had_heart_attack,sleep_hours,height_in_meters,physical_health_days,mental_health_days)

Are there any significant differences in heart disease prevalence by sex and race

race_p_summary <- ha %>%
  group_by(race_ethnicity_category, had_heart_attack) %>%
  summarise(count = n(), .groups = "drop")

# Create a highcharter stacked bar chart with tooltip showing the count
hchart(race_p_summary, "column", hcaes(x = race_ethnicity_category, y = log(count), group = had_heart_attack)) %>%
  hc_title(text = "Heart Attack by Race/Ethnicity") %>%
  hc_xAxis(title = list(text = "Race/Ethnicity")) %>%
  hc_yAxis(title = list(text = "Log of the Frequency")) %>%
  hc_plotOptions(column = list(stacking = "normal")) %>%
  hc_tooltip(
    pointFormat = "<b>Count:</b> {point.count}<br><b>Race/Ethnicity:</b> {point.race_ethnicity_category}<br><b>Heart Attack:</b> {point.had_heart_attack}"
  ) ## refrence for the  tooltip was chart gbt

selceting values for the data frame

variables <- c("Height", "Sleep Hours", "BMI", "Weight", "Physical Health Days", "Mental Health Days")

make the ui for application

ui <- fluidPage(
  titlePanel("Heart Attack Data Violin Plot"),
  sidebarLayout(
    sidebarPanel(
      selectInput("variable", "Select a variable", choices = variables)
    ),
    mainPanel(
      plotlyOutput("violinPlot")
    )
  )
)

server logic

server <- function(input, output) {
  
  ## Taking variables from the data set  
  selected_variable <- reactive({
    switch(input$variable,
           "Height" = gf$height_in_meters,
           "Sleep Hours" = gf$sleep_hours,
           "BMI" = gf$bmi,
           "Weight" = gf$weight_in_kilograms,
           "Physical Health Days" = gf$physical_health_days,
           "Mental Health Days" = gf$mental_health_days)
  }) ### created by me 
  
  ## Render the interactive violin plot
  output$violinPlot <- renderPlotly({
    
    # Get the selected variable data
    y_data <- selected_variable()
    
    # Generate the violin plot
    plot_ly(data = ha, 
            x = ~factor(had_heart_attack), 
            y = y_data, 
            type = "violin", 
            box = list(visible = TRUE),
            meanline = list(visible = TRUE),
            line = list(color = "black"),
            fillcolor = 'rgba(0,100,80,0.6)',
            opacity = 0.6) %>%
      layout(
        title = paste("Distribution for Individuals with Heart Attack Status -", input$variable),
        xaxis = list(title = "Heart Disease Status"),
        yaxis = list(title = input$variable),
        violingap = 0.5
      )
  })
} ## scored from chartgbt

creating the shiny plot

shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents