######A1Tuvial Diagrams #Adapted from: https://github.com/corybrunson /ggalluvial #install.packages (“ggalluvial”) library(“ggalluvial”) library(“tidyverse”) library(“packcircles”) library(“dplyr”) library(“ggplot2”) ####21 students are divided in 3 groups, they are men and women, and they get High pass,
#### create some fake data about student performance in classes
#### 21 students are divided in 3 groups, they are men and women, and they get high pass, pass or fail
library("ggalluvial")
## 载入需要的程辑包:ggplot2
library("dplyr")
##
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
group1<- tibble::tibble(groupid=groupid<-rep("group1",7),
studentid=sample(seq(from=1,to=20),7),
gender=sample(c("M","F"),7,replace=TRUE),
grades=sample(c("High Pass","Pass","Fail"),7,replace =TRUE))
group2<- tibble::tibble(groupid=groupid<-rep("group2",7),
studentid=sample(seq(from=21,to=30),7),
gender=sample(c("M","F"),7,replace=TRUE),
grades=sample(c("High Pass","Pass","Fail"),7,replace =TRUE))
group3<- tibble::tibble(groupid=groupid<-rep("group3",7),
studentid=sample(seq(from=31,to=40),7),
gender=sample(c("M","F"),7,replace=TRUE),
grades=sample(c("High Pass","Pass","Fail"),7,replace =TRUE))
students<- bind_rows(group1,group2,group3)
students_table<-students%>%
group_by(groupid,gender,grades) %>%
count()
ggplot(students_table, aes(axis1=groupid,axis2=grades,y=n))+
geom_alluvium(aes(fill=gender))+
geom_stratum()+
geom_text(stat="stratum",
aes(label=after_stat(stratum)))+
scale_fill_manual(values=c("purple","green"))
#packed circle figure
#install.packages("packcircles")
####use congress data as an example
#### sample a number of members from the 114th congress
library("tidyverse")
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble 3.1.6 ✓ purrr 0.3.4
## ✓ tidyr 1.1.4 ✓ stringr 1.4.0
## ✓ readr 2.1.1 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library("packcircles")
cel <- read_csv(url("https://www.dropbox.com/s/4ebgnkdhhxo5rac/cel_volden_wiseman%20_coursera.csv?raw=1"))
## Rows: 10262 Columns: 38
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): thomas_name, st_name
## dbl (36): thomas_num, icpsr, congress, year, cd, dem, elected, female, votep...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cel_114<- cel %>% dplyr::filter(congress==114)
members<- sample_n(cel_114,25)
packing<- circleProgressiveLayout(members$all_pass,sizetype = 'area')
## Warning in circleProgressiveLayout(members$all_pass, sizetype = "area"): missing
## and/or non-positive sizes will be ignored
members<-add_column(members,packing)
dat.gg<-circleLayoutVertices(packing,npoints = 50)
ggplot()+
geom_polygon(data=dat.gg,
aes(x=x,
y=y,
group=id,
fill=as.factor(id),
alpha=0.6))+
geom_text(data=members,
aes(x=x,
y=y,
size=all_pass,
label=thomas_name))+
theme(legend.position = "none")+
coord_equal()
## Warning: Removed 9 rows containing missing values (geom_text).
#### pie charts
Prop<-c(3,7,9,1,2)
pie(Prop)