0% found this document useful (0 votes)
13 views16 pages

Patient Socio-Economic and Marital Status Analysis

Uploaded by

katy cyrus
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

Patient Socio-Economic and Marital Status Analysis

Uploaded by

katy cyrus
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

R scripts

1-statut socio eco

# Load required libraries

library(ggplot2)

library(readxl)

library(dplyr)

library(RColorBrewer)

# Load the data from the Excel file

data <- read_excel("C:/Users/bnabi/OneDrive/Documents/nez/[Link]")

# Get unique patients only (to avoid counting patients multiple times)

patient_data <- data %>%

group_by(code_patient) %>%

slice(1) %>% # Take first occurrence of each patient

ungroup()

# Count occurrences of each socio-economic status category

socio_economic_counts <- patient_data %>%

group_by(statut_socio_economique) %>%

summarise(count = n()) %>%

filter(![Link](statut_socio_economique)) %>%

mutate(percentage = (count / sum(count)) * 100,

label = paste0(count, "\n(", round(percentage, 1), "%)")) # Combine count and percentage into
label
# Define a better color palette using RColorBrewer (qualitative palette)

socio_economic_colors <- [Link](3, "Set2") # Using the 'Set2' palette for 3 categories: bas,
moyen, haut

# Create the enhanced pie chart

ggplot(socio_economic_counts, aes(x = "", y = percentage, fill = statut_socio_economique)) +

geom_bar(stat = "identity", width = 1, color = "white", size = 1.5) + # Increase slice border thickness

coord_polar(theta = "y") + # Convert bar chart to pie chart

geom_text(aes(y = cumsum(percentage) - 0.5 * percentage, label = label),

color = "black", fontface = "bold", size = 5,

hjust = 0.5, vjust = 0.5) + # Center the text inside pie slices

scale_fill_manual(values = socio_economic_colors, name = "Statut Socio-Économique") +

labs(title = "Répartition du Statut Socio-Économique des Patients",

subtitle = paste0("Distribution des patients par statut socio-économique pour ",


sum(socio_economic_counts$count), " patients uniques"),

caption = "Source: Données extraites de l'étude\nNote: Chaque patient est compté une seule
fois") +

theme_void() + # Removes axis for a clean pie chart

theme(

[Link] = "right", # Place legend on the right

[Link] = element_text(face = "bold", size = 14), # Increased legend title size

[Link] = element_text(size = 12), # Increased legend text size

[Link] = element_text(face = "bold", size = 18, hjust = 0.5, margin = margin(b = 10)),

[Link] = element_text(size = 14, hjust = 0.5, margin = margin(b = 20)),

[Link] = element_text(size = 10, color = "gray40", hjust = 0, margin = margin(t = 20)),

[Link] = unit(1, "cm"), # Increase the size of the legend keys

[Link] = margin(20, 20, 20, 20), # Add margin around the plot for clarity

[Link] = element_rect(fill = "white", color = "white"), # Clean background

[Link] = element_rect(color = "gray80", fill = NA) # Subtle border around the plot
)+

# Add a smooth edge to the pie slices

theme([Link] = element_rect(fill = "white"))

# Save the high-resolution plot

ggsave("repartition_statut_socio_economique_with_percentages_enhanced.png", width = 9, height


= 7, dpi = 300)

2- matrimoniale

# Load required libraries

library(ggplot2)

library(readxl)

library(dplyr)

# Load the data from the Excel file

data <- read_excel("C:/Users/bnabi/OneDrive/Documents/nez/[Link]")

# Get unique patients only (to avoid counting patients multiple times)

patient_data <- data %>%

group_by(code_patient) %>%

slice(1) %>% # Take first occurrence of each patient

ungroup()

# Count occurrences of each marital status category

marital_status_counts <- patient_data %>%

group_by(situation_matrimoniale) %>%

summarise(count = n()) %>%

filter(![Link](situation_matrimoniale)) %>%
mutate(percentage = (count / sum(count)) * 100,

label_position = cumsum(percentage) - 0.5 * percentage, # Calculate label position

label = paste0(count, "\n(", round(percentage, 1), "%)"),

# Define colors for marital status categories

color_order = factor(situation_matrimoniale, levels = c("marié", "célibataire", "divorcé")))

# Calculate total patients

total_patients <- sum(marital_status_counts$count)

# Define custom colors for the marital status categories

marital_colors <- c("#66B3FF", "#FF9999", "#99FF99")

# Create the pie chart for marital status

ggplot(marital_status_counts, aes(x = "", y = percentage, fill = color_order)) +

geom_bar(stat = "identity", width = 1, color = "white", size = 1) +

coord_polar(theta = "y") + # Convert bar chart to pie chart

geom_text(aes(y = label_position, label = label),

color = "black", fontface = "bold", size = 4,

hjust = 0.5, vjust = 0.5) + # Center the text

scale_fill_manual(values = marital_colors, name = "Situation Matrimoniale") +

labs(title = "Répartition de la Situation Matrimoniale des Patients",

subtitle = paste0("Distribution des patients par situation matrimoniale pour ", total_patients, "
patients uniques"),

caption = "Source: Données extraites de l'étude\nNote: Chaque patient est compté une seule
fois") +

theme_void() + # Removes axis for a clean pie chart

theme(

[Link] = "right",
[Link] = element_text(face = "bold", size = 12),

[Link] = element_text(size = 10),

[Link] = element_text(face = "bold", size = 16, hjust = 0.5, margin = margin(b = 10)),

[Link] = element_text(size = 12, hjust = 0.5, margin = margin(b = 20)),

[Link] = element_text(size = 9, color = "gray40", hjust = 0, margin = margin(t = 20)),

[Link] = unit(0.8, "cm"),

[Link] = margin(20, 20, 20, 20),

[Link] = element_rect(color = "gray80", fill = NA)

# Save the high-resolution plot

ggsave("repartition_situation_matrimoniale.png", width = 9, height = 7, dpi = 300)

3- sexe

a-

# Load necessary libraries

library(dplyr)

library(readxl)

# Load your data from '[Link]' (replace with the actual file path)

data <- read_excel("C:/Users/bnabi/OneDrive/Documents/nez/[Link]") # Update the path


accordingly

# Check the structure of your data to ensure it's loaded correctly

str(data)

# Remove duplicate patients based on 'code_patient', keeping their sex information

unique_patients <- data %>%

distinct(code_patient, sexe) # Ensure the column names match your data


# Count the occurrences of each gender ('homme' and 'femme')

sexe_count <- table(unique_patients$sexe)

# Create the pie chart

pie(sexe_count,

labels = paste(names(sexe_count), ":", sexe_count),

col = c("lightblue", "lightpink"),

main = "Répartition des sexes par code patient",

clockwise = TRUE)

# Create a donut chart using pie chart, with a hole in the middle

pie(sexe_count,

labels = paste(names(sexe_count), ":", sexe_count),

col = c("lightblue", "lightpink"),

main = "Répartition des sexes par code patient",

radius = 0.7,

clockwise = TRUE)

b-

# Horizontal bar chart

barplot(sexe_count,

[Link] = names(sexe_count),

col = c("lightblue", "lightpink"),

main = "Répartition des sexes par code patient",

horiz = TRUE,

xlab = "Nombre de patients",

ylab = "Sexe")
# Example: Assuming you have an 'age' column in your dataset

# Replace this with the actual numerical column you want to analyze

age_data <- data$age # Make sure to replace 'age' with your actual column name

# Calculate the standard deviation

sd_value <- sd(age_data, [Link] = TRUE)

# Display the standard deviation

print(paste("The standard deviation of age is:", sd_value))

# Calculate the frequency of 'sexe' (gender) for each unique patient

sexe_frequency <- table(unique_patients$sexe)

# Print the frequency table

print(sexe_frequency)

c-

# Load required libraries

library(readxl)

library(dplyr)

# Load your data from '[Link]'

data <- read_excel("C:/Users/bnabi/OneDrive/Documents/nez/[Link]")

# Check the structure of your data

str(data)
# Remove duplicate patients based on 'code_patient', keeping their sex information

unique_patients <- data %>%

distinct(code_patient, sexe)

# Count the occurrences of each gender ('homme' and 'femme')

sexe_count <- table(unique_patients$sexe)

# Create the pie chart

pie(sexe_count,

labels = paste(names(sexe_count), ":", sexe_count),

col = c("lightblue", "lightpink"),

main = "Répartition des sexes par code patient",

clockwise = TRUE)

# Optional: Create a donut chart variation

pie(sexe_count,

labels = paste(names(sexe_count), ":", sexe_count),

col = c("lightblue", "lightpink"),

main = "Répartition des sexes par code patient",

radius = 0.7,

clockwise = TRUE)

# Print the frequency table

print(sexe_frequency)

# Load required libraries

library(readxl)
library(dplyr)

library(ggplot2)

# Load data

data <- read_excel("C:/Users/bnabi/OneDrive/Documents/nez/[Link]")

# Remove duplicates and get gender counts

unique_patients <- data %>%

distinct(code_patient, sexe)

sexe_count <- table(unique_patients$sexe)

# 1. Basic Bar Plot

barplot(sexe_count,

main = "Distribution des sexes",

xlab = "Sexe",

ylab = "Nombre de patients",

col = c("lightblue", "lightpink"))

# 2. Horizontal Bar Plot

barplot(sexe_count,

main = "Distribution des sexes",

xlab = "Nombre de patients",

ylab = "Sexe",

col = c("lightblue", "lightpink"),

horiz = TRUE)

# 3. ggplot2 Bar Plot


ggplot(data = [Link](sexe_count),

aes(x = Var1, y = Freq, fill = Var1)) +

geom_bar(stat = "identity") +

scale_fill_manual(values = c("lightblue", "lightpink")) +

labs(title = "Distribution des sexes",

x = "Sexe",

y = "Nombre de patients") +

theme_minimal()

# 4. Percentage calculation and visualization

percentages <- [Link](sexe_count) * 100

barplot(percentages,

main = "Distribution des sexes (%)",

xlab = "Sexe",

ylab = "Pourcentage",

col = c("lightblue", "lightpink"))

# 5. Create a data frame for detailed statistics

gender_stats <- [Link](

Gender = names(sexe_count),

Count = [Link](sexe_count),

Percentage = [Link](percentages)

# Print detailed statistics

print("Detailed Gender Statistics:")

print(gender_stats)
# 6. Additional statistics

total_patients <- sum(sexe_count)

print(paste("Total number of patients:", total_patients))

print("Percentages:")

print(round(percentages, 2))

# 7. ggplot2 fancy version with percentages

ggplot(gender_stats, aes(x = Gender, y = Count, fill = Gender)) +

geom_bar(stat = "identity", width = 0.7) +

geom_text(aes(label = sprintf("%.1f%%", Percentage)),

position = position_stack(vjust = 0.5)) +

scale_fill_manual(values = c("lightblue", "lightpink")) +

labs(title = "Distribution des sexes",

subtitle = paste("Total patients:", total_patients),

x = "Sexe",

y = "Nombre de patients") +

theme_minimal() +

theme([Link] = "none")

# 8. Density plot (if you have age data by gender)

# Uncomment and use if you have age data

# ggplot(data, aes(x = age, fill = sexe)) +

# geom_density(alpha = 0.5) +

# scale_fill_manual(values = c("lightblue", "lightpink")) +

# labs(title = "Distribution de l'âge par sexe",

# x = "Âge",
# y = "Densité") +

# theme_minimal()

e-

# Load required libraries

library(readxl)

library(dplyr)

library(ggplot2)

library(RColorBrewer) # For additional color options

# Load data

data <- read_excel("C:/Users/bnabi/OneDrive/Documents/nez/[Link]")

# Remove duplicates and get gender counts

unique_patients <- data %>%

distinct(code_patient, sexe)

sexe_count <- table(unique_patients$sexe)

# 1. Enhanced Basic Bar Plot

barplot(sexe_count,

main = "Distribution des sexes",

xlab = "Sexe",

ylab = "Nombre de patients",

col = c("#1f77b4", "#ff7f0e"), # Custom colors

border = "white", # Remove borders

[Link] = 1.5, # Larger title

[Link] = 1.2, # Larger axis labels

[Link] = 1.2) # Larger x-axis labels


# 2. Enhanced Horizontal Bar Plot

barplot(sexe_count,

main = "Distribution des sexes",

xlab = "Nombre de patients",

ylab = "Sexe",

col = c("#1f77b4", "#ff7f0e"),

horiz = TRUE,

border = "white",

[Link] = 1.5,

[Link] = 1.2,

[Link] = 1.2)

# 3. Enhanced ggplot2 Bar Plot

ggplot(data = [Link](sexe_count),

aes(x = Var1, y = Freq, fill = Var1)) +

geom_bar(stat = "identity", [Link] = FALSE) +

scale_fill_manual(values = c("#1f77b4", "#ff7f0e")) +

labs(title = "Distribution des sexes",

x = "Sexe",

y = "Nombre de patients") +

theme_minimal(base_size = 14) + # Larger base size for easier reading

theme([Link].x = element_text(size = 12, face = "bold"),

[Link].y = element_text(size = 12, face = "bold"),

[Link] = element_text(face = "bold", size = 16, hjust = 0.5),

[Link] = element_text(hjust = 0.5, size = 12),

[Link] = element_blank(),
[Link] = element_blank())

# 4. Enhanced Percentage Calculation and Plot

percentages <- [Link](sexe_count) * 100

barplot(percentages,

main = "Distribution des sexes (%)",

xlab = "Sexe",

ylab = "Pourcentage",

col = c("#1f77b4", "#ff7f0e"),

border = "white",

[Link] = 1.5,

[Link] = 1.2,

[Link] = 1.2)

# 5. Enhanced Detailed Statistics Table

gender_stats <- [Link](

Gender = names(sexe_count),

Count = [Link](sexe_count),

Percentage = round([Link](percentages), 2)

# Print enhanced statistics with titles

cat("\nDetailed Gender Statistics:\n")

print(gender_stats)

# 6. Additional Statistics

total_patients <- sum(sexe_count)


cat("\nTotal number of patients:", total_patients, "\n")

cat("\nPercentages:\n")

print(round(percentages, 2))

# 7. ggplot2 Fancy Version with Percentages and Custom Theme

ggplot(gender_stats, aes(x = Gender, y = Count, fill = Gender)) +

geom_bar(stat = "identity", width = 0.7) +

geom_text(aes(label = sprintf("%.1f%%", Percentage)),

position = position_stack(vjust = 0.5), size = 4, color = "white", fontface = "bold") +

scale_fill_manual(values = c("#1f77b4", "#ff7f0e")) +

labs(title = "Distribution des sexes",

subtitle = paste("Total patients:", total_patients),

x = "Sexe",

y = "Nombre de patients") +

theme_minimal(base_size = 14) +

theme([Link].x = element_text(size = 12, face = "bold"),

[Link].y = element_text(size = 12, face = "bold"),

[Link] = element_text(face = "bold", size = 16, hjust = 0.5),

[Link] = element_text(hjust = 0.5, size = 12),

[Link] = element_blank(),

[Link] = element_blank(),

[Link] = "none")

# 8. Enhanced Density Plot (if you have age data by gender)

# Uncomment and use if you have age data

# ggplot(data, aes(x = age, fill = sexe)) +

# geom_density(alpha = 0.5) +
# scale_fill_manual(values = c("#1f77b4", "#ff7f0e")) +

# labs(title = "Distribution de l'âge par sexe",

# x = "Âge",

# y = "Densité") +

# theme_minimal(base_size = 14) +

# theme([Link].x = element_text(size = 12, face = "bold"),

# [Link].y = element_text(size = 12, face = "bold"),

# [Link] = element_text(face = "bold", size = 16, hjust = 0.5),

# [Link] = element_text(hjust = 0.5, size = 12),

# [Link] = element_blank(),

# [Link] = element_blank())

4-

You might also like