LAB 5

Author

Gabriela Chajon

options(scipen=999) 
library(tidyverse)
library(socviz)
library(ggplot2)
head(gss_sm)
# A tibble: 6 × 32
   year    id ballot   age childs sibs  degree race  sex   region income16 relig
  <dbl> <dbl> <labe> <dbl>  <dbl> <lab> <fct>  <fct> <fct> <fct>  <fct>    <fct>
1  2016     1 1         47      3 2     Bache… White Male  New E… $170000… None 
2  2016     2 2         61      0 3     High … White Male  New E… $50000 … None 
3  2016     3 3         72      2 3     Bache… White Male  New E… $75000 … Cath…
4  2016     4 1         43      4 3     High … White Fema… New E… $170000… Cath…
5  2016     5 3         55      2 2     Gradu… White Fema… New E… $170000… None 
6  2016     6 2         53      2 2     Junio… White Fema… New E… $60000 … None 
# ℹ 20 more variables: marital <fct>, padeg <fct>, madeg <fct>, partyid <fct>,
#   polviews <fct>, happy <fct>, partners <fct>, grass <fct>, zodiac <fct>,
#   pres12 <labelled>, wtssall <dbl>, income_rc <fct>, agegrp <fct>,
#   ageq <fct>, siblings <fct>, kids <fct>, religion <fct>, bigregion <fct>,
#   partners_rc <fct>, obama <dbl>

Part 1

Summarizing data

library(socviz)
pip1 <- gss_sm %>%         
  group_by(degree, polviews) %>%
  summarize(N = n()) %>% 
  mutate(freq = N/sum(N),
         pct = round((freq*100),0))
pip1
# A tibble: 43 × 5
# Groups:   degree [6]
   degree         polviews                   N   freq   pct
   <fct>          <fct>                  <int>  <dbl> <dbl>
 1 Lt High School Extremely Liberal         19 0.0579     6
 2 Lt High School Liberal                   29 0.0884     9
 3 Lt High School Slightly Liberal          34 0.104     10
 4 Lt High School Moderate                 112 0.341     34
 5 Lt High School Slightly Conservative     41 0.125     12
 6 Lt High School Conservative              39 0.119     12
 7 Lt High School Extremely Conservative    24 0.0732     7
 8 Lt High School <NA>                      30 0.0915     9
 9 High School    Extremely Liberal         51 0.0349     3
10 High School    Liberal                  132 0.0903     9
# ℹ 33 more rows

Interpretation

The table shows the count of observations for each combination of degree and political views. Just by seeing the rows the data shows that the biggest amount is 607 observations for people that have a high school degree and presents moderate political views. It does make sense because it is common for the majority of the population to identify with such views.

Part 2

Stacked bar chart

p <- ggplot(data = subset(pip1, !is.na(degree) & !is.na(polviews)), 
                        aes(x=degree, y=pct, fill = polviews))

p + geom_col(position = "stack") +
    labs(x="Degree", y="Percent", fill = "Political views",
         title = "Degree and Political views" , 
         subtitle = "As a stacked bar chart") +
    geom_text(aes(label=pct), position = position_stack(vjust=.5))

Dodged Bar chart

p + geom_col(position = "dodge2") +
    labs(x="Degree", y="Percent", fill = "Political views",
         title = "Degree and political views", 
         subtitle = "As a dodged bar chart") + 
    geom_text(aes(label = pct), position = position_dodge(width = .9))

Faceted horizontal bar chart

p + geom_col(position = "dodge2") +
    labs(x=NULL, y="Percent", fill = "Religion",
         title = "Relationship of degree and political view", 
         subtitle = "As a faceted horizontal bar chart") +
         guides(fill = "none") +
         coord_flip() +
         facet_grid(~ polviews) +
    geom_text(aes(label = pct), position = position_dodge2(width = 1))

Interpretation

The charts show that people with moderate political views are significantly more than others while extremely liberal and conservative views have the less percentage. It is not very significant but the percentage of people who identify as extremely liberal or liberal increases with higher levels of education.

Part 3

Summarizing data (Add mean for 2 continuous variables)

pip2 <- gss_sm %>%         
  group_by(polviews) %>%
  summarize(N = n(),
            age_mean = mean(age, na.rm = TRUE),
            sibs_mean = mean(sibs, na.rm = TRUE)) %>%
  mutate(freq = N/sum(N),
         pct = round((freq*100), 0))

pip2
# A tibble: 8 × 6
  polviews                   N age_mean sibs_mean   freq   pct
  <fct>                  <int>    <dbl>     <dbl>  <dbl> <dbl>
1 Extremely Liberal        136     48.6      3.43 0.0474     5
2 Liberal                  350     46.8      3.29 0.122     12
3 Slightly Liberal         310     47.3      3.33 0.108     11
4 Moderate                1032     48.0      3.89 0.360     36
5 Slightly Conservative    382     50.3      3.72 0.133     13
6 Conservative             426     53.8      3.59 0.149     15
7 Extremely Conservative   120     54.3      3.93 0.0419     4
8 <NA>                     111     46.6      5.18 0.0387     4

Part 4

Scatterplot with 2 continuous and 1 categorical

This code does not remove NA – make sure you deal with that.

pip2 <- na.omit(pip2)
p <- ggplot(pip2, aes(x=sibs_mean, y=age_mean, color=polviews))
p + geom_point(size=5) +
    annotate(geom = "text", x = 3.55, y=49, 
                     label = "More liberal responses show lower # of siblings", hjust=0.5) +
    labs(y="Average Age", x="Average Number of siblings.", 
         title="Age and Number of siblings by political views", 
         subtitle = "Average liberal political views decrease with less # of children",
         caption <- "gss_sm dataset{socviz}")

Interpretation

More liberal responses show lower number of children. However it is harder to know if the relationship is significant because the average numbers are very close and the range is short.

Part 5

Modifications in the legend

p <- ggplot(pip2, aes(x = sibs_mean, y = age_mean, color = polviews, fill = polviews)) +
  geom_point(size = 5) +
  annotate(geom = "text", x = 3.7, y = 53, 
           label = "Average conservative responses are the highest in age", hjust = 0.6) +
  labs(y = "Average Age", x = "Average Number of siblings",
       title = "Relationship between Average Age and Number of Siblings by Political Views",
       color = "Political Views", fill = "Political Views") +
  theme(legend.title = element_text(color = "darkmagenta", size = 14, face = "bold"),
        legend.position = "right",  # Adjust legend position
        axis.text = element_text(size = 15),  # Adjust axis text size
        axis.title = element_text(size = 15),  # Adjust axis title size
        plot.title = element_text(size = 13, face = "bold")) +  # Adjust plot title size
  scale_color_brewer(palette = "Set3") +
  scale_fill_brewer(palette = "Set3")

p

Part 6

Replace labels

p <- ggplot(pip2, aes(x=sibs_mean, y=age_mean, color=polviews))
p + geom_point(size=2) +
    geom_text(mapping = aes(label=polviews), hjust=1, size=3) +
      annotate(geom = "text", x = 4, y=53, 
                     label = "Conservative and moderate responses show bigger # of siblings", hjust=0.5) +
    annotate(geom = "text", x = 3.5, y=49, 
                     label = "Average liberal responses are from younger people", hjust=0.5) +
    labs(y="Average Age", x="Average Number of Siblings", 
         title="Relationship between Average Age and Number of Siblings by Political View", 
         color = "Political Views") +
    theme(legend.position = "none") +
      scale_x_continuous(limits = c(3, 5))

PART 7

The chart shows that conservative political views are somewhat positively correlated to age of the respondents. The results make sense as older generations have usually more conservative beliefs and perspectives as they tend to become more resistant to change. Furthermore , people with a higher average number of conservative and moderate responses tend to fall into the higher average of siblings, but this is harder to determine because of the small range. The slight relationship between those two can be contributed to higher levels of peer influence or maybe socioeconomic status.