Lab5_final

The echo: false option disables the printing of code (only output is displayed).

Lab 5 Graph 1: Legend Refinement

library(socviz)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)

pip1 <- gss_sm %>%         
  group_by(partyid, marital) %>%
  summarize(N = n()) %>% 
  mutate(freq = N/sum(N),
         pct = round((freq*100),0))
`summarise()` has grouped output by 'partyid'. You can override using the
`.groups` argument.
pip1
# A tibble: 45 × 5
# Groups:   partyid [9]
   partyid          marital           N   freq   pct
   <fct>            <fct>         <int>  <dbl> <dbl>
 1 Strong Democrat  Married         177 0.382     38
 2 Strong Democrat  Widowed          55 0.119     12
 3 Strong Democrat  Divorced         94 0.203     20
 4 Strong Democrat  Separated        20 0.0432     4
 5 Strong Democrat  Never Married   117 0.253     25
 6 Not Str Democrat Married         208 0.419     42
 7 Not Str Democrat Widowed          48 0.0968    10
 8 Not Str Democrat Divorced         65 0.131     13
 9 Not Str Democrat Separated        16 0.0323     3
10 Not Str Democrat Never Married   158 0.319     32
# ℹ 35 more rows
pip1_no_na <- subset(pip1, !is.na(partyid) & !is.na(marital) & !is.na(pct))

pip2 <- gss_sm %>%         
  group_by(partyid) %>%
  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: 9 × 6
  partyid                N age_mean sibs_mean   freq   pct
  <fct>              <int>    <dbl>     <dbl>  <dbl> <dbl>
1 Strong Democrat      463     53.8      4.54 0.161     16
2 Not Str Democrat     496     48.1      3.58 0.173     17
3 Ind,near Dem         405     45.2      3.58 0.141     14
4 Independent          473     44.5      4.00 0.165     16
5 Ind,near Rep         292     48.4      3.28 0.102     10
6 Not Str Republican   364     51.3      3.26 0.127     13
7 Strong Republican    270     55.9      3.34 0.0942     9
8 Other Party           72     47.9      3.57 0.0251     3
9 <NA>                  32     47.1      4.26 0.0112     1
ggplot(pip1_no_na, aes(x = partyid, y = pct, fill = marital)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Faceted Horizontal Bar Chart",
       subtitle = "Widowed status appears most prevalent in the Not St Republican category.",
       x = "Party ID", y = "Percentage") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom")  

The first refinement I did for this graph was changing the Legend position. Orginally the Legend was positioned to the right which made the graph appear smaller. Now that the Legend is on the bottom I think it looks cleaner. It also made the graph look a lot bigger to the point that I wasnt sure it was the same graph.

Lab 5 Graph 1: Legend Refinement

ggplot(pip1_no_na, aes(x = partyid, y = pct, fill = marital)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Faceted Horizontal Bar Chart",
       subtitle = "Widowed status appears most prevalent in the Not St Republican category.",
       x = "Party ID", y = "Percentage") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom",
        plot.title = element_text(size = rel(2.5)))  # Adjust plot title size

The second refinement I did for this chart was to make the title bigger. I think this makes the chart look better overall.

The data shows that within both Strong Democrats and Not Strong Democrats, the majority of respondents are married, with 38% and 42% respectively. A significant number of Not Strong Democrats have never been married (32%), which is notably higher than the 25% among Strong Democrats. The other categories—widowed, divorced, and separated—are less represented in both groups, suggesting marriage is the predominant status among the surveyed individuals, and there might be variations in political engagement based on marital status.

Lab 5 Graph 2: Theme Refinement

library(ggplot2)
library(ggthemes)
ggplot(pip1_no_na, aes(x = partyid, y = pct, fill = marital)) +
  geom_bar(stat = "identity") +
  labs(title = "Stacked Bar Chart", x = "Party ID", y = "Percentage",caption = "Source: socviz") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

ggplot(pip1_no_na, aes(x = partyid, y = pct, fill = marital)) +
  geom_bar(stat = "identity") +
  labs(title = "Stacked Bar Chart", x = "Party ID", y = "Percentage", caption = "Source: socviz") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom",
        legend.title = element_text(size = rel(.75)))  

The first refinement I did was to add a theme to my chart. I thought that a theme would improve the overall look of the graph. This specific one I wanted to use was wsj as it was used in the book. However, when I tried to use it I kept getting an error. So, I instead used the minimal theme.

Lab 5 Graph 2: Font Refinement

library(ggplot2)
library(ggthemes)

ggplot(pip1_no_na, aes(x = partyid, y = pct, fill = marital)) +
  geom_bar(stat = "identity") +
  labs(title = "Stacked Bar Chart", x = "Party ID", y = "Percentage", caption = "Source: socviz") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom",
        legend.title = element_text(size = rel(1.0)))  

The second refinement I did was to change the font size of the legend title. After making the legend title bigger the legend looks better as it is easier to distiunguish the legend from the title.

Across all party identifications, the majority of individuals are married, taking up more than half of each column. The ‘Never Married’ category is the second most significant segment, particularly among Independent and ‘Ind,near Dem’ groups. The ‘Divorced’ and ‘Widowed’ statuses show smaller proportions across all political affiliations, with the ‘Widowed’ category being the least represented among Strong Republicans and Independents. ‘Separated’ individuals form a relatively minor segment in each group. Interestingly, the proportions of ‘Married’ and ‘Never Married’ individuals seem inversely related across the political spectrum, suggesting varying marital trends among different political party supporters. The chart indicates that while marriage is common across all affiliations, nuances in the other marital statuses may reflect broader social trends within each political group.

Lab 5 Graph 2: Palette Refinement

highest_age_mean <- max(pip2$age_mean, na.rm = TRUE)

ggplot(pip2, aes(x = age_mean, y = sibs_mean, color = partyid)) +
  geom_point() +  
  labs(title = "Mean Age vs. Mean Number of Siblings by Party ID",
       x = "Mean Age",
       y = "Mean Number of Siblings",
       caption = "Source: socviz",
       color = "Party ID") +  
  theme_minimal() +  
  theme(legend.title = element_text(face = "bold", size = 12),  
        legend.position = "right",  
        legend.box = "vertical") +  
  scale_color_brewer(type = "qual", palette = "Set1", direction = -1) +  
  geom_text(data = subset(pip2, age_mean == highest_age_mean), 
            aes(label = paste("Highest Age Value:", age_mean)), 
            hjust = 1.0, vjust = 0, check_overlap = TRUE) +  
  guides(color = guide_legend(title = "Party ID", title.position = "top", label.position = "right", 
                              title.hjust = 0.5, label.hjust = 0.5))
Warning: Removed 1 rows containing missing values (`geom_point()`).

library(ggplot2)
library(ggthemes)
library(RColorBrewer) 
Warning: package 'RColorBrewer' was built under R version 4.1.2
my_palette <- brewer.pal(8, "Dark2")


highest_age_mean <- max(pip2$age_mean, na.rm = TRUE)

ggplot(pip2, aes(x = age_mean, y = sibs_mean, color = partyid)) +
  geom_point() +  
  labs(title = "Mean Age vs. Mean Number of Siblings by Party ID",
       x = "Mean Age",
       y = "Mean Number of Siblings",
       caption = "Source: socviz",
       color = "Party ID") +  
  scale_color_manual(values = my_palette) +
    theme_minimal() + 
  theme(legend.title = element_text(face = "bold", size = 12),  
        legend.position = "right",  
        legend.box = "vertical",  
        axis.text.x = element_text(angle = 45, hjust = 1)) +  
  geom_text(data = subset(pip2, age_mean == highest_age_mean), 
            aes(label = paste("Highest Age Value:", age_mean)), 
            hjust = 1.0, vjust = 0, check_overlap = TRUE) +
  guides(color = guide_legend(title = "Party ID", title.position = "top", 
                              label.position = "right", 
                              title.hjust = 0.5, label.hjust = 0.5))  

The first refinement I did for this graph was to change the color palette. The previous color palette had a yellow that was hard to see because it was so light. This color palette is dark2 which uses dark colors which are easily seen on the white background.

Lab 5 Graph 2: Legend and theme Refinements

library(ggplot2)
library(ggthemes)
library(RColorBrewer) 

my_palette <- brewer.pal(8, "Dark2")


highest_age_mean <- max(pip2$age_mean, na.rm = TRUE)

ggplot(pip2, aes(x = age_mean, y = sibs_mean, color = partyid)) +
  geom_point() +  
  labs(title = "Mean Age vs. Mean Number of Siblings by Party ID",
       x = "Mean Age",
       y = "Mean Number of Siblings",
       caption = "Source: socviz",
       color = "Party ID") +  
  scale_color_manual(values = my_palette) +
    
  theme(legend.title = element_text(face = "bold", size = 12),  
        legend.position = "top",  
        legend.box = "vertical", 
        plot.title = element_text(size = rel(1.5)),  
        axis.text.x = element_text(angle = 45, hjust = 1)) +  
  geom_text(data = subset(pip2, age_mean == highest_age_mean), 
            aes(label = paste("Highest Age Value:", age_mean)), 
            hjust = 1.0, vjust = 0, check_overlap = TRUE) +
  guides(color = guide_legend(title = "Party ID", title.position = "top", 
                              label.position = "right", 
                              title.hjust = 0.5, label.hjust = 0.5))  

For the second refinement I changed the position of the legend. Doing so elongates the graph giving it a different look. I also removed the minimal theme because I actually like the grey background that the regular chart has. I think it looks well against the colors.

The graph reveals several trends: Strong Democrats tend to be the oldest with the largest number of siblings, as indicated by their position in the top right, suggesting a trend of larger family sizes among older demographics within this political group. Strong Republicans also appear older on average but with fewer siblings than Strong Democrats, positioned in the bottom right. Independents and those near Democrats (Ind,near Dem) have a moderate number of siblings and are younger on average. Not Str Democrats, Ind,near Rep, and Not Str Republicans are clustered in the center, indicating a middle ground in both age and sibling count. The data generally shows that those with stronger political affiliations (either Democrat or Republican) are older, with varying family sizes, while those with weaker or no stated affiliations tend to be younger, with a slight tendency towards smaller families.