GraficaciĆ³n Biblioteca lattice

Estudio R

Octubre de 2017

Lectura de datos

datos <- read.csv(file = "DatosPecuarios.csv", header = TRUE, sep = ",",
                  dec = ",")
head(datos)
##    AƱo InventarioNacionalBovino TotalPredios SacrificioBovinos.miles.cab.
## 1 2006                 22592834       495170                         3908
## 2 2007                 22324297       490763                         3973
## 3 2008                 22622965       495613                         4248
## 4 2009                 22540251       499101                         4149
## 5 2010                 23169365       503361                         3979
## 6 2011                 23048045       494593                         4262
##   ProdCarneBovino.MilesToneladas. ProdLecheCruda.M_lts.
## 1                             840                  6157
## 2                             859                  6312
## 3                             924                  6579
## 4                             899                  6406
## 5                             864                  6363
## 6                             929                  6390
##   PrecioNovilloGordoPie..US..Kg. IPC_Total ConsumoLeche..Lt.hab.
## 1                          1.170    282.25               139.813
## 2                          1.560    298.36               143.017
## 3                          1.707    321.24               146.999
## 4                          1.474    327.70               143.720
## 5                          1.534    338.10               139.000
## 6                          1.632    350.70               139.662
##   ConsumoRes.kg.hab. ConsumoPollo..kg.hab. ConsumoCerdo.kg.hab.
## 1              18.88                  20.1                 3.71
## 2              17.81                  21.6                 4.35
## 3              17.38                  23.3                 4.30
## 4              17.67                  22.7                 4.22
## 5              18.94                  23.4                 4.77
## 6              20.01                  23.8                 5.52
##   ConsumoPescado.kg.hab. ConsumoHuevo.Un.hab.
## 1                   2.81                  202
## 2                   4.03                  188
## 3                   4.17                  198
## 4                   3.49                  215
## 5                   4.48                  214
## 6                   4.52                  234
str(datos)
## 'data.frame':    9 obs. of  14 variables:
##  $ AƱo                            : int  2006 2007 2008 2009 2010 2011 2012 2013 2014
##  $ InventarioNacionalBovino       : int  22592834 22324297 22622965 22540251 23169365 23048045 22666751 22399618 22593283
##  $ TotalPredios                   : int  495170 490763 495613 499101 503361 494593 502905 497747 497008
##  $ SacrificioBovinos.miles.cab.   : int  3908 3973 4248 4149 3979 4262 4479 4399 4332
##  $ ProdCarneBovino.MilesToneladas.: int  840 859 924 899 864 929 973 955 941
##  $ ProdLecheCruda.M_lts.          : int  6157 6312 6579 6406 6363 6390 6518 6617 6717
##  $ PrecioNovilloGordoPie..US..Kg. : num  1.17 1.56 1.71 1.47 1.53 ...
##  $ IPC_Total                      : num  282 298 321 328 338 ...
##  $ ConsumoLeche..Lt.hab.          : num  140 143 147 144 139 ...
##  $ ConsumoRes.kg.hab.             : num  18.9 17.8 17.4 17.7 18.9 ...
##  $ ConsumoPollo..kg.hab.          : num  20.1 21.6 23.3 22.7 23.4 23.8 23.7 27.1 29.5
##  $ ConsumoCerdo.kg.hab.           : num  3.71 4.35 4.3 4.22 4.77 5.52 6.01 6.67 7.18
##  $ ConsumoPescado.kg.hab.         : num  2.81 4.03 4.17 3.49 4.48 4.52 5.4 6.1 6.1
##  $ ConsumoHuevo.Un.hab.           : int  202 188 198 215 214 234 228 236 242

Formato ancho a largo

datosLargo <- datos %>%
            gather(key = Item,
                   value = Valor,
                   -c(AƱo))
head(datosLargo, n = 5)
##    AƱo                     Item    Valor
## 1 2006 InventarioNacionalBovino 22592834
## 2 2007 InventarioNacionalBovino 22324297
## 3 2008 InventarioNacionalBovino 22622965
## 4 2009 InventarioNacionalBovino 22540251
## 5 2010 InventarioNacionalBovino 23169365
str(datosLargo)
## 'data.frame':    117 obs. of  3 variables:
##  $ AƱo  : int  2006 2007 2008 2009 2010 2011 2012 2013 2014 2006 ...
##  $ Item : chr  "InventarioNacionalBovino" "InventarioNacionalBovino" "InventarioNacionalBovino" "InventarioNacionalBovino" ...
##  $ Valor: num  22592834 22324297 22622965 22540251 23169365 ...

GraficaciĆ³n con lattice - Histograma

#Instalar la biblioteca lattice
library(lattice)
histogram(datos$InventarioNacionalBovino/1e6,
          col = 2, lwd = 4,
          xlab = "Inventario nacional bovino (Millones)",
          ylab = "Porcentaje",
          type = "percent",
          breaks = 50)

Filtrando consumos para graficar histogramas:

consumos <- c("ConsumoLeche..Lt.hab.", "ConsumoRes.kg.hab.",
              "ConsumoPollo..kg.hab.", "ConsumoCerdo.kg.hab.",
              "ConsumoPescado.kg.hab.", "ConsumoHuevo.Un.hab.")

datConsumo <- datos %>%
                select(AƱo, consumos) %>%
                gather(key = Item, value = Consumo, -c(AƱo)) %>%
                arrange(AƱo)
head(datConsumo, n = 5)
##    AƱo                   Item Consumo
## 1 2006  ConsumoLeche..Lt.hab. 139.813
## 2 2006     ConsumoRes.kg.hab.  18.880
## 3 2006  ConsumoPollo..kg.hab.  20.100
## 4 2006   ConsumoCerdo.kg.hab.   3.710
## 5 2006 ConsumoPescado.kg.hab.   2.810
str(datConsumo)
## 'data.frame':    54 obs. of  3 variables:
##  $ AƱo    : int  2006 2006 2006 2006 2006 2006 2007 2007 2007 2007 ...
##  $ Item   : chr  "ConsumoLeche..Lt.hab." "ConsumoRes.kg.hab." "ConsumoPollo..kg.hab." "ConsumoCerdo.kg.hab." ...
##  $ Consumo: num  139.81 18.88 20.1 3.71 2.81 ...

Histograma por item:

histogram(~datConsumo$Consumo | factor(datConsumo$Item),
          col = 2, lwd = 4,
          xlab = "Consumo de proteĆ­na animal",
          ylab = "Porcentaje",
          type = "percent")

Histograma por aƱo:

histogram(~datConsumo$Consumo | factor(datConsumo$AƱo),
          col = 3, lwd = 4,
          xlab = "Consumo de proteĆ­na animal",
          ylab = "Porcentaje",
          type = "percent")

Densidad

densityplot(~datConsumo$Consumo | factor(datConsumo$AƱo),
            groups = datConsumo$AƱo,
            plot.points = TRUE,
            auto.key = FALSE,
            xlab = "Consumo de proteĆ­na animal",
            ylab = "Densidad")

Densidad por aƱo:

densityplot(~datConsumo$Consumo | factor(datConsumo$Item),
            groups = datConsumo$Item,
            plot.points = TRUE,
            auto.key = FALSE,
            xlab = "Consumo de proteĆ­na animal",
            ylab = "Densidad")

Funciones importantes en lattice

Diagrama de cajas (Boxplot)

bwplot(factor(datConsumo$AƱo) ~ datConsumo$Consumo,
       col = 2,
       xlab = "Consumo de proteina animal",
       ylab = "AƱo")

bwplot(factor(datConsumo$Item) ~ datConsumo$Consumo,
       col = 2,
       xlab = "Consumo de proteina animal",
       ylab = "item de consumo")

Diagrama de barras

barchart(factor(datConsumo$AƱo) ~ datConsumo$Consumo,
         col = "blue",
         xlab = "Consumo de proteĆ­na animal",
         ylab = "AƱo")

Consumo por item y aƱo:

barchart(factor(datConsumo$AƱo) ~ datConsumo$Consumo
         | factor(datConsumo$Item),
         col = "forestgreen",
         xlab = "Consumo de proteĆ­na animal",
         ylab = "AƱo")

Consumo por aƱo e item:

barchart(factor(datConsumo$Item) ~ datConsumo$Consumo
         | factor(datConsumo$AƱo),
         col = "orangered",
         xlab = "Consumo de proteĆ­na animal",
         ylab = "Item de consumo")

Otra forma de representar las barras:

dotplot(factor(datConsumo$Item) ~ datConsumo$Consumo
         | factor(datConsumo$AƱo),
         col = "darkred",
         xlab = "Consumo de proteĆ­na animal",
         ylab = "Item de consumo")

dotplot(factor(datConsumo$Item) ~ datConsumo$Consumo, groups = factor(datConsumo$AƱo), type = "o",
auto.key = list(space = "right", points = TRUE, lines = TRUE),
col = rainbow(9))

Diagrama de dispersiĆ³n

IPC vs Precio novillo gordo:

xyplot(datos$PrecioNovilloGordoPie..US..Kg. ~ datos$IPC_Total,
       col = 2,
       xlab = "IPC total",
       ylab = "Precio de novillo gordo (US$/Kg)")

IPC vs Consumo de carne de cerdo:

xyplot(datos$ConsumoCerdo.kg.hab. ~ datos$IPC_Total,
       col = "forestgreen",
       xlab = "IPC total",
       ylab = "Consumo de cerdo (Kg/habitante)",
       cex = 2,
       pch = 10,
       type = c("p", "smooth"))

xyplot(datos$ConsumoRes.kg.hab. ~ datos$IPC_Total,
       col = "orangered",
       xlab = "IPC total",
       ylab = "Consumo de res (Kg/habitante)",
       cex = 2,
       pch = 21,
       type = c("p", "smooth"))