library(data.table)
## Warning: package 'data.table' was built under R version 4.1.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## -- 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.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## Warning: package 'dplyr' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::between()   masks data.table::between()
## x dplyr::filter()    masks stats::filter()
## x dplyr::first()     masks data.table::first()
## x dplyr::lag()       masks stats::lag()
## x dplyr::last()      masks data.table::last()
## x purrr::transpose() masks data.table::transpose()
library(ggplot2)
library(stats)
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.1.3
library(maps)
## Warning: package 'maps' was built under R version 4.1.3
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
library(gifski)
## Warning: package 'gifski' was built under R version 4.1.3
library(transformr)
## Warning: package 'transformr' was built under R version 4.1.3
library(sf)
## Warning: package 'sf' was built under R version 4.1.3
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
## 
## Attaching package: 'sf'
## The following object is masked from 'package:transformr':
## 
##     st_normalize
library(rnaturalearth)
## Warning: package 'rnaturalearth' was built under R version 4.1.3
library(rnaturalearthdata)
## Warning: package 'rnaturalearthdata' was built under R version 4.1.3
library(rgeos)
## Warning: package 'rgeos' was built under R version 4.1.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.1.3
## rgeos version: 0.5-9, (SVN revision 684)
##  GEOS runtime version: 3.9.1-CAPI-1.14.2 
##  Please note that rgeos will be retired by the end of 2023,
## plan transition to sf functions using GEOS at your earliest convenience.
##  GEOS using OverlayNG
##  Linking to sp version: 1.4-6 
##  Polygon checking: TRUE
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.1.3
#caragar de datos
elections <- fread("Elections.csv")
glimpse(elections)
## Rows: 9,845
## Columns: 38
## $ thomas_num     <dbl> 1, 2, 3, 4, 6, 8, 7, 9, 10, 11, 12, 14, 15, 16, 17, 18,~
## $ thomas_name    <chr> "Abdnor, James", "Abzug, Bella", "Adams, Brock", "Addab~
## $ icpsr          <int> 14000, 13001, 10700, 10500, 12000, 12001, 10501, 14001,~
## $ congress       <int> 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93,~
## $ year           <int> 1973, 1973, 1973, 1973, 1973, 1973, 1973, 1973, 1973, 1~
## $ st_name        <chr> "SD", "NY", "WA", "NY", "AR", "CA", "IL", "NC", "ND", "~
## $ cd             <int> 2, 20, 7, 7, 1, 35, 16, 4, 1, 11, 7, 5, 17, 9, 1, 21, 1~
## $ dem            <int> 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1~
## $ elected        <int> 1972, 1970, 1964, 1960, 1968, 1968, 1960, 1972, 1963, 1~
## $ female         <int> 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ votepct        <int> 55, 56, 85, 75, 100, 75, 72, 50, 73, 53, 82, 62, 57, 69~
## $ dwnom1         <dbl> 0.228, -0.687, -0.361, -0.400, -0.281, -0.293, 0.102, -~
## $ deleg_size     <int> 6, 39, 7, 39, 4, 43, 24, 11, 1, 24, 24, 5, 23, 23, 9, 3~
## $ speaker        <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ subchr         <int> 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1~
## $ afam           <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ latino         <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0~
## $ votepct_sq     <int> 3025, 3136, 7225, 5625, 10000, 5625, 5184, 2500, 5329, ~
## $ power          <int> 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0~
## $ chair          <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ state_leg      <int> 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0~
## $ state_leg_prof <dbl> 0.104, 0.000, 0.000, 0.000, 0.000, 0.526, 0.000, 0.190,~
## $ majority       <int> 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1~
## $ maj_leader     <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ min_leader     <int> 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ meddist        <dbl> 0.275, 0.640, 0.314, 0.353, 0.234, 0.246, 0.149, 0.051,~
## $ majdist        <dbl> 0.547500014, 0.367499977, 0.041500002, 0.080500007, 0.0~
## $ all_bills      <int> 22, 136, 37, 38, 53, 87, 55, 17, 23, 53, 35, 11, 34, 28~
## $ all_aic        <int> 0, 1, 2, 0, 3, 0, 0, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 2, 2~
## $ all_abc        <int> 0, 1, 2, 0, 3, 0, 0, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 2, 2~
## $ all_pass       <int> 0, 1, 2, 0, 2, 0, 0, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 1, 2~
## $ all_law        <int> 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1~
## $ les            <dbl> 0.10957027, 0.76243109, 1.23647845, 0.15505226, 1.87505~
## $ seniority      <int> 1, 2, 5, 7, 3, 3, 7, 1, 6, 5, 2, 1, 7, 10, 2, 2, 1, 2, ~
## $ benchmark      <dbl> 0.2713412, 0.5108124, 1.5303414, 1.5844928, 1.4761900, ~
## $ expectation    <int> 1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 2, 2, 1, 3, 2, 1, 1, 3, 2~
## $ TotalInParty   <int> 197, 248, 248, 248, 248, 248, 197, 248, 197, 248, 197, ~
## $ RankInParty    <int> 132, 119, 88, 197, 64, 147, 67, 218, 135, 129, 103, 126~
Congreso <- fread("Congreso.csv")

# primer punto
elections %>%
  
  filter(congress==115) %>% 
  mutate(Gender=ifelse(female==1,"Female","Male"),
         Mayoria = ifelse(majority==1,"Majority","Minority")) %>% 
  ggplot(aes(Mayoria,les))+
  geom_boxplot()+
  labs(x="Majority or Minority",y="Lesgislative Effectiveness",
       title = "LES in the 115th congress")

# segundo punto
Estudiantes<-c("carol","Davis","Diana")
Semestre<-c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6)

Grado<-runif(18,min=80, max=100)
grafica<-data.frame(Estudiantes,Semestre,Grado)
ggplot(grafica,aes(Semestre,Grado,group=Estudiantes, colour=Estudiantes))+
  geom_line()+
  geom_point(size=3,shape=18,fill="white")+
  ggtitle("Student Grades by Semester")+
  theme_gray()+
  facet_wrap(vars(Estudiantes),nrow = 1)

# tercer punto 
elections %>%                        
  mutate(Genre =ifelse(female == 1, "Female", "Male")) %>% 
  ggplot(aes(x = year ,fill=Genre))+ 
  geom_bar(position = "fill")+
  scale_y_continuous(labels = scales::percent)+
  ggthemes::theme_fivethirtyeight()+
  labs(title = "Women Participation")

         ##cuarto punto
         mapa_mundo = map_data("world")
         View(mapa_mundo)
        
         suramerica<- mapa_mundo %>%
           filter(region%in%c("Argentina","Colombia", "Ecuador", "Peru","Bolivia","Brazil","Chile","Ecuador","Guyana",
                              "Paraguay","Suriname","Uruguay","Venezuela")) 
         s_america<-ne_countries(scale="medium",continent='south america',returnclass="sf")%>%
           rename(region=sovereignt)
         
         data_unificada <- suramerica %>%
           left_join(s_america, by = "region")
         ggplot(data_unificada, aes(x= long, y = lat,group=group, fill = pop_est))+
           geom_polygon(color = "black")+
           theme(axis.text.x = element_text(),
                 axis.text.y = element_text(),
                 axis.ticks = element_blank())+
           labs(x="long", y="lat")+
           scale_fill_distiller(palette=10)

         # quinto punto
         
         Category<-c("Alpha","Beta","Zeta")
         City<-c("Hong Kong","London","Nairobi")
         
         my_dat<-expand_grid(Category,City)
         set.seed(84684)
         my_dat$Value<-sample(1:10,9,replace=T)
         ani<- ggplot(my_dat, aes(x = Category, y= Value, fill= City) ) +                        
           geom_bar(width = 0.9, stat="identity", position = position_dodge())+                    
           labs(x="Category", y= "Value") +   
           labs(fill = "City")+
           transition_states(City)+
           enter_fade()+
           exit_shrink() + 
           ease_aes("sine-in-out") 
         ani