#データフレーム名変更 df <- EVS_WVS_Joint_Stata_v4_0 #パッケージ読み込み library(dplyr) library(purrr) library(ggplot2) library(stringr) ##変数処理 #左右イデオロギーのDKNAダミー変数作成 df <- df %>% mutate(lrdk = e033) df <- df %>% mutate(lrdk = case_when( lrdk >= 1 & lrdk <= 10 ~ 1, lrdk == -1 ~ 0, lrdk >= -5 & lrdk <= -3 ~ NA_real_, TRUE ~ lrdk )) #分析使用変数の欠損値処理 df <- df %>% mutate(across(c("e033", "g006", "g257", "a124_06", "b008", "d059", "f118", "e114", "e235"), ~ case_when( . >= -5 & . <= -1 ~ NA_real_, TRUE ~ . ))) #環境設問の回答3を処理 df <- df %>% mutate(b008 = case_when( b008 == 3 ~ NA_real_, TRUE ~ b008 )) #解釈簡単化のためg006とg257の正負を逆に df$g006 <- 5 - df$g006 df$g257 <- 5 - df$g257 #dfにoecd変数を挿入 df <- df %>% mutate(oecd = ifelse(cntry_an %in% c('AR','AT','AU','BG','CA','CH','CL','CO','CZ','DE','DK','EE','ES','FI','FR','GB','GR','HU','IS','IT','JP','KR','LT','LV','MN','MX','NL','NO','NZ','PL','PR','PT','RO','SE','SI','SK','TW','US'), 1, 0)) #補助関数の事前作成。目的①国により無回答変数がある場合には分析対象から外し結果でNAを返す(エラー回避)。目的②表示桁数の設定。目的③相関係数とp値算出の補助関数を分ける。 handle_cor <- function(x, y) { if (sum(complete.cases(x, y)) > 1) { return(round(cor(x, y, use = "complete.obs"), 3)) } else { return(NA) } } handle_cor_test <- function(x, y) { if (sum(complete.cases(x, y)) > 1) { return(round(cor.test(x, y, use = "complete.obs")$p.value, 3)) } else { return(NA) } } #相関係数とp値の一覧が並んだ行列Zを作成 Z <- df %>% group_by(cntry_an) %>% summarise( cor_g006a124 = handle_cor(g006, a124_06), p_value_g006a124 = handle_cor_test(g006, a124_06), cor_e033g006 = handle_cor(e033, g006), p_value_e033g006 = handle_cor_test(e033, g006), cor_e033g257 = handle_cor(e033, g257), p_value_e033g257 = handle_cor_test(e033, g257), cor_e033a124 = handle_cor(e033, a124_06), p_value_e033a124 = handle_cor_test(e033, a124_06), cor_g257b008 = handle_cor(g257, b008), p_value_g257b008 = handle_cor_test(g257, b008), cor_g257d059 = handle_cor(g257, d059), p_value_g257d059 = handle_cor_test(g257, d059), cor_g257f118 = handle_cor(g257, f118), p_value_g257f118 = handle_cor_test(g257, f118), cor_g257e235 = handle_cor(g257, e235), p_value_g257e235 = handle_cor_test(g257, e235), cor_g006e235 = handle_cor(g006, e235), p_value_g006e235 = handle_cor_test(g006, e235), cor_a124e235 = handle_cor(a124_06, e235), p_value_a124e235 = handle_cor_test(a124_06, e235) ) #国名表記の変更(略称から国号ヘ) Z$cntry_an2 <- Z$cntry_an Z <- Z %>% mutate( cntry_an2 = str_replace(cntry_an2, "AR", "アルゼンチン"), cntry_an2 = str_replace(cntry_an2, "AT", "オーストリア"), cntry_an2 = str_replace(cntry_an2, "AU", "オーストラリア"), cntry_an2 = str_replace(cntry_an2, "BG", "ブルガリア"), cntry_an2 = str_replace(cntry_an2, "CA", "カナダ"), cntry_an2 = str_replace(cntry_an2, "CH", "スイス"), cntry_an2 = str_replace(cntry_an2, "CL", "チリ"), cntry_an2 = str_replace(cntry_an2, "CO", "コロンビア"), cntry_an2 = str_replace(cntry_an2, "CZ", "チェコ"), cntry_an2 = str_replace(cntry_an2, "DE", "ドイツ"), cntry_an2 = str_replace(cntry_an2, "DK", "デンマーク"), cntry_an2 = str_replace(cntry_an2, "EE", "エストニア"), cntry_an2 = str_replace(cntry_an2, "ES", "スペイン"), cntry_an2 = str_replace(cntry_an2, "FI", "フィンランド"), cntry_an2 = str_replace(cntry_an2, "FR", "フランス"), cntry_an2 = str_replace(cntry_an2, "GB", "イギリス"), cntry_an2 = str_replace(cntry_an2, "GR", "ギリシャ"), cntry_an2 = str_replace(cntry_an2, "HU", "ハンガリー"), cntry_an2 = str_replace(cntry_an2, "IS", "アイスランド"), cntry_an2 = str_replace(cntry_an2, "IT", "イタリア"), cntry_an2 = str_replace(cntry_an2, "JP", "日本"), cntry_an2 = str_replace(cntry_an2, "KR", "韓国"), cntry_an2 = str_replace(cntry_an2, "LT", "リトアニア"), cntry_an2 = str_replace(cntry_an2, "LV", "ラトビア"), cntry_an2 = str_replace(cntry_an2, "MN", "モンゴル"), cntry_an2 = str_replace(cntry_an2, "MX", "メキシコ"), cntry_an2 = str_replace(cntry_an2, "NL", "オランダ"), cntry_an2 = str_replace(cntry_an2, "NO", "ノルウェー"), cntry_an2 = str_replace(cntry_an2, "NZ", "ニュージーランド"), cntry_an2 = str_replace(cntry_an2, "PL", "ポーランド"), cntry_an2 = str_replace(cntry_an2, "PR", "プエルトリコ"), cntry_an2 = str_replace(cntry_an2, "PT", "ポルトガル"), cntry_an2 = str_replace(cntry_an2, "RO", "ルーマニア"), cntry_an2 = str_replace(cntry_an2, "SE", "スウェーデン"), cntry_an2 = str_replace(cntry_an2, "SI", "スロベニア"), cntry_an2 = str_replace(cntry_an2, "SK", "スロバキア"), cntry_an2 = str_replace(cntry_an2, "TW", "台湾"), cntry_an2 = str_replace(cntry_an2, "US", "アメリカ") ) #OECDダミーの作成 Z <- Z %>% mutate(oecd = ifelse(cntry_an %in% c('AR','AT','AU','BG','CA','CH','CL','CO','CZ','DE','DK','EE','ES','FI','FR','GB','GR','HU','IS','IT','JP','KR','LT','LV','MN','MX','NL','NO','NZ','PL','PR','PT','RO','SE','SI','SK','TW','US'), 1, 0)) ##行列Zを元に図表を作っていく## ##図0.1の3ナショナリズム相関 cor.test(df$g006, df$a124_06) cor.test(df$g257, df$a124_06) cor.test(df$g257, df$g006) #ウェイトをかける場合 library(weights) wtd.cor(df$g257, df$g006, w = df$pwght) wtd.cor(df$g257, df$a124_06, w = df$pwght) wtd.cor(df$g006, df$a124_06, w = df$pwght) #分析その0(図2.2:ナショナルプライドと排外主義相関) # 欠損値等を除く作業 Z0 <- subset(Z, oecd == 1) Z0 <- na.omit(Z0) # プロットの色と形を変数yに応じて設定 Z0$shape <- cut(Z0$p_value_g006a124, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("●", "◆", "×")) Z0$color <- cut(Z0$p_value_g006a124, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("black", "gray30", "gray60")) # xの値でデータフレームをソート Z0_sorted <- Z0[order(Z0$cor_g006a124), ] # プロット作成 ggplot(Z0_sorted, aes(y = reorder(cntry_an2, cor_g006a124), x = cor_g006a124, shape = shape, col = color)) + geom_point(size = 4) + scale_color_identity() + scale_shape_identity() + xlab("ナショナルプライドと排外主義の相関係数") + ylab("") + geom_vline(xintercept = 0) + ggtitle("") + theme_minimal() #分析その1(図3.1:ナショナルアイデンティティと左右認識) # 欠損値等を除く作業 Z2 <- na.omit(Z) Z2 <- subset(Z2, oecd == 1) # プロットの色と形を変数yに応じて設定 Z2$shape <- cut(Z2$p_value_e033g257, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("●", "◆", "×")) Z2$color <- cut(Z2$p_value_e033g257, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("black", "gray30", "gray60")) # xの値でデータフレームをソート Z2_sorted <- Z2[order(Z2$cor_e033g257), ] # プロット作成 ggplot(Z2_sorted, aes(y = reorder(cntry_an2, cor_e033g257), x = cor_e033g257, shape = shape, col = color)) + geom_point(size = 4) + scale_color_identity() + scale_shape_identity() + xlab("政治左右と帰属意識の相関係数") + ylab("") + geom_vline(xintercept = 0) + ggtitle("") + theme_minimal() #分析その1(図3.2a:ナショナルプライドと左右認識) # 欠損値等を除く作業 Z1 <- na.omit(Z) Z1 <- subset(Z1, oecd == 1) # プロットの色を変数yに応じて設定 Z1$shape <- cut(Z1$p_value_e033g006, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("●", "◆", "×")) Z1$color <- cut(Z1$p_value_e033g006, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("black", "gray30", "gray60")) # xの値でデータフレームをソート Z1_sorted <- Z1[order(Z1$cor_e033g006), ] # プロット作成 ggplot(Z1_sorted, aes(y = reorder(cntry_an2, cor_e033g006), x = cor_e033g006, shape = shape, col = color)) + geom_point(size = 4) + scale_color_identity() + scale_shape_identity() + xlab("政治左右とナショナルプライドの相関係数") + ylab("") + geom_vline(xintercept = 0) + ggtitle("") + theme_minimal() #分析その3(図3.2b:排外主義と左右認識)形を変える場合 # 欠損値等を除く作業 Z3 <- na.omit(Z) Z3 <- subset(Z3, oecd == 1) # プロットの色を変数yに応じて設定 Z3$shape <- cut(Z3$p_value_e033a124, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("●", "◆", "×")) Z3$color <- cut(Z3$p_value_e033a124, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("black", "gray30", "gray60")) # xの値でデータフレームをソート Z3_sorted <- Z3[order(Z3$cor_e033a124), ] # プロット作成 ggplot(Z3_sorted, aes(y = reorder(cntry_an2, cor_e033a124), x = cor_e033a124, shape=shape, col = color)) + geom_point(size = 4) + scale_color_identity() + scale_shape_identity() + xlab("政治左右と排外主義の相関係数") + ylab("") + geom_vline(xintercept = 0) + ggtitle("") + theme_minimal() #分析その4(図4.1ジェンダー平等意識d59およびLGBT権利意識f118とナショナルアイデンティティ) # 2相関の平均値データを作成 Z$genlgbt <- (Z$cor_g257d059 + Z$cor_g257f118)/2 # 欠損値等を除く作業 Z4 <- na.omit(Z) Z4 <- subset(Z4, oecd == 1) # プロットの色を変数yに応じて設定 Z4$color4a <- cut(Z4$p_value_g257d059, breaks = c(-Inf, 0.05, Inf), labels = c("black", "gray75")) Z4$color4b <- cut(Z4$p_value_g257f118, breaks = c(-Inf, 0.05, Inf), labels = c("black", "gray75")) Z4$shape4a <- cut(Z4$p_value_g257d059, breaks = c(-Inf, 0.05, Inf), labels = c("▲", "△")) Z4$shape4b <- cut(Z4$p_value_g257f118, breaks = c(-Inf, 0.05, Inf), labels = c("▼", "▽")) # プロット作成 ggplot(Z4) + geom_point(aes(x = cor_g257d059, y = reorder(cntry_an2, genlgbt), col=color4a, shape=shape4a)) + geom_point(aes(x = cor_g257f118, y = reorder(cntry_an2, genlgbt), col=color4b, shape=shape4b)) + labs(title = " ", x = " ", y = " ") + geom_vline(xintercept = 0) + scale_color_identity() + scale_shape_identity() + theme_bw() + theme(legend.position = "none") #分析その5(図4.2:環境主義とナショナルアイデンティティ) # 欠損値等を除く作業 Z5 <- na.omit(Z) Z5 <- subset(Z5, oecd == 1) # プロットの色を変数yに応じて設定 Z5$color5 <- cut(Z5$p_value_g257b008, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("black", "gray30", "gray60")) Z5$shape5 <- cut(Z5$p_value_g257b008, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("●", "◆", "×")) # プロット作成 ggplot(Z5) + geom_point(size=4, aes(x = cor_g257b008, y = reorder(cntry_an2, cor_g257b008), shape = shape5, color=color5)) + labs(title = " ", x = " ", y = " ") + geom_vline(xintercept = 0) + scale_color_identity() + scale_shape_identity() + theme_bw() + theme(legend.position = "none") #分析その6(図5.1 ナショナルアイデンティティ・ナショナルプライドと民主的規範) # 2相関の平均値データを作成 Z$democ <- (Z$cor_g257e235 + Z$cor_g006e235)/2 # 欠損値等を除く作業 Z6 <- na.omit(Z) Z6 <- subset(Z6, oecd == 1) # プロットの色を変数yに応じて設定 Z6$color6a <- cut(Z6$p_value_g257e235, breaks = c(-Inf, 0.05, Inf), labels = c("black", "gray75")) Z6$color6b <- cut(Z6$p_value_g006e235, breaks = c(-Inf, 0.05, Inf), labels = c("black", "gray75")) Z6$shape6a <- cut(Z6$p_value_g257e235, breaks = c(-Inf, 0.05, Inf), labels = c("▲", "△")) Z6$shape6b <- cut(Z6$p_value_g006e235, breaks = c(-Inf, 0.05, Inf), labels = c("▼", "▽")) # プロット作成 ggplot(Z6) + geom_point(aes(x = cor_g257e235, y = reorder(cntry_an2, democ), col=color6a, shape=shape6a)) + geom_point(aes(x = cor_g006e235, y = reorder(cntry_an2, democ), col=color6b, shape=shape6b)) + labs(title = " ", x = " ", y = " ") + geom_vline(xintercept = 0) + scale_color_identity() + scale_shape_identity() + theme_bw() + theme(legend.position = "none") #分析その7(図5.2 排外主義と民主的規範) # 2相関の平均値データを作成 # 欠損値等を除く作業 Z7 <- na.omit(Z) Z7 <- subset(Z7, oecd == 1) # プロットの色を変数yに応じて設定 Z7$color7 <- cut(Z7$p_value_a124e235, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("black", "gray30", "gray60")) Z7$shape7 <- cut(Z7$p_value_a124e235, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("●", "◆", "×")) # プロット作成 ggplot(Z7) + geom_point(size=4, aes(x = cor_a124e235, y = reorder(cntry_an2, cor_a124e235), shape = shape7, color=color7)) + labs(title = " ", x = " ", y = " ") + geom_vline(xintercept = 0) + scale_color_identity() + scale_shape_identity() + theme_bw() + theme(legend.position = "none")