#Abrir librerias
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## āœ” dplyr     1.1.0     āœ” readr     2.1.4
## āœ” forcats   1.0.0     āœ” stringr   1.5.0
## āœ” ggplot2   3.4.1     āœ” tibble    3.1.8
## āœ” lubridate 1.9.2     āœ” tidyr     1.3.0
## āœ” purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## āœ– dplyr::filter() masks stats::filter()
## āœ– dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(dplyr)
library(ggplot2)
library(nycflights13)

data(flights)
view(flights)

#1.  Elabora una grÔfica de barras que refleje el número de vuelos por aerolínea que han salido de NYC en el año 2013.
bar_graph <- flights %>% select(carrier,origin, year) %>% filter(year == 2013,) %>% group_by(carrier) %>% summarise(Total=n())

ggplot(bar_graph, aes(x = carrier, y=Total) ) +    
  geom_bar(width = 0.8, stat='identity', fill='pink',             
           position = position_dodge()                
           )+  
  
  ggtitle("Vuelos por aerolinea NY, 2013")+
  
    labs(x="Aerolineas", y= "# de vuelos") +   
  labs(fill = "")+  
  
    geom_text(aes(label=Total), vjust=1.6, color="black",    
              position = position_dodge(0.9),  size=2.0
            ) +                                            
  
  theme_bw(base_size = 10)

#2.  Obtén una tabla que indique el número de vuelos por aerolínea.


#3. Elabora una grÔfica de barras que refleje el número de vuelos por aerolínea que han salido de NYC en el año 2013 para cada uno de los tres aeropuertos. ( John F. Kennedy, LaGuardia and Newark Liberty)

#JFK
bar_graph_JFK <- flights %>% select(carrier,origin, year) %>% filter(year == 2013,) %>% filter(origin == 'JFK')%>% group_by(carrier) %>% summarise(Total=n())

ggplot(bar_graph_JFK, aes(x = carrier, y=Total) ) +    
  geom_bar(width = 0.8, stat='identity', fill='skyblue',             
           position = position_dodge()                
           )+  
  
  ggtitle("Vuelos por aerolinea NY, 2013")+
  
    labs(x="Aerolineas", y= "# de vuelos") +   
  labs(fill = "")+  
  
    geom_text(aes(label=Total), vjust=1.6, color="black",    
              position = position_dodge(0.9),  size=2.0
            ) +                                            
  
  theme_bw(base_size = 10)

#LGA
bar_graph_LGA <- flights %>% select(carrier,origin, year) %>% filter(year == 2013,) %>% filter(origin == 'LGA')%>% group_by(carrier) %>% summarise(Total=n())

ggplot(bar_graph_LGA, aes(x = carrier, y=Total) ) +    
  geom_bar(width = 0.8, stat='identity', fill='orange',             
           position = position_dodge()                
           )+  
  
  ggtitle("Vuelos por aerolinea NY, 2013")+
  
    labs(x="Aerolineas", y= "# de vuelos") +   
  labs(fill = "")+  
  
    geom_text(aes(label=Total), vjust=1.6, color="black",    
              position = position_dodge(0.9),  size=2.0
            ) +                                            
  
  theme_bw(base_size = 10)

#EWR
bar_graph_EWR <- flights %>% select(carrier,origin, year) %>% filter(year == 2013,) %>% filter(origin == 'EWR')%>% group_by(carrier) %>% summarise(Total=n())

ggplot(bar_graph_EWR, aes(x = carrier, y=Total) ) +    
  geom_bar(width = 0.8, stat='identity', fill='lightgreen',             
           position = position_dodge()                
           )+  
  
  ggtitle("Vuelos por aerolinea NY, 2013")+
  
    labs(x="Aerolineas", y= "# de vuelos") +   
  labs(fill = "")+  
  
    geom_text(aes(label=Total), vjust=1.6, color="black",    
              position = position_dodge(0.9),  size=2.0
            ) +                                            
  
  theme_bw(base_size = 10)