Graphing Descriptive Stats - ATP PEW
New data frame “atp_selected”:
Q19 - Pivot Longer; new dataframe “Leadership” where Confidence_in_world_leaders is new column name
Confidence_in_world_leaders Level Freq
1 Chinese_Jinping_Confidence A lot of confidence 29
2 French_Macron_Confidence A lot of confidence 259
3 German_Merkel_Confidence A lot of confidence 592
4 Russian_Putin_Confidence A lot of confidence 38
5 US_Biden_Confidence A lot of confidence 859
6 Chinese_Jinping_Confidence Some confidence 296
Calculate percentages
Leadership <- Leadership %>%
group_by(`Confidence_in_world_leaders`, Level) %>%
summarise(Freq = n()) %>%
mutate(percentage = formattable::percent(Freq / sum(Freq)))
head(Leadership)
# A tibble: 6 × 4
# Groups: Confidence_in_world_leaders [2]
Confidence_in_world_leaders Level Freq percentage
<chr> <fct> <int> <formttbl>
1 Chinese_Jinping_Confidence A lot of confidence 29 1.12%
2 Chinese_Jinping_Confidence Some confidence 296 11.40%
3 Chinese_Jinping_Confidence Not too much confidence 1043 40.18%
4 Chinese_Jinping_Confidence No confidence at all 1170 45.07%
5 Chinese_Jinping_Confidence Refused 58 2.23%
6 French_Macron_Confidence A lot of confidence 259 9.98%
There may be a different way to calculate these percentages. Need to pull out the “Refused” responses eventually.
Shows confidence Americans have in world leaders
Leadership <- Leadership %>%
select(Confidence_in_world_leaders, Level, percentage)
head(Leadership)
# A tibble: 6 × 3
# Groups: Confidence_in_world_leaders [2]
Confidence_in_world_leaders Level percentage
<chr> <fct> <formttbl>
1 Chinese_Jinping_Confidence A lot of confidence 1.12%
2 Chinese_Jinping_Confidence Some confidence 11.40%
3 Chinese_Jinping_Confidence Not too much confidence 40.18%
4 Chinese_Jinping_Confidence No confidence at all 45.07%
5 Chinese_Jinping_Confidence Refused 2.23%
6 French_Macron_Confidence A lot of confidence 9.98%
ggplot(Leadership, aes(x=percentage, y=Confidence_in_world_leaders, fill=Level))+
geom_bar(stat = "identity", position = position_dodge())+
labs(x="Confidence Level", y="World Leaders",
title="American Confidence in World Leadership")+
theme_classic()
Here, we take a look at American’s confidence in world leaders by their education level (F_EDUCAT).
education <- atp_selected %>%
select(US_Biden_Confidence, Chinese_Jinping_Confidence, Russian_Putin_Confidence,
German_Merkel_Confidence, French_Macron_Confidence, F_EDUCCAT)
education <- pivot_longer(education, cols = c(US_Biden_Confidence, Chinese_Jinping_Confidence,
Russian_Putin_Confidence, German_Merkel_Confidence,
French_Macron_Confidence),
names_to = "confidence_in_world_leaders", values_to = "level")
Leadership_conf_ed_summary <- Leadership_conf_ed_summary %>%
pivot_wider(names_from = "level", values_from = "freq")
Leadership_conf_ed_summary$Confidence<- Leadership_conf_ed_summary$`A lot of confidence`+
Leadership_conf_ed_summary$`Some confidence`
ggplot(Leadership_conf_ed_summary, aes(x=confidence_in_world_leaders, y = Confidence, fill = F_EDUCCAT)) +
geom_histogram(stat = "identity", position = position_dodge())+
labs(x="World Leaders", y="Confidence", title="Confidence in World Leadership by Education")+
coord_flip() +
theme_classic()
Question 35
US_cooperation_polit_lean <- atp_selected %>%
select(Intl_Collaboration, F_PARTYSUMIDEO) %>%
filter(Intl_Collaboration %in% c("MANY of the problems facing our country can be solved by working with other countries","FEW of the problems facing our country can be solved by working with other countries")) %>%
group_by(`Intl_Collaboration`, F_PARTYSUMIDEO)%>%
summarise(freq = n()) %>%
mutate(percentage = formattable::percent(freq / sum(freq)))
head(US_cooperation_polit_lean)
# A tibble: 6 × 4
# Groups: Intl_Collaboration [2]
Intl_Collaboration F_PARTYSUMIDEO freq percentage
<fct> <fct> <int> <formttbl>
1 MANY of the problems facing our cou… Conservative … 188 13.19%
2 MANY of the problems facing our cou… Moderate/Libe… 156 10.95%
3 MANY of the problems facing our cou… Moderate/Cons… 483 33.89%
4 MANY of the problems facing our cou… Liberal Dem/L… 543 38.11%
5 MANY of the problems facing our cou… Refused eithe… 55 3.86%
6 FEW of the problems facing our coun… Conservative … 501 43.95%
ggplot(US_cooperation_polit_lean, aes(x=Intl_Collaboration, y=percentage, fill=F_PARTYSUMIDEO))+
geom_bar(stat = "identity", position = position_dodge())+
labs(x="Collaborative Approach", y="Level", title = "American interest in intl. collaboration to solve world problems, by Polit. Party")+
theme_classic()
Need to label legend, clean up x axis, pull out refused, and rethink variables.
Question 33 and 35;the proportion of people who rate a FX_Policy-goal at top priority (Q33), based on how they answered Intl-Collaboration (Q35)
intl <- atp_selected %>%
select(c(9:19))
intl <- pivot_longer(intl, cols = c(Reduce_WeaponsMD,American_Jobs,Strengthen_UN,
Reduce_USMil_Overseas, Limit_Russia_Power, Promote_Democracy_Overseas,
Reduce_Illegal_Immigr, Limit_China_Power, Maintain_USMil_Advantage,
Global_Climate_Change), names_to = "FX_Policy-goals", values_to ="Level") %>% group_by(`FX_Policy-goals`,`Intl_Collaboration`) %>%
filter(Intl_Collaboration %in% c("MANY of the problems facing our country can be solved by working with other countries","FEW of the problems facing our country can be solved by working with other countries")) %>%
summarise(freq = n()) %>%
mutate(percentage = formattable::percent(freq / sum(freq)))
head(intl)
# A tibble: 6 × 4
# Groups: FX_Policy-goals [3]
`FX_Policy-goals` Intl_Collaboration freq percentage
<chr> <fct> <int> <formttbl>
1 American_Jobs MANY of the problems facing … 1425 55.56%
2 American_Jobs FEW of the problems facing o… 1140 44.44%
3 Global_Climate_Change MANY of the problems facing … 1425 55.56%
4 Global_Climate_Change FEW of the problems facing o… 1140 44.44%
5 Limit_China_Power MANY of the problems facing … 1425 55.56%
6 Limit_China_Power FEW of the problems facing o… 1140 44.44%
dim(intl)
[1] 20 4
Data frame with 4 columns; 2 variables (FX Policy Goals, Intl_Collaboration) and 2 values columns (one count; other percentage).
On y axis, want to plot different foreign policy goals On x axis, want the the percentage based on their response to Intl_Cooperation (ppl responding “FEW” vs. “MANY”) On a horizontal line with both percentage connected on a continuum along the x axis.
I tried to 2 methods, neither are working.
Method 1
intl %>%
ggplot(aes(x=percentage, y=`FX_Policy-goals`, color=Intl_Collaboration))+
geom_point()
Method 2?
Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
For attribution, please cite this work as
Garibian (2022, March 23). Data Analytics and Computational Social Science: Homework_4. Retrieved from https://github.com/DACSS/dacss_course_website/posts/httpsrpubscomlenna717881430/
BibTeX citation
@misc{garibian2022homework_4, author = {Garibian, Lenna}, title = {Data Analytics and Computational Social Science: Homework_4}, url = {https://github.com/DACSS/dacss_course_website/posts/httpsrpubscomlenna717881430/}, year = {2022} }