1 Required libraries and source files

library(dplyr)
library(ggplot2)
library(tidyverse)  
library(patchwork)  # For arranging plots side by side
library(viridis)
library(afcommon)

2 Creating Sample Dataset

  data <- mtcars
  data$cyl <- factor(data$cyl)
  data$am <- factor(data$am)
  data$vs <- factor(data$vs)

3 Density

af_create_y_plot(data, y_var = "mpg", group_var = "cyl", plot_types = c("density"))


af_create_y_plot(data, y_var = "mpg", group_var = "cyl", subgroup_var = "am", plot_types = c("density"))


af_create_y_plot(data, y_var = "mpg", group_var = "cyl", subgroup_var = "am", plot_types = c("density"), 
                 use_facet = TRUE)

4 Histograms

af_create_y_plot(data, y_var = "mpg", plot_types = c("histogram"), bins = 10)


af_create_y_plot(data, y_var = "mpg", plot_types = c("histogram"), bins = 30)


af_create_y_plot(data, y_var = "mpg", group_var = "cyl", plot_types = c("histogram"), bins = 10)


af_create_y_plot(data, y_var = "mpg", group_var = "cyl", plot_types = c("histogram dodged"), bins = 10)


af_create_y_plot(data, y_var = "mpg", group_var = "cyl", subgroup_var = "am", 
                 plot_types = c("histogram dodged"), bins = 10)


af_create_y_plot(data, y_var = "mpg", group_var = "cyl", subgroup_var = "am", plot_types = c("histogram dodged"), 
                 bins = 10, use_facet = TRUE, facet_cols = 2)

5 Bar Chart

af_create_y_plot(data, y_var = "cyl", plot_types = c("bar"))


af_create_y_plot(data, y_var = "cyl", group_var = "cyl", plot_types = c("bar"))

6 Boxplot

af_create_y_plot(data, y_var = "mpg", group_var = "cyl", plot_types = c("boxplot"))

7 Violin

af_create_y_plot(data, y_var = "mpg", group_var = "cyl", plot_types = c("violin"))

8 Multiple types

af_create_y_plot(data, y_var = "mpg", group_var = "cyl", plot_types = c("density", "histogram"), bins = 10)

9 XY Plot

9.1 Basic Usage - Just Y

# Basic usage - plotting horsepower vs mpg
af_create_xy_plot(
  data = mtcars,
  y_var = "hp",
  grouping_variable = "None",
  show_points = TRUE
)

9.2 Basic Usage - X and Y

# Basic usage - plotting horsepower vs mpg
af_create_xy_plot(
  data = mtcars,
  x_var = "mpg",
  y_var = "hp",
  grouping_variable = "None",
  show_points = TRUE
)

9.3 Smoothing

# Basic usage - plotting horsepower vs mpg
af_create_xy_plot(
  data = mtcars,
  x_var = "mpg",
  y_var = "hp",
  smooth = TRUE,
  grouping_variable = "None",
  show_points = TRUE
)

9.4 Grouping and Faceting

# Creating facets by cylinder count
af_create_xy_plot(
  data = mtcars,
  x_var = "mpg",
  y_var = "hp",
  grouping_variable = "cyl",
  use_facet = TRUE,
  facet_cols = 3,
  show_points = TRUE
)

10 Multi-Y Plots

10.1 Just Y

# Plotting horsepower, displacement and weight against mpg
af_create_x_multi_y_plot(
  data = mtcars,
  y_var_names = c("hp", "disp", "wt"),
  show_points = TRUE
)

10.2 Basic X and Multi-Y Plot

# Plotting horsepower, displacement and weight against mpg
af_create_x_multi_y_plot(
  data = mtcars,
  x_var = "mpg",
  y_var_names = c("hp", "disp", "wt"),
  show_points = TRUE
)

10.3 Smoothing

Add trend lines to see relationships more clearly (note that connecting lines are removed when smoothing is enabled):

# Adding smoothed trend lines
af_create_x_multi_y_plot(
  data = mtcars,
  x_var = "mpg",
  y_var_names = c("hp", "disp", "wt"),
  smooth = TRUE,
  show_points = TRUE
)

10.4 Faceting

# Creating facets for each y variable
af_create_x_multi_y_plot(
  data = mtcars,
  x_var = "mpg",
  y_var_names = c("hp", "disp", "wt"),
  use_facet = TRUE,
  facet_cols = 3,
  show_points = TRUE
)

10.5 Aggregation

mean aggregation by cylinder count

# Using mean aggregation by cylinder count
af_create_x_multi_y_plot(
  data = mtcars,
  x_var = "cyl",
  y_var_names = c("hp", "disp", "wt"),
  agg_func = "mean",
  show_points = TRUE
)

11 Correlation Plots

af_plot_correlation(mtcars, vars = NULL, title = "Mtcars Correlation Plot")


af_combine_plots(
  af_plot_correlation_per_group(mtcars, group_var = "cyl", group_values = NULL, vars = NULL)
)

12 QQ plot

af_plot_qq(mtcars, "mpg")

13 Scatter Map plot

Create example dataset (HDI vs Extremism data)

set.seed(123)
countries <- c("USA", "FRA", "GER", "JPN", "RUS", "CHN", "BRA", "IND", "MEX", "CAN", 
               "ARG", "ESP", "ITA", "AUS", "NED", "SWE", "POL", "TUR", "EGY", "SAF", 
               "NOR", "FIN", "DEN", "GRE", "POR", "CZE", "HUN", "AUT", "BEL", "IRE",
               "MAL", "ETH", "PAK", "BAN", "VIE", "NIC", "HOK", "TAJ", "ALB", "ARM")

regions <- c(rep("West", 10), 
             rep("Latin/Central America", 5), 
             rep("Ex-communist countries", 8), 
             rep("Arab countries", 5), 
             rep("Asia", 7), 
             rep("Sub-Saharan Africa", 5))

# Create sample data frame
df <- data.frame(
  country_code = countries,
  region = regions,
  HDI = runif(40, min = 40, max = 95),
  total_extremism = rnorm(40, mean = 0, sd = 20)
)

Example 1: With coloring by region and a linear trend line

af_scatter_map(df, 
               identity_var_name = "country_code", 
               x_var_name = "HDI", 
               y_var_name = "total_extremism", 
               color_var_name = "region", 
               line_type = "linear")

Example 2: With coloring by region and a smooth trend line

af_scatter_map(df, 
               identity_var_name = "country_code", 
               x_var_name = "HDI", 
               y_var_name = "total_extremism", 
               color_var_name = "region", 
               line_type = "smooth")

14 Saving a plot

af_ggsave("my_last_plot.png", display = FALSE)