knitr::opts_chunk$set(echo = TRUE, message = FALSE)
options(repos = "https://cloud.r-project.org")
#install.packages("plotly")
#install.packages("ggplot2")
library(plotly)
library(ggplot2)  # Load ggplot2 package
heart_Data <- read.csv("D://heart.csv")
str(heart_Data)
## 'data.frame':    1025 obs. of  14 variables:
##  $ age     : int  52 53 70 61 62 58 58 55 46 54 ...
##  $ sex     : int  1 1 1 1 0 0 1 1 1 1 ...
##  $ cp      : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ trestbps: int  125 140 145 148 138 100 114 160 120 122 ...
##  $ chol    : int  212 203 174 203 294 248 318 289 249 286 ...
##  $ fbs     : int  0 1 0 0 1 0 0 0 0 0 ...
##  $ restecg : int  1 0 1 1 1 0 2 0 0 0 ...
##  $ thalach : int  168 155 125 161 106 122 140 145 144 116 ...
##  $ exang   : int  0 1 1 0 0 0 0 1 0 1 ...
##  $ oldpeak : num  1 3.1 2.6 0 1.9 1 4.4 0.8 0.8 3.2 ...
##  $ slope   : int  2 0 0 2 1 1 0 1 2 1 ...
##  $ ca      : int  2 0 0 1 3 0 3 1 0 2 ...
##  $ thal    : int  3 3 3 3 2 2 1 3 3 2 ...
##  $ target  : int  0 0 0 0 0 1 0 0 0 0 ...

Base R graph

For this graph, the investigation focuses on the relationship between age and resting blood pressure. The scatterplot helps in visualizing if there’s any correlation or pattern between age and blood pressure. This understanding can be crucial in assessing the potential impact of age on blood pressure levels in the dataset.

# Create a scatterplot using base R
plot(heart_Data$age, heart_Data$trestbps, xlab = "Age", ylab = "Resting Blood Pressure", main = "Scatterplot of Age vs Resting Blood Pressure")

The base R scatterplot shows a varied pattern in resting blood pressure across different age groups, indicating potential variability in blood pressure trends with age.

Straight lines observed more and for example age - 50 observed different Blood Pressures. like that all over the age straight lines are indicating there is not straight correlation of BP with age

ggplot graph

ggplot Graph - Boxplot: Cholesterol (chol) by Chest Pain Type (cp) and Heart Disease Status (target)

For this graph, investigating the distribution of cholesterol levels based on different chest pain types and heart disease status. The boxplot visualization helps in comparing the median and spread of cholesterol levels across different chest pain types, while also considering heart disease status as a factor. This understanding can provide insights into potential relationships between chest pain, cholesterol levels, and heart disease.

# Create a modified ggplot boxplot
ggplot(heart_Data, aes(x = as.factor(cp), y = chol, fill = as.factor(target))) +
  geom_boxplot() +
  labs(x = "Chest Pain Type", y = "Cholesterol", fill = "Heart Disease", title = "Boxplot of Cholesterol by Chest Pain Type and Heart Disease Status")

ggplot boxplot includes the variables chest pain type (cp), cholesterol levels (chol), and heart disease status (target). It helps in visualizing the distribution of cholesterol levels among different chest pain types, differentiated by heart disease status.

Identified very few outliers less than 4 for cholesterol in each chest pain type.

ggplot Graph - Scatterplot: Age vs Resting Blood Pressure (trestbps) by Gender (sex) with Cholesterol (chol) as Bubble Size

For this graph, investigating the relationship between age, resting blood pressure, and gender, with cholesterol levels represented by bubble size. The scatterplot with bubble size encoding helps in visualizing the distribution of cholesterol levels along with age and blood pressure, while also considering gender as a factor. This visualization aids in understanding potential associations between age, gender, blood pressure, and cholesterol levels in the dataset.

# Create the ggplot scatterplot with bubble size encoding cholesterol
ggplot(heart_Data, aes(x = age, y = trestbps, color = as.factor(sex), size = chol)) +
  geom_point(alpha = 0.7) +
  scale_size_continuous(range = c(1, 10)) +
  labs(x = "Age", y = "Resting Blood Pressure", color = "Gender", size = "Cholesterol") +
  ggtitle("Scatterplot of Age vs Resting Blood Pressure by Gender with Cholesterol as Bubble Size")

In this ggplot scatterplot, age and resting blood pressure are represented on the x and y axes, respectively. Gender is encoded by color, and cholesterol levels are represented by bubble size. This visualization provides insights into the relationships between age, blood pressure, gender, and cholesterol levels simultaneously.

The above plot defines high cholesterol is observed on medium blood pressure but with high age and gender = 0

plotly graph

Plotly Graph - Scatterplot: Maximum Heart Rate (thalach) vs Oldpeak

For this graph, investigating the relationship between maximum heart rate, oldpeak (ST depression induced by exercise relative to rest), and heart disease status. The scatterplot with color encoding for heart disease status helps in visualizing any patterns or trends between oldpeak, maximum heart rate, and heart disease status. This understanding can be valuable in analyzing how the oldpeak and maximum heart rate metrics relate to the presence or absence of heart disease in the dataset.

# Create a plotly scatterplot
plot_ly(heart_Data, x = ~oldpeak, y = ~thalach, color = ~as.factor(target), type = "scatter", mode = "markers") %>%
  layout(title = "Scatterplot of Max Heart Rate vs Oldpeak by Heart Disease Status",
         xaxis = list(title = "Oldpeak"),
         yaxis = list(title = "Max Heart Rate"),
         showlegend = TRUE)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

The plotly scatterplot shows the relationship between maximum heart rate and oldpeak, differentiated by heart disease status. It helps in understanding any potential associations between these variables and heart disease.

For gender = 1 Least Old peak is observed and for gender 0 have high old peak compared to gender 1

Plotly Graph - Scatterplot: Age vs Resting Blood Pressure (trestbps) by Gender (sex) with Cholesterol (chol) as Bubble Size

For this graph, investigating the relationship between age, maximum heart rate, and chest pain type. The scatterplot with color encoding for chest pain type helps in visualizing if there’s any correlation or pattern between age, maximum heart rate, and the type of chest pain experienced by individuals. This understanding can be valuable in analyzing how age and chest pain type relate to maximum heart rate levels in the dataset, providing insights into potential risk factors for heart-related conditions.

# Create a plotly scatterplot
plot_ly(heart_Data, x = ~age, y = ~thalach, color = ~as.factor(cp), type = "scatter", mode = "markers") %>%
  layout(title = "Scatterplot of Age vs Max Heart Rate by Chest Pain Type",
         xaxis = list(title = "Age"),
         yaxis = list(title = "Max Heart Rate"),
         showlegend = TRUE)

In this plotly scatterplot, age and resting blood pressure are represented on the x and y axes, respectively. Gender is encoded by color, and cholesterol levels are represented by bubble size. This visualization provides insights into the relationships between age, blood pressure, gender, and cholesterol levels simultaneously.

There is no unique patterns identified

Plotly Graph - Bubble Chart: Age vs Resting Blood Pressure (trestbps) by Gender (sex)

# Create a plotly bubble chart
plot_ly(heart_Data, x = ~age, y = ~trestbps, size = ~chol, color = ~as.factor(sex), type = "scatter", mode = "markers") %>%
  layout(title = "Bubble Chart of Age vs Resting Blood Pressure by Gender",
         xaxis = list(title = "Age"),
         yaxis = list(title = "Resting Blood Pressure"),
         showlegend = TRUE)
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

The plotly bubble chart visualizes age, resting blood pressure, and cholesterol levels by gender. The size of the bubbles represents cholesterol levels, providing a multi-dimensional view of these variables’ relationships with age and gender.

Straight lines observed more and for example age - 50 observed different Blood Pressures. like that all over the age straight lines are indicating there is not straight correlation of BP with age and gender 0 has less blood pressure