library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.1.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(gganimate)
library(gifski)
library(transformr)
data(mtcars)
ggplot(data=mtcars,aes(x=factor(cyl),y=mpg))+
geom_boxplot()+
facet_wrap(~gear)

my_anim<-ggplot(data=mtcars,aes(x=factor(cyl),y=mpg))+
geom_boxplot()+
transition_states(
gear
)
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...
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(cel)
## # A tibble: 6 x 38
## thomas_num thomas_name icpsr congress year st_name cd dem elected female
## <dbl> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 1 Abdnor, Ja~ 14000 93 1973 SD 2 0 1972 0
## 2 2 Abzug, Bel~ 13001 93 1973 NY 20 1 1970 1
## 3 3 Adams, Bro~ 10700 93 1973 WA 7 1 1964 0
## 4 4 Addabbo, J~ 10500 93 1973 NY 7 1 1960 0
## 5 6 Alexander,~ 12000 93 1973 AR 1 1 1968 0
## 6 8 Anderson, ~ 12001 93 1973 CA 35 1 1968 0
## # ... with 28 more variables: votepct <dbl>, dwnom1 <dbl>, deleg_size <dbl>,
## # speaker <dbl>, subchr <dbl>, afam <dbl>, latino <dbl>, votepct_sq <dbl>,
## # power <dbl>, chair <dbl>, state_leg <dbl>, state_leg_prof <dbl>,
## # majority <dbl>, maj_leader <dbl>, min_leader <dbl>, meddist <dbl>,
## # majdist <dbl>, all_bills <dbl>, all_aic <dbl>, all_abc <dbl>,
## # all_pass <dbl>, all_law <dbl>, les <dbl>, seniority <dbl>, benchmark <dbl>,
## # expectation <dbl>, TotalInParty <dbl>, RankInParty <dbl>
### transition_time
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...
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(cel)
## # A tibble: 6 x 38
## thomas_num thomas_name icpsr congress year st_name cd dem elected female
## <dbl> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 1 Abdnor, Ja~ 14000 93 1973 SD 2 0 1972 0
## 2 2 Abzug, Bel~ 13001 93 1973 NY 20 1 1970 1
## 3 3 Adams, Bro~ 10700 93 1973 WA 7 1 1964 0
## 4 4 Addabbo, J~ 10500 93 1973 NY 7 1 1960 0
## 5 6 Alexander,~ 12000 93 1973 AR 1 1 1968 0
## 6 8 Anderson, ~ 12001 93 1973 CA 35 1 1968 0
## # ... with 28 more variables: votepct <dbl>, dwnom1 <dbl>, deleg_size <dbl>,
## # speaker <dbl>, subchr <dbl>, afam <dbl>, latino <dbl>, votepct_sq <dbl>,
## # power <dbl>, chair <dbl>, state_leg <dbl>, state_leg_prof <dbl>,
## # majority <dbl>, maj_leader <dbl>, min_leader <dbl>, meddist <dbl>,
## # majdist <dbl>, all_bills <dbl>, all_aic <dbl>, all_abc <dbl>,
## # all_pass <dbl>, all_law <dbl>, les <dbl>, seniority <dbl>, benchmark <dbl>,
## # expectation <dbl>, TotalInParty <dbl>, RankInParty <dbl>
###Plot total number of D and Rs across Congresses
cel$party<-recode(cel$dem,'1'="Democrat",'0'="Republican")
cong_dat<- cel%>%
group_by(year,party)%>%
summarise("Seats"=n())
## `summarise()` has grouped output by 'year'. You can override using the `.groups` argument.
anim2<-ggplot(cong_dat,aes(x=year,y=Seats,fill=party))+
geom_bar(stat="identity")+
geom_hline(yintercept = 217)+
scale_fill_manual(values = c("blue","red"))+
transition_time(year)
anim2<-ggplot(cong_dat,aes(x=year,y=Seats,fill=party))+
geom_bar(stat="identity")+
geom_hline(yintercept = 217)+
scale_fill_manual(values = c("blue","red"))+
transition_time(year)
ggplot()+
geom_jitter(aes(x=seniority,y=all_pass,color=party),data=filter(cel,congress==115 & party=="Democrat"))+
geom_jitter(aes(x=seniority,y=all_pass,color=party),data=filter(cel,congress==115 & party=="Republican"))+
geom_smooth(aes(x=seniority,y=all_pass,color=party),data=filter(cel,congress==115 & party=="Democrat"))+
geom_smooth(aes(x=seniority,y=all_pass,color=party),data=filter(cel,congress==115 & party=="Republican"))+
scale_color_manual(values = c("blue","red"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

anim3<-ggplot()+
geom_jitter(aes(x=seniority,y=all_pass,color=party),data=filter(cel,congress==115 & party=="Democrat"))+
geom_jitter(aes(x=seniority,y=all_pass,color=party),data=filter(cel,congress==115 & party=="Republican"))+
geom_smooth(aes(x=seniority,y=all_pass,color=party),data=filter(cel,congress==115 & party=="Democrat"))+
geom_smooth(aes(x=seniority,y=all_pass,color=party),data=filter(cel,congress==115 & party=="Republican"))+
scale_color_manual(values = c("blue","red"))+
transition_layers()
ggplot(mtcars,aes(factor(cyl),mpg))+
geom_boxplot()+
transition_states(factor(cyl))

# fade in, fade-out
anim0<- my_anim+
enter_fade()+
exit_fade()
#shadowing
anim4<-ggplot(cong_dat,aes(x=year,y=Seats,fill=party))+
geom_bar(stat = "identity")+
geom_hline(yintercept = 217)+
scale_fill_manual(values = c("blue","red"))+
transition_time(year)+
shadow_wake(wake_length = 1,alpha=FALSE,wrap = FALSE)
anim_save("test.gif",animation=anim4)
library(plotly)
##
## 载入程辑包:'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
set.seed(955)
# Make some noisily increasing data
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
p <- ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) # Use hollow circles
fig <- ggplotly(p)
fig
ggplotly(ggplot(cel,aes(x=seniority,y=les,color=party,fame=year))+
geom_point()+
labs(x="Seniority",y="leg. Effectiveness")+
scale_color_manual(values = c("blue","red"))
)
### add an ids option to create object constancy - if the same member is in multiple congresses, move that point independently
ggplotly(ggplot(cel,aes(x=seniority,y=les,color=party,fame=year,ids=thomas_name))+
geom_point()+
labs(x="Seniority",y="leg. Effectiveness")+
scale_color_manual(values = c("blue","red"))
)