1 Introduction

This analysis examines the central tendency and distribution of extremism within specific communities based on political orientation (Left/Center/Right) in Israel across six waves of data collection. We focus on normative points (NP), extremism points (EP), and their ratios (EP/NP) to identify significant trends in extremism patterns.

community_variable <- "pe_left_center_right"
community_order <- c("left", "center", "right")

dimensions_order <- c("Overall", "Cognitive", "Behavioral", "Social")
wave_order <- c("First", "Second", "Third", "Fourth", "Fifth", "Sixth")

# Read the data from indices_table.txt
df <- as.data.frame(readRDS("Israel Survey/data/il_pe.RDS"))
indices_df <- af_gauge_indices(df, pop_var1 = "Wave", comm_var1 = community_variable, 
                               threshold_type = "MAD", k_factor = 1.5)
indices_data <- indices_df$indices_table

# Filter for rows that contain community data (non-NA community indices)
community_data <- indices_data %>% 
  filter(!is.na(cnp_c))

# Extract unique communities
communities <- unique(community_data[[community_variable]])

# Focus on the absolute indices for communities
community_absolute <- community_data %>%
  dplyr::select(Wave, !!sym(community_variable), cnp_c, bnp_c, snp_c, onp_c, cep_c, bep_c, sep_c, oep_c) %>%
  mutate(Wave = factor(Wave, levels = wave_order)) %>%
  mutate(!!sym(community_variable) := factor(!!sym(community_variable), levels = community_order))

# Calculate EP/NP ratios for each dimension within each community
community_ratios <- community_absolute %>%
  mutate(
    cep_cnp_ratio = cep_c / cnp_c,
    bep_bnp_ratio = bep_c / bnp_c,
    sep_snp_ratio = sep_c / snp_c,
    oep_onp_ratio = oep_c / onp_c
  )

2 Political Extremism by Dimension

The cognitive dimension (cnp_c) shows different patterns across the different communities. Secular communities display increasing values in later waves, with the highest values in waves Five and Six. center communities consistently show the lowest cognitive dimension values across most waves. The relationship between specific community and cognitive extremism appears to be complex.

The behavioral dimension (bnp_c) shows the least variation between communities among all three dimensions.Behavioral extremism shows very moderate growth (<5%) across all communities.

Except for the National Religious, the values of the other religious groups converge in later waves, with very small differences between by wave Six. This suggests that support for political violence (behavioral dimension) has become more homogeneous across religious communities over time.

Internal polarization is measures based on the EP/NP Ratio. Higher ratio means higher polarization.

The Secular and Traditional communities showed a consistent high polarization in the cognitive dimension (EP/NP ratio cep_c/cnp_c across waves). The Orthodox and National Ultra-Orthodox showed decreasing internal polarization. The Religious community showed a U-shaped patterns and the national-religious had stable level of internal polarization with smaller fluctuations.

2.1 Normative Points

plot_data <- community_absolute

p1 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "onp_c",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Overall - Normative Points",
                  y_label = "onp_c (1-7)",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p1)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "onp_c", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 3.30 3.23 3.65 3.52 3.30 3.49
center 2.72 2.68 2.70 2.72 2.82 2.91
right 3.29 3.23 3.07 3.05 3.17 3.14

p2 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "cnp_c",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Cognitive Dimension - Normative Points",
                  y_label = "cnp_c (1-7)",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p2)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "cnp_c", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 5.00 4.96 5.86 5.57 4.99 5.36
center 3.00 3.10 2.97 2.95 3.30 3.38
right 3.57 3.19 3.02 2.94 3.28 3.23

p3 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "bnp_c",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Behavioral Dimension - Normative Points",
                  y_label = "bnp_c (1-7)",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p3)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "bnp_c", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 1.06 1.08 1.05 1.08 1.19 1.25
center 1.06 1.09 1.09 1.11 1.13 1.18
right 1.13 1.20 1.18 1.14 1.16 1.16

p4 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "snp_c",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Social Dimension - Normative Points",
                  y_label = "snp_c (1-7)",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p4)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "snp_c", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 2.54 2.38 2.16 2.23 2.52 2.49
center 3.48 3.27 3.45 3.50 3.41 3.57
right 4.30 4.45 4.22 4.25 4.25 4.22

2.2 Extremism Points

p1 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "oep_c",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Overall - Extremism Points",
                  y_label = "oep_c (1-7)",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p1)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "oep_c", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 4.59 4.20 4.24 4.51 4.30 4.60
center 4.13 3.91 3.89 4.16 4.24 4.40
right 4.76 4.69 4.43 4.43 4.50 4.53

p2 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "cep_c",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Cognitive Dimension - Extremism Points",
                  y_label = "cep_c Value (1-7)",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p2)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "cep_c", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 8.00 7.39 7.00 7.15 7.56 7.65
center 5.14 5.05 5.01 4.99 5.58 5.86
right 6.13 5.23 4.82 4.68 5.31 5.26

p3 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "bep_c",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Behavioral Dimension - Extremism Points",
                  y_label = "bep_c (1-7)",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p3)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "bep_c", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 1.16 1.20 1.13 1.20 1.47 1.62
center 1.16 1.22 1.22 1.27 1.33 1.44
right 1.32 1.49 1.42 1.30 1.40 1.37

p4 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "sep_c",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Social Dimension - Extremism Points",
                  y_label = "sep_c (1-7)",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p4)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "sep_c", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 3.76 3.47 3.14 3.32 3.78 3.67
center 5.06 4.67 5.25 5.18 5.06 5.15
right 6.00 6.23 5.96 6.01 6.11 6.13

3 Political Extremism by Political Orientation

3.1 Left-Wing

plot_data <- community_absolute %>% dplyr::filter(!!sym(community_variable) == "left")
  
p1 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("onp_c", "cnp_c", "bnp_c", "snp_c"),
                                y_var_labels = dimensions_order,
                                title = "Left - Normative Points",
                                y_label = "Normative Points Value (1-7)", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p1)

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", y_var_names = c("onp_c", "cnp_c", "bnp_c", "snp_c"))
variable First Second Third Fourth Fifth Sixth
onp_c 3.30 3.23 3.65 3.52 3.30 3.49
cnp_c 5.00 4.96 5.86 5.57 4.99 5.36
bnp_c 1.06 1.08 1.05 1.08 1.19 1.25
snp_c 2.54 2.38 2.16 2.23 2.52 2.49

p2 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("oep_c", "cep_c", "bep_c", "sep_c"), 
                                y_var_labels = dimensions_order,
                                title = "Left - Extremism Points",
                                y_label = "Extremism Points Value (1-7)", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p2) 

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", y_var_names = c("oep_c", "cep_c", "bep_c", "sep_c"))
variable First Second Third Fourth Fifth Sixth
oep_c 4.59 4.20 4.24 4.51 4.30 4.60
cep_c 8.00 7.39 7.00 7.15 7.56 7.65
bep_c 1.16 1.20 1.13 1.20 1.47 1.62
sep_c 3.76 3.47 3.14 3.32 3.78 3.67

3.2 Center-Wing

plot_data <- community_absolute %>% dplyr::filter(!!sym(community_variable) == "center")

p1 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("onp_c", "cnp_c", "bnp_c", "snp_c"), 
                                y_var_labels = dimensions_order,
                                title = "Center - Normative Points",
                                y_label = "Normative Points Value (1-7)", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p1)

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", y_var_names = c("onp_c", "cnp_c", "bnp_c", "snp_c"))
variable First Second Third Fourth Fifth Sixth
onp_c 2.72 2.68 2.70 2.72 2.82 2.91
cnp_c 3.00 3.10 2.97 2.95 3.30 3.38
bnp_c 1.06 1.09 1.09 1.11 1.13 1.18
snp_c 3.48 3.27 3.45 3.50 3.41 3.57

p2 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("oep_c", "cep_c", "bep_c", "sep_c"), 
                                y_var_labels = dimensions_order,
                                title = "Center - Extremism Points",
                                y_label = "Extremism Points Value (1-7)", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p2) 

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", y_var_names = c("oep_c", "cep_c", "bep_c", "sep_c"))
variable First Second Third Fourth Fifth Sixth
oep_c 4.13 3.91 3.89 4.16 4.24 4.40
cep_c 5.14 5.05 5.01 4.99 5.58 5.86
bep_c 1.16 1.22 1.22 1.27 1.33 1.44
sep_c 5.06 4.67 5.25 5.18 5.06 5.15

3.3 Right-Wing

plot_data <- community_absolute %>% dplyr::filter(!!sym(community_variable) == "right")

p1 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("onp_c", "cnp_c", "bnp_c", "snp_c"),
                                y_var_labels = dimensions_order,
                                title = "Right - Normative Points",
                                y_label = "Normative Points Value (1-7)", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p1)

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", y_var_names = c("onp_c", "cnp_c", "bnp_c", "snp_c"))
variable First Second Third Fourth Fifth Sixth
onp_c 3.29 3.23 3.07 3.05 3.17 3.14
cnp_c 3.57 3.19 3.02 2.94 3.28 3.23
bnp_c 1.13 1.20 1.18 1.14 1.16 1.16
snp_c 4.30 4.45 4.22 4.25 4.25 4.22

p2 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("oep_c", "cep_c", "bep_c", "sep_c"),
                                y_var_labels = dimensions_order,
                                title = "Right - Extremism Points",
                                y_label = "Extremism Points Value (1-7)", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p2) 

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", y_var_names = c("oep_c", "cep_c", "bep_c", "sep_c"))
variable First Second Third Fourth Fifth Sixth
oep_c 4.76 4.69 4.43 4.43 4.50 4.53
cep_c 6.13 5.23 4.82 4.68 5.31 5.26
bep_c 1.32 1.49 1.42 1.30 1.40 1.37
sep_c 6.00 6.23 5.96 6.01 6.11 6.13

4 Internal Polarization

4.1 Internal Polarization by Dimension

plot_data <- community_ratios 

p1 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "oep_onp_ratio",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Overall - Internal Polarization",
                  y_label = "oep_c/onp_c",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p1)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "oep_onp_ratio", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 1.39 1.30 1.16 1.28 1.30 1.32
center 1.52 1.46 1.44 1.53 1.50 1.51
right 1.45 1.45 1.44 1.45 1.42 1.44

p2 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "cep_cnp_ratio",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Cognitive Dimension - Internal Polarization",
                  y_label = "cep_c/cnp_c",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p2)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "cep_cnp_ratio", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 1.60 1.49 1.20 1.28 1.52 1.43
center 1.71 1.63 1.69 1.69 1.69 1.74
right 1.72 1.64 1.60 1.59 1.62 1.63

p3 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "bep_bnp_ratio",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Behavioral Dimension - Internal Polarization",
                  y_label = "bep_c/bnp_c",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p3)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "bep_bnp_ratio", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 1.09 1.11 1.07 1.11 1.24 1.30
center 1.09 1.12 1.12 1.15 1.18 1.22
right 1.17 1.24 1.21 1.14 1.21 1.18

p4 <- af_create_xy_plot(data = plot_data, x_var = "Wave", y_var = "sep_snp_ratio",
                  grouping_variable = community_variable, show_points = TRUE,
                  title = "Social Dimension - Internal Polarization",
                  y_label = "sep_c/snp_c",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p4)

af_create_xy_table(df = plot_data, x_var = "Wave", y_var = "sep_snp_ratio", g_var = community_variable)
pe_left_center_right First Second Third Fourth Fifth Sixth
left 1.48 1.46 1.46 1.49 1.50 1.48
center 1.45 1.43 1.52 1.48 1.48 1.44
right 1.39 1.40 1.41 1.41 1.44 1.45

4.2 Internal Polarization by Political Orientation

plot_data <- community_ratios %>% dplyr::filter(!!sym(community_variable) == "left")
p1 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("oep_onp_ratio", 
                                                "cep_cnp_ratio", 
                                                "bep_bnp_ratio", 
                                                "sep_snp_ratio"), 
                                y_var_labels = dimensions_order,
                                title = "Left - Internal Polarization",
                                y_label = "EP / NP Ratio", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p1) 

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", 
                          y_var_names = c("oep_onp_ratio",
                                          "cep_cnp_ratio", 
                                          "bep_bnp_ratio", 
                                          "sep_snp_ratio"))
variable First Second Third Fourth Fifth Sixth
oep_onp_ratio 1.39 1.30 1.16 1.28 1.30 1.32
cep_cnp_ratio 1.60 1.49 1.20 1.28 1.52 1.43
bep_bnp_ratio 1.09 1.11 1.07 1.11 1.24 1.30
sep_snp_ratio 1.48 1.46 1.46 1.49 1.50 1.48

plot_data <- community_ratios %>% dplyr::filter(!!sym(community_variable) == "center")
p2 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("oep_onp_ratio", 
                                                "cep_cnp_ratio", 
                                                "bep_bnp_ratio", 
                                                "sep_snp_ratio"), 
                                y_var_labels = dimensions_order,
                                title = "Center - Internal Polarization",
                                y_label = "EP / NP Ratio", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p2)

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", 
                          y_var_names = c("oep_onp_ratio",
                                          "cep_cnp_ratio", 
                                          "bep_bnp_ratio", 
                                          "sep_snp_ratio"))
variable First Second Third Fourth Fifth Sixth
oep_onp_ratio 1.52 1.46 1.44 1.53 1.50 1.51
cep_cnp_ratio 1.71 1.63 1.69 1.69 1.69 1.74
bep_bnp_ratio 1.09 1.12 1.12 1.15 1.18 1.22
sep_snp_ratio 1.45 1.43 1.52 1.48 1.48 1.44

plot_data <- community_ratios %>% dplyr::filter(!!sym(community_variable) == "right")
p3 <- af_create_x_multi_y_plot (data = plot_data, x_var = "Wave", 
                                y_var_names = c("oep_onp_ratio",
                                                "cep_cnp_ratio", 
                                                "bep_bnp_ratio", 
                                                "sep_snp_ratio"), 
                                y_var_labels = dimensions_order,
                                title = "Right - Internal Polarization",
                                y_label = "EP / NP Ratio", 
                                legend_position = "bottom", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p3)

af_create_x_multi_y_table(df = plot_data, x_var = "Wave", 
                          y_var_names = c("oep_onp_ratio",
                                          "cep_cnp_ratio", 
                                          "bep_bnp_ratio", 
                                          "sep_snp_ratio"))
variable First Second Third Fourth Fifth Sixth
oep_onp_ratio 1.45 1.45 1.44 1.45 1.42 1.44
cep_cnp_ratio 1.72 1.64 1.60 1.59 1.62 1.63
bep_bnp_ratio 1.17 1.24 1.21 1.14 1.21 1.18
sep_snp_ratio 1.39 1.40 1.41 1.41 1.44 1.45

5 Community Volatility

Volatility refers to the extent of variation within a specific community or dimension across the six survey waves. It captures the degree to which a given dimension changes for each community over time. I measure volatility using the coefficient of variation (CV), calculated as the ratio of the standard deviation to the mean of normative points within each dimension. The CV provides a standardized measure of dispersion, indicating the degree of heterogeneity or instability in the central tendency of cognitive extremism across communities. Higher CV values reflect greater variability or dispersion.

# Calculate coefficient of variation for each community NP
community_cv <- community_absolute %>%
  group_by(!!sym(community_variable)) %>%
  summarize(
    cv_cnp = sd(cnp_c) / mean(cnp_c),
    cv_bnp = sd(bnp_c) / mean(bnp_c),
    cv_snp = sd(snp_c) / mean(snp_c),
    cv_onp = sd(onp_c) / mean(onp_c),
    .groups = "drop"
  ) %>%
  pivot_longer(
    cols = starts_with("cv_"),
    names_to = "dimension",
    values_to = "cv"
  ) %>%
  mutate(
    dimension = case_when(
      dimension == "cv_cnp" ~ "Cognitive",
      dimension == "cv_bnp" ~ "Behavioral",
      dimension == "cv_snp" ~ "Social",
      dimension == "cv_onp" ~ "Overall"
    ),
    dimension = factor(dimension, levels = dimensions_order)
  ) 

af_create_xy_bar(data = community_cv, x_var = community_variable, y_var = "cv",
                 fill_var = "dimension",
                 title = "Community Volatility in Normative Points Across Waves",
                 subtitle = "Higher CV means higher disperssion across waves",
                 x_label = "Community",
                 y_label = "Coefficient of Variation (CV)",
                 legend_position = "bottom")


# Calculate coefficient of variation for each community EP
community_cv <- community_absolute %>%
  group_by(!!sym(community_variable)) %>%
  summarize(
    cv_cep = sd(cep_c) / mean(cep_c),
    cv_bep = sd(bep_c) / mean(bep_c),
    cv_sep = sd(sep_c) / mean(sep_c),
    cv_oep = sd(oep_c) / mean(oep_c),
    .groups = "drop"
  ) %>%
  pivot_longer(
    cols = starts_with("cv_"),
    names_to = "dimension",
    values_to = "cv"
  ) %>%
  mutate(
    dimension = case_when(
      dimension == "cv_cep" ~ "Cognitive",
      dimension == "cv_bep" ~ "Behavioral",
      dimension == "cv_sep" ~ "Social",
      dimension == "cv_oep" ~ "Overall"
    ),
    dimension = factor(dimension, levels = dimensions_order)
  ) 

af_create_xy_bar(data = community_cv, x_var = community_variable, y_var = "cv",
                 fill_var = "dimension",
                 title = "Community Volatility in Extremism Points Across Waves",
                 subtitle = "Higher CV means higher disperssion across waves",
                 x_label = "Community",
                 y_label = "Coefficient of Variation (CV)",
                 legend_position = "bottom")

6 Dimensional Patterns Across Communities

Comparing all three dimensions reveals distinctly different patterns in how extremism manifests across religious communities. The social dimension shows the strongest and most consistent relationship with political orientation, the cognitive dimension shows a more complex non-linear relationship, and the behavioral dimension shows minimal community differences.

# NP average heatmap by community and dimension
dimension_summary <- community_absolute %>%
  group_by(!!sym(community_variable)) %>%
  summarize(
    avg_overall = mean(onp_c),
    avg_cognitive = mean(cnp_c),
    avg_behavioral = mean(bnp_c),
    avg_social = mean(snp_c),
    .groups = "drop"
  ) %>%
  arrange(desc(avg_social))

# Create a heatmap of average values
dimension_summary_long <- dimension_summary %>%
  pivot_longer(
    cols = starts_with("avg_"),
    names_to = "dimension",
    values_to = "average"
  ) %>%
  mutate(
    dimension = case_when(
      dimension == "avg_overall" ~ "Overall",
      dimension == "avg_cognitive" ~ "Cognitive",
      dimension == "avg_behavioral" ~ "Behavioral",
      dimension == "avg_social" ~ "Social"
    ),
    dimension = factor(dimension, levels = dimensions_order)
  ) 

af_create_heatmap(data = dimension_summary_long, x_var = "dimension", y_var = community_variable,
                  fill_var = "average",
                  title = "Average Normative Points by Community and Dimension",
                  x_label = "Dimension",
                  y_label = "Community",
                  legend_position = "bottom")


# EP average heatmap by community and dimension
dimension_summary <- community_absolute %>%
  group_by(!!sym(community_variable)) %>%
  summarize(
    avg_overall = mean(oep_c),
    avg_cognitive = mean(cep_c),
    avg_behavioral = mean(bep_c),
    avg_social = mean(sep_c),
    .groups = "drop"
  ) %>%
  arrange(desc(avg_social))

# Create a heatmap of average values
dimension_summary_long <- dimension_summary %>%
  pivot_longer(
    cols = starts_with("avg_"),
    names_to = "dimension",
    values_to = "average"
  ) %>%
  mutate(
    dimension = case_when(
      dimension == "avg_overall" ~ "Overall",
      dimension == "avg_cognitive" ~ "Cognitive",
      dimension == "avg_behavioral" ~ "Behavioral",
      dimension == "avg_social" ~ "Social"
    ),
    dimension = factor(dimension, levels = dimensions_order)
  ) 

af_create_heatmap(data = dimension_summary_long, x_var = "dimension", y_var = community_variable,
                  fill_var = "average",
                  title = "Average Extremism Points by Community and Dimension",
                  x_label = "Dimension",
                  y_label = "Community",
                  legend_position = "bottom")