Import Your Data

In the following code hunk, import your data.

#### Use read_csv() or another function

#### Make sure your data is converted into a tibble. 

#### For demonstration purposes, this example uses the mtcars data.

dat <- drop_na(read.csv(url("https://www.dropbox.com/s/uhfstf6g36ghxwp/cces_sample_coursera.csv?raw=1")))
dat<-tibble(dat)

Figure 1(Plotly)

fig_dat1<-dat %>% select(educ,ideo5)
####make sure you call the data so it will display in your report
fig_dat1<-rename(fig_dat1,education_level=educ,ideology_level=ideo5)
fig1<-ggplot(fig_dat1,aes(x=ideology_level,y=education_level))+
  geom_jitter()+
  geom_smooth(method="lm",level=.95)+
  labs(x="Ideology level (1=Very liberal, 5=Very conservative)",y="Education level",title="Correlation between Education and Ideology level")
ggplotly(fig1)
## `geom_smooth()` using formula = 'y ~ x'

Figure 2

fig_dat2<-dat %>% select(gender,ideo5)
####make sure you call the data so it will display in your report
fig_dat2<-rename(fig_dat2,ideology_level=ideo5)
fig_dat2$gender<-recode(fig_dat2$gender,`1`="Male",`2`="Female")
tibble(fig_dat2)
## # A tibble: 869 × 2
##    gender ideology_level
##    <chr>           <int>
##  1 Male                3
##  2 Female              2
##  3 Female              3
##  4 Female              1
##  5 Male                5
##  6 Male                4
##  7 Female              5
##  8 Male                1
##  9 Male                5
## 10 Female              3
## # ℹ 859 more rows
ggplot(fig_dat2,aes(x=ideology_level,fill=gender,group=gender))+
  geom_bar(position="dodge")+
  labs(x="Ideology level (1=Very liberal, 5=Very conservative)",y="Count",title="Counts of different Ideology levels grouped by gender")

Figure 3

fig_dat3<-dat %>% filter(pid7==4) %>% select(ideo5) 
####make sure you call the data so it will display in your report
fig_dat3<-rename(fig_dat3,ideology_level=ideo5)
ggplot(fig_dat3,aes(x=ideology_level))+
  geom_density()+
  labs(x="Ideology level (1=Very liberal, 5=Very conservative)",y="Probability Density",title="Independent voter distribution on different ideology levels")

Figure 4(Plotly)

fig_dat4<-dat %>% select(educ,pew_religimp)
####make sure you call the data so it will display in your report
fig_dat4<-rename(fig_dat4,education_level=educ,religion_importance=pew_religimp)
fig4<-ggplot(fig_dat4,aes(x=religion_importance,y=education_level))+
  geom_jitter()+
  geom_smooth(level=.95)+
  labs(x="Religion importance(1=Very imp.,4=Not imp.)",y="Education level",title="Correlation between Education and Religion importance")
ggplotly(fig4)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : pseudoinverse used at 0.985
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : neighborhood radius 2.015
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : reciprocal condition number 9.3403e-16
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : There are other near singularities as well. 1
## Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
## else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
## 0.985
## Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
## else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 2.015
## Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
## else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
## number 9.3403e-16
## Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
## else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other near
## singularities as well. 1

Figure 5

fig_dat5<-dat %>% select(CC18_308a,gender)
####make sure you call the data so it will display in your report
fig_dat5<-rename(fig_dat5,Trump_approval=CC18_308a)
fig_dat5$gender<-recode(fig_dat5$gender,`1`="Male",`2`="Female")
ggplot(fig_dat5,aes(x=Trump_approval,fill=gender,alpha = 0.5))+
  geom_density(show.legend = FALSE)+
  facet_wrap(~gender)+
  labs(x="Trump approval(1=Strongly approve, 4=Strongly disapprove)",y="Probability Density",title="Trump aprroval level distribution, female versus male")

Figure 6

fig_dat6<-dat %>% select(faminc_new,gender) %>% filter(faminc_new<=8)
####make sure you call the data so it will display in your report
fig_dat6<-rename(fig_dat6,year_income=faminc_new)
fig_dat6$gender<-recode(fig_dat6$gender,`1`="Male",`2`="Female")
fig_dat6$year_income<-recode(fig_dat6$year_income,`1`="<10k",`2`="10~20k",`3`="20~30k",`4`="30~40k",`5`="40~50k",`6`="50~60k",`7`="60~70k",`8`="70~80k")
ggplot(fig_dat6,aes(x=year_income,fill=gender,group=gender))+
  geom_bar(position="dodge")+
  labs(x="Year income(USD)",y="Count",title="Counts of different levels of year income grouped by gender,<80k USD")

Figure 7

fig_dat7<-dat %>% select(faminc_new,gender) %>% filter(faminc_new>9)
fig_dat7<-rename(fig_dat7,year_income=faminc_new)
fig_dat7$gender<-recode(fig_dat7$gender,`1`="Male",`2`="Female")
fig_dat7$year_income<-recode(fig_dat7$year_income,`9`="80~100k",`10`="100~120k",`11`="120~150k",`12`="150~200k",`13`="200~250k",`14`="250~350k",`15`="350~500k",`16`="500k above")
ggplot(fig_dat7,aes(x=year_income,fill=gender,group=gender))+
  geom_bar(position="dodge")+
  labs(x="Year income(USD)",y="Count",title="Counts of different levels of year income grouped by gender,>100k USD")+
  annotate("text",x=7,y=6,label="No Female",color="red")

Figure 8(Plotly)

fig_dat8<-dat %>% select(ideo5,newsint)
####make sure you call the data so it will display in your report
fig_dat8<-rename(fig_dat8,ideology_level=ideo5)
fig8<-ggplot(fig_dat8,aes(x=ideology_level,y=newsint))+
  geom_jitter()+
  geom_smooth(method="lm",level=.95)+
  labs(x="Ideology level (1=Very liberal, 5=Very conservative)",y="Frequency of following public affairs(4=highest)",title="Correlation between Ideo. levels & Freq. of following public affairs")
ggplotly(fig8)
## `geom_smooth()` using formula = 'y ~ x'