dimensions_order <- c("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"))

1 Dimensions

# Plot the three dimensions
p <- af_create_x_multi_y_plot (data = df, x_var = "Wave", 
                                y_var_names = c("pe_ideology", "pe_violence", "pe_intolerance"),
                                y_var_labels = dimensions_order,
                                title = "Political Extremism Dimensions", 
                                subtitle = ("Median Values"),
                                y_label = "Median (1-7)", agg_func = "median",
                                legend_position = "right", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)

af_create_x_multi_y_table(df, x_var = "Wave", 
                          y_var_names = c("pe_ideology", "pe_violence", "pe_intolerance"),
                          agg_func = median)
variable First Second Third Fourth Fifth Sixth
pe_ideology 3.57 3.36 3.23 3.15 3.43 3.40
pe_intolerance 3.75 3.72 3.81 3.79 3.83 3.80
pe_violence 1.11 1.15 1.13 1.13 1.16 1.18

2 Sub-Dimensions

# Plot Violence sub-dimensions
p <- af_create_x_multi_y_plot (data = df, x_var = "Wave", 
                                y_var_names = c("pe_violence", "pe_violence_state", "pe_violence_outgroup"),
                                y_var_labels = c("Violence", "Violence-State", "Violence-Outgroup"),
                                title = "Behavioral (Violence) Sub-Dimensions", 
                                subtitle = ("Violence combined, Violence against the State, and Violence against the Outgroup"),
                                y_label = "Median (1-7)", agg_func = "median",
                                legend_position = "right", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)

af_create_x_multi_y_table(df, x_var = "Wave", 
                          y_var_names = c("pe_violence", "pe_violence_state", "pe_violence_outgroup"),
                          agg_func = median)
variable First Second Third Fourth Fifth Sixth
pe_violence 1.11 1.15 1.13 1.13 1.16 1.18
pe_violence_outgroup 2.00 2.12 2.00 2.00 2.00 2.24
pe_violence_state 1.00 1.05 1.05 1.05 1.05 1.05

# Plot Intolerance sub-dimensions
p <- af_create_x_multi_y_plot (data = df, x_var = "Wave", 
                                y_var_names = c("pe_intolerance", "pe_intolerance_political", "pe_intolerance_social"),
                                y_var_labels = c("Intolerance", "Political Exclusion", "Social Distancing"),
                                title = "Social (Intolerance) Sub-Dimensions", 
                                subtitle = ("Intolerance combined, Political Exclusion, and Social Distancing"),
                                y_label = "Median (1-7)", agg_func = "median",
                                legend_position = "right", show_points = TRUE) + 
  scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)

af_create_x_multi_y_table(df, x_var = "Wave", 
                          y_var_names = c("pe_intolerance", "pe_intolerance_political", "pe_intolerance_social"),
                          agg_func = median)
variable First Second Third Fourth Fifth Sixth
pe_intolerance 3.75 3.72 3.81 3.79 3.83 3.80
pe_intolerance_political 3.02 2.99 3.03 2.98 2.96 3.10
pe_intolerance_social 4.67 4.79 4.72 4.80 4.83 4.96

3 Sub-Dimensions by Political Orientation

3.1 Behavioral (Violence)

p <- af_create_xy_plot(data = df, x_var = "Wave", y_var = "pe_violence",
                  grouping_variable = "pe_left_center_right", show_points = TRUE,
                  title = "Behavioral (Violence) Dimension by Political Orientation",
                  y_label = "Median (1-7)", agg_func = "median",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)

# af_create_xy_table(df, x_var = "Wave", y_var = "pe_violence", g_var = "pe_left_center_right",
#                    agg_func = median)

p <- af_create_xy_plot(data = df, x_var = "Wave", y_var = "pe_violence_state",
                  grouping_variable = "pe_left_center_right", show_points = TRUE,
                  title = "Violence-State by Political Orientation",
                  y_label = "Median (1-7)", agg_func = "median",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)


p <- af_create_xy_plot(data = df, x_var = "Wave", y_var = "pe_violence_outgroup",
                  grouping_variable = "pe_left_center_right", show_points = TRUE,
                  title = "Violence-Outgroup by Political Orientation",
                  y_label = "Median (1-7)", agg_func = "median",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)

3.2 Social (Intolerance)

p <- af_create_xy_plot(data = df, x_var = "Wave", y_var = "pe_intolerance",
                  grouping_variable = "pe_left_center_right", show_points = TRUE,
                  title = "Social (Intolerance) Dimension by Political Orientation",
                  y_label = "Median (1-7)", agg_func = "median",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)


p <- af_create_xy_plot(data = df, x_var = "Wave", y_var = "pe_intolerance_political",
                  grouping_variable = "pe_left_center_right", show_points = TRUE,
                  title = "Political Exclusion by Political Orientation",
                  y_label = "Median (1-7)", agg_func = "median",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)


p <- af_create_xy_plot(data = df, x_var = "Wave", y_var = "pe_intolerance_social",
                  grouping_variable = "pe_left_center_right", show_points = TRUE,
                  title = "Social Distancing by Political Orientation",
                  y_label = "Median (1-7)", agg_func = "median",
                  legend_position = "bottom") + scale_x_discrete(limits = levels(df$Wave))
af_event_labels(p)

4 Least Liked

af_create_leastliked_table(df, title = md("Least Liked Groups by Wave - **All Respondents**"))
Least Liked Groups by Wave - All Respondents
Least Liked Group First Second Third Fourth Fifth Sixth
Settlers in Judea and Samaria 5.8 7.4 0.0 0.0 0.0 0.0
Israeli Arabs 22.4 33.6 22.1 21.1 21.5 25.9
Leftists 15.0 12.6 15.8 14.5 14.0 13.1
Rightists 2.4 2.4 4.0 3.4 3.1 2.9
Middle Eastern Jews 0.7 0.8 0.1 0.7 0.9 1.1
Ashkenazi Jews 0.1 0.5 1.4 1.3 1.0 0.5
Ultra-Orthodox Jews 21.8 18.7 21.8 21.2 24.6 24.7
Secular Jews 0.3 0.5 1.1 0.6 0.5 1.0
SOviet Union Immigrants 1.2 1.1 0.8 1.6 0.9 0.6
Ethiopians 0.6 0.7 0.5 1.0 0.7 0.6
African Migrants/Refugees 24.3 17.9 25.2 28.8 26.4 23.9
Foreign Workers 5.3 0.0 0.0 0.0 0.0 0.0
LGBTQ+ 0.0 3.9 7.3 5.7 6.5 5.7

af_create_leastliked_table(df, political_orientation = "left", 
                           title = md("Least Liked Groups by Wave - **Left** Political Orientation"))
Least Liked Groups by Wave - Left Political Orientation
Least Liked Group First Second Third Fourth Fifth Sixth
Settlers in Judea and Samaria 23.9 28.2 0.0 0.0 0.0 0.0
Israeli Arabs 4.5 8.6 4.7 4.5 3.9 4.8
Leftists 0.4 0.4 0.9 1.1 0.6 0.0
Rightists 9.5 9.0 18.9 16.9 13.5 16.1
Middle Eastern Jews 0.4 0.8 0.0 0.0 0.6 2.4
Ashkenazi Jews 0.0 0.0 1.9 0.0 0.6 0.0
Ultra-Orthodox Jews 50.2 40.0 54.7 60.7 57.9 58.1
Secular Jews 0.4 0.0 1.9 0.0 0.0 1.6
SOviet Union Immigrants 0.0 0.8 0.9 1.1 1.1 0.0
Ethiopians 0.4 0.4 0.9 2.2 0.0 0.0
African Migrants/Refugees 8.2 11.0 14.2 12.4 19.1 15.3
Foreign Workers 2.1 0.0 0.0 0.0 0.0 0.0
LGBTQ+ 0.0 0.8 0.9 1.1 2.8 1.6

af_create_leastliked_table(df, political_orientation = "center", 
                           title = md("Least Liked Groups by Wave - **Center** Political Orientation"))
Least Liked Groups by Wave - Center Political Orientation
Least Liked Group First Second Third Fourth Fifth Sixth
Settlers in Judea and Samaria 5.4 9.5 0.0 0.0 0.0 0.0
Israeli Arabs 13.6 23.8 16.8 10.6 14.0 18.6
Leftists 5.6 4.2 5.9 5.3 5.5 4.9
Rightists 2.4 2.7 4.2 3.7 3.1 2.7
Middle Eastern Jews 1.5 1.5 0.0 1.1 1.3 1.2
Ashkenazi Jews 0.4 0.6 0.8 1.1 0.4 0.6
Ultra-Orthodox Jews 31.9 31.8 28.2 29.6 38.7 38.1
Secular Jews 0.2 0.6 1.7 1.1 0.2 0.6
SOviet Union Immigrants 0.6 0.4 1.7 2.1 1.1 0.3
Ethiopians 0.9 1.5 0.4 1.1 0.7 0.9
African Migrants/Refugees 31.2 21.1 33.2 39.7 31.7 26.5
Foreign Workers 6.2 0.0 0.0 0.0 0.0 0.0
LGBTQ+ 0.0 2.3 7.1 4.8 3.3 5.5

af_create_leastliked_table(df, political_orientation = "right", 
                           title = md("Least Liked Groups by Wave - **Right** Political Orientation"))
Least Liked Groups by Wave - Right Political Orientation
Least Liked Group First Second Third Fourth Fifth Sixth
Settlers in Judea and Samaria 1.1 0.6 0.0 0.0 0.0 0.0
Israeli Arabs 31.7 45.8 27.9 29.4 28.9 33.5
Leftists 23.9 20.4 23.1 21.5 21.0 19.6
Rightists 0.6 0.5 0.9 0.5 1.1 0.5
Middle Eastern Jews 0.3 0.5 0.2 0.7 0.7 0.8
Ashkenazi Jews 0.0 0.6 1.5 1.7 1.3 0.6
Ultra-Orthodox Jews 9.0 5.7 12.5 9.1 10.7 11.8
Secular Jews 0.3 0.6 0.7 0.5 0.7 1.1
SOviet Union Immigrants 1.8 1.5 0.4 1.4 0.8 0.9
Ethiopians 0.6 0.3 0.4 0.7 0.8 0.6
African Migrants/Refugees 25.0 18.0 23.8 27.4 25.1 24.2
Foreign Workers 5.8 0.0 0.0 0.0 0.0 0.0
LGBTQ+ 0.0 5.6 8.7 7.2 8.9 6.5