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: 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.