Functions to run exploratory data analysis on a drug use dataset:
create_bar_use_plot(): Creates a bar plot for substance use.create_bar_freq_plot(): Creates a bar plot for substance use frequency.create_scatter_plot(): Creates a scatter plot with a regression line.aggregate_data(): Aggregates data for youth vs. adult comparison.create_grouped_bar_plot(): Creates grouped bar plots for youth vs. adult comparison. Function to create bar plot for substance use
Arguments
- data
A .csv dataframe containing data to be plotted.
- x_var
A string specifying the x-axis variable.
- y_var
A string specifying the y-axis variable.
- title
A string specifying the plot title.
- x_label
A string specifying the x-axis label.
- y_label
A string specifying the y-axis label.
- fill_color
A string specifying the fill color for the bars.
- output_file
A string specifying the output file path for saving the plot.
Examples
sample_data <- data.frame(
age = c("18-24", "25-34", "35-44"),
alcohol.use = c(80, 75, 60),
alcohol.frequency = c(20, 15, 10),
marijuana.use = c(50, 30, 20),
heroin.frequency = c(5, 3, 2),
class = c("youth", "adult", "adult"),
n = c(100, 200, 150)
)
create_bar_use_plot(
sample_data,
"age",
"alcohol.use",
"Alcohol Use",
"Age",
"Proportion",
"dodgerblue",
"output/eda-test/test1.png"
)