library(tidyverse)
library(ggplot2)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Challenge 7
Challenge Overview
Today’s challenge is to:
- read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
- tidy data (as needed, including sanity checks)
- mutate variables as needed (including sanity checks)
- Recreate at least two graphs from previous exercises, but introduce at least one additional dimension that you omitted before using ggplot functionality (color, shape, line, facet, etc) The goal is not to create unneeded chart ink (Tufte), but to concisely capture variation in additional dimensions that were collapsed in your earlier 2 or 3 dimensional graphs.
- Explain why you choose the specific graph type
- If you haven’t tried in previous weeks, work this week to make your graphs “publication” ready with titles, captions, and pretty axis labels and other viewer-friendly features
R Graph Gallery is a good starting point for thinking about what information is conveyed in standard graph types, and includes example R code. And anyone not familiar with Edward Tufte should check out his fantastic books and courses on data visualizaton.
(be sure to only include the category tags for the data you use!)
Read in data
Read in one (or more) of the following datasets, using the correct R package and command.
- abc_poll ⭐⭐
read the abc_poll_2021.csv
<- read.csv("_data/abc_poll_2021.csv") df
Briefly describe the data
In this data, I want to look at the relationship between education and political party affiliation. Therefore, I table to show the value of my idea. In this table, the rows show 4 different education and the rows show 5 types of political here.
table(df$ppeducat,df$QPID)
A Democrat A Republican An Independent Skipped
Bachelors degree or higher 72 55 73 0
High school 37 41 43 1
Less than high school 12 10 4 1
Some college 55 46 48 1
Something else
Bachelors degree or higher 7
High school 11
Less than high school 2
Some college 8
Well, I don’t want to show some of political party affiliation with A or A[n] so I used the “str_remove” to remove the words.
Document your work here.
<- df %>%
df1 mutate(partyid = str_remove(QPID, "A[n]* "),
partyid = na_if(partyid, "Skipped")) %>%
select(-QPID)
Visualization with Multiple Dimensions
In the Visualization, I used the bar chat to show the Education with 5 different political. Therefore, it is easy for us to understand those.
<-table(df1$partyid,abc_poll_new$ppeducat) partyvseducation
Error in table(df1$partyid, abc_poll_new$ppeducat): 找不到物件 'abc_poll_new'
<- as.data.frame(table(partyvseducation)) myFrame
Error in table(partyvseducation): 找不到物件 'partyvseducation'
<-ggplot(abc_poll_new, aes(abc_poll_new$ppeducat, ..count..)) +
party_graphicgeom_bar(aes(fill = abc_poll_new$partyid), position = "dodge")
Error in ggplot(abc_poll_new, aes(abc_poll_new$ppeducat, ..count..)): 找不到物件 'abc_poll_new'
print(party_graphic + labs(fill = "Political", x = "Education"))
Error in print(party_graphic + labs(fill = "Political", x = "Education")): 找不到物件 'party_graphic'