11/09

DOMINGO ARNAIZ

library("bit64")
## Loading required package: bit
## Attaching package bit
## package:bit (c) 2008-2012 Jens Oehlschlaegel (GPL-2)
## creators: bit bitwhich
## coercion: as.logical as.integer as.bit as.bitwhich which
## operator: ! & | xor != ==
## querying: print length any all min max range sum summary
## bit access: length<- [ [<- [[ [[<-
## for more help type ?bit
## 
## Attaching package: 'bit'
## The following object is masked from 'package:base':
## 
##     xor
## Attaching package bit64
## package:bit64 (c) 2011-2012 Jens Oehlschlaegel
## creators: integer64 seq :
## coercion: as.integer64 as.vector as.logical as.integer as.double as.character as.bin
## logical operator: ! & | xor != == < <= >= >
## arithmetic operator: + - * / %/% %% ^
## math: sign abs sqrt log log2 log10
## math: floor ceiling trunc round
## querying: is.integer64 is.vector [is.atomic} [length] format print str
## values: is.na is.nan is.finite is.infinite
## aggregation: any all min max range sum prod
## cumulation: diff cummin cummax cumsum cumprod
## access: length<- [ [<- [[ [[<-
## combine: c rep cbind rbind as.data.frame
## WARNING don't use as subscripts
## WARNING semantics differ from integer
## for more help type ?bit64
## 
## Attaching package: 'bit64'
## The following object is masked from 'package:bit':
## 
##     still.identical
## The following objects are masked from 'package:base':
## 
##     %in%, :, is.double, match, order, rank

“ggplot2”

library(ggplot2)
library(data.table)
## 
## Attaching package: 'data.table'
## The following object is masked from 'package:bit':
## 
##     setattr
EOD<-fread("EOD_Trabajada.csv", encoding = "UTF-8")
Personas<- EOD[!duplicated(id)]

crea sistema de coordenadas

ggplot(data=EOD, aes(x=Edad))

ggplot(data=EOD, aes(x=Edad))+
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(data=EOD, aes(x=Edad))+
  geom_histogram(bins = 50)

los bins son el numero de rectangulos

plot1<-ggplot(data=Personas,aes(x=Edad,weights=Factor))+
  geom_histogram(bins = 50)
plot1

ggplot(data=EOD,aes(x=TiempoViaje,y=IngresoFinal))+ 
  geom_point()

ggplot(data=EOD,aes(x=TiempoViaje,y=IngresoFinal,weights=Factor))+ 
  geom_point()

ggplot(data=EOD,aes(x=TiempoViaje,y=IngresoFinal))+ 
  geom_jitter()

ggplot(data=EOD,aes(x=TiempoViaje,y=IngresoFinal,weights=Factor))+ 
  geom_jitter()

ggplot(data=EOD[TiempoViaje<200],aes(x=TiempoViaje,y=IngresoFinal,weights=Factor))+ 
  geom_jitter(size=1)

plot2<-ggplot(data=EOD[TiempoViaje<200],aes(x=TiempoViaje,y=IngresoFinal,weights=Factor,colour=Actividad.y))+ 
  geom_jitter(size=1.5)

plot2

ggplot(data=EOD[TiempoViaje<200],aes(x=TiempoViaje,weights=Factor,fill=Actividad.y))+ 
  geom_histogram()+ facet_wrap(~Actividad.y)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Agregar etiquetas a los ejes, títulos y subtítulos al gráfico

plot2.1<-plot2+
  labs(x="Tiempo de Viaje en Minutos",y="Ingreso Total en Pesos",title = "Mi primer plot esta epic",subtitle = "tropa de wns",caption = "Source: Datos de Encuesta origen y destino Gran Valparaíso 2014, SECTRA - Gobierno de Chile")
plot2.1

Cambiar el título de la leyenda

plot2.2<-plot2.1+                       
  scale_color_discrete(name="Actividad")

plot2.2

Gráfico de Barras

Gráfico de Barra sencillo

plot3<-ggplot(data=Personas,aes(x=ComunaR,weights=Factor))+
  geom_bar()

plot3

position cambia la posición en el gráfico de las barras por la variable dentro del fill, en este caso la actividad

ggplot(data=Personas,aes(x=ComunaR,weights=Factor,fill=Actividad.y))+
  geom_bar()

ggplot(data=Personas,aes(x=ComunaR,weights=Factor,fill=Actividad.y))+
  geom_bar(position="stack")

ggplot(data=Personas,aes(x=ComunaR,weights=Factor,fill=Actividad.y))+
  geom_bar(position="dodge")

ggplot(data=Personas,aes(x=ComunaR,weights=Factor,fill=Actividad.y))+
  geom_bar(position="fill")

plot3.1<-ggplot(data=Personas,aes(x=ComunaR,weights=Factor,fill=Actividad.y))+
  geom_bar(position="stack")

plot3.1

Agregar detalles estéticos

plot3.2<-plot3.1+
  labs(x="Comuna",y="",title = "Número de Personas",subtitle = "Por Comuna de Residencia y Actividad",caption = "Source: Datos de Encuesta origen y destino Gran Valparaíso 2014, SECTRA - Gobierno de Chile")+
  scale_fill_discrete(name="Actividad")

plot3.2

plot3.3<-plot3.2+
  scale_y_continuous(labels=function(n){format(n, scientific = FALSE)})

plot3.3

#Gráfico Box-plot

ggplot(data=EOD,aes(x=ComunaR,y=TiempoViaje))+
  geom_boxplot()

ggplot(data=EOD[TiempoViaje<200],aes(x=ComunaR,y=TiempoViaje))+
  geom_boxplot()

# Podemos darle colores a cada categoría y Leyenda

plot5<-ggplot(data=EOD,aes(x=ComunaR,y=TiempoViaje,fill=ComunaR))+
  geom_boxplot()

plot5

plot5.1<-plot5+
  ylim(0,200)

plot5.1
## Warning: Removed 62 rows containing non-finite values (stat_boxplot).

plot5.2<-plot5.1+
  labs(x="",y="Minutos de Viaje",title = "Distribución de Tiempo de Viaje EOD",subtitle = "Por Comuna de Residencia",caption = "Source: Datos de Encuesta origen y destino Gran Valparaíso 2014, SECTRA - Gobierno de Chile")+
  scale_fill_discrete(name="Comuna")

plot5.2
## Warning: Removed 62 rows containing non-finite values (stat_boxplot).

Podemos modificar cada elemento de los ejes del gráfico

plot5.3<-plot5.2+
  theme(
    plot.title = element_text(size=25, face="bold"),
    axis.title.y = element_text(size=15),
    axis.text.y = element_text(size=10,angle = 90),
    axis.title.x = element_text(size=20),
    axis.text.x = element_blank(),
    legend.title = element_text(size=20),
    legend.text = element_text(size=16))

plot5.3  
## Warning: Removed 62 rows containing non-finite values (stat_boxplot).

ggsave(device = "pdf",filename="boxplot.pdf")
## Saving 7 x 5 in image
## Warning: Removed 62 rows containing non-finite values (stat_boxplot).