Manejo Habil de Datos
y Ciencia Reproducible
Taller Dia 1

2018-03-26

Preambulo

Quien soy?

Gabriel Muñoz

  • Biólogo
  • Ecólogo Computacional

Consultor en Datos Biodiversidad y Geoespaciales

Coordinador General

NASUA

Charla introductoria

https://github.com/fgabriel1891/ManejoHabilDatosMediaLab

Material para este taller

https://github.com/fgabriel1891/ManejoHabilDatosWorkshop

Como organizar datos en las hojas de cálculo?

Una Variable, Una Columna!!!!

Control de Calidad

Como asegurar la calidad de nuestros datos?

  • Errores de tipeo

  • Discrepancias

  • Fueras de lugar

  • Errores de formato

  • Irregularidades

  • Datos faltantes

Google Open Refine

http://openrefine.org/download.html

tidyverse

Exportar datos

Como exportar hojas de cálculo?

  • Registros similares en un dataset

  • Seguir standards internacionales en unidades y formatos (e.g. yyyy-mm-dd)

  • Se consistente (e.g. Mts, (m), metros)

  • Preserva en formatos estables (.csv, .txt, .tsv)

Análisis y visualización de datos en R

Intro a R

R version 3.4.4 (Someone to Lean On)

RStudio Desktop 1.1.442

Que es R?

  • Software gratis y abierto para el computación estadística y graficos.

  • Lenguaje de programación

Crear un proyecto

Un proyecto, una carpeta!

Empezar un script

Buenas prácticas de programación

  • Estandarización

  • nombrar con sentido
  • xx11223
  • datosMediaLab

  • nombrar con forma
  • datosMediaLab_ene
  • datosMediaLab_feb

  • Indentación

  • Simple

Vectores

# Numericos 
num = c(2,4,5,6,7,5)
num
## [1] 2 4 5 6 7 5
# Logicos 

tOf = c(TRUE, FALSE, T,T, F)
tOf
## [1]  TRUE FALSE  TRUE  TRUE FALSE
# texto (strings)

text = c("a","b","c","d")
text 
## [1] "a" "b" "c" "d"
# Examinar: 
length(text)
## [1] 4
# Combinar 
comb = c(num ,tOf, text)
comb # Numeros a texto 
##  [1] "2"     "4"     "5"     "6"     "7"     "5"     "TRUE"  "FALSE"
##  [9] "TRUE"  "TRUE"  "FALSE" "a"     "b"     "c"     "d"

Vectores

Variables

# "<-" asigna un valor a la variable 
x <- 3 

# "=" tambien asigna un valor a la variable 
y = 4

# Operaciones aritmeticas
z = x + y
z
## [1] 7
w = ((x^2) + (y^2))/z 
w
## [1] 3.571429

Vectores

Indices []

text
## [1] "a" "b" "c" "d"
text[3]
## [1] "c"
text[-3]
## [1] "a" "b" "d"
text[c(1,3)]
## [1] "a" "c"
text[c(4,3,2,1)]
## [1] "d" "c" "b" "a"
text[c(1:3)]
## [1] "a" "b" "c"
tOf
## [1]  TRUE FALSE  TRUE  TRUE FALSE
text[tOf]
## [1] "a" "c" "d"

Vectores

Nombres

nombres = c("Michelle", "Diana", "Gabriel", "Horacio","Pablo")

names(num)
## NULL
names(num) = nombres
num
## Michelle    Diana  Gabriel  Horacio    Pablo     <NA> 
##        2        4        5        6        7        5
names(num) = c(nombres,"Sara")
num
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##        2        4        5        6        7        5
num["Diana"]
## Diana 
##     4
num[c("Diana","Gabriel")]
##   Diana Gabriel 
##       4       5

Mas tipos de objetos en R

Matrix

matrix1 = matrix(c(1,2,3,4, 5,6,7,8), nrow = 2,ncol = 4)
matrix1
##      [,1] [,2] [,3] [,4]
## [1,]    1    3    5    7
## [2,]    2    4    6    8
str(matrix1)
##  num [1:2, 1:4] 1 2 3 4 5 6 7 8
matrix2 = matrix(c(11,12,13,14,15,16,17,18), 2 , 4)
matrix2
##      [,1] [,2] [,3] [,4]
## [1,]   11   13   15   17
## [2,]   12   14   16   18

Mas tipos de objetos en R

Unir matrices

# por columna 
cbind(matrix1, matrix2)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    3    5    7   11   13   15   17
## [2,]    2    4    6    8   12   14   16   18
#por fila
rbind(matrix1, matrix2)
##      [,1] [,2] [,3] [,4]
## [1,]    1    3    5    7
## [2,]    2    4    6    8
## [3,]   11   13   15   17
## [4,]   12   14   16   18

Mas tipos de objetos en R

Lista

lista = list(text, num, tOf)
lista
## [[1]]
## [1] "a" "b" "c" "d"
## 
## [[2]]
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##        2        4        5        6        7        5 
## 
## [[3]]
## [1]  TRUE FALSE  TRUE  TRUE FALSE

Mas tipos de objetos en R

Lista

lista[[2]]
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##        2        4        5        6        7        5
lista[[2]][3]
## Gabriel 
##       5
lista[[2]][3] = 4
lista[[2]]
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##        2        4        4        6        7        5

Mas tipos de objetos en R

Lista con nombres (named list)

lista = list("letras" = text,  "numeros"= num, "opLogicos" = tOf)
lista
## $letras
## [1] "a" "b" "c" "d"
## 
## $numeros
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##        2        4        5        6        7        5 
## 
## $opLogicos
## [1]  TRUE FALSE  TRUE  TRUE FALSE
lista$letras
## [1] "a" "b" "c" "d"

Mas tipos de objetos en R

Lista con nombres (named list)

lista$letras[2]
## [1] "b"
lista[c("numeros", "opLogicos")]
## $numeros
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##        2        4        5        6        7        5 
## 
## $opLogicos
## [1]  TRUE FALSE  TRUE  TRUE FALSE

Mas tipos de objetos en R

Data Frame

# Lista de vectores del mismo tamaño 
data.frame(text, num, tOf)
## Error in data.frame(text, num, tOf): arguments imply differing number of rows: 4, 6, 5
# install.packages("datasets")
library(datasets)
str(datasets::beaver1)
## 'data.frame':    114 obs. of  4 variables:
##  $ day  : num  346 346 346 346 346 346 346 346 346 346 ...
##  $ time : num  840 850 900 910 920 930 940 950 1000 1010 ...
##  $ temp : num  36.3 36.3 36.4 36.4 36.5 ...
##  $ activ: num  0 0 0 0 0 0 0 0 0 0 ...
data2play = datasets::beaver1

data2play[1]
##     day
## 1   346
## 2   346
## 3   346
## 4   346
## 5   346
## 6   346
## 7   346
## 8   346
## 9   346
## 10  346
## 11  346
## 12  346
## 13  346
## 14  346
## 15  346
## 16  346
## 17  346
## 18  346
## 19  346
## 20  346
## 21  346
## 22  346
## 23  346
## 24  346
## 25  346
## 26  346
## 27  346
## 28  346
## 29  346
## 30  346
## 31  346
## 32  346
## 33  346
## 34  346
## 35  346
## 36  346
## 37  346
## 38  346
## 39  346
## 40  346
## 41  346
## 42  346
## 43  346
## 44  346
## 45  346
## 46  346
## 47  346
## 48  346
## 49  346
## 50  346
## 51  346
## 52  346
## 53  346
## 54  346
## 55  346
## 56  346
## 57  346
## 58  346
## 59  346
## 60  346
## 61  346
## 62  346
## 63  346
## 64  346
## 65  346
## 66  346
## 67  346
## 68  346
## 69  346
## 70  346
## 71  346
## 72  346
## 73  346
## 74  346
## 75  346
## 76  346
## 77  346
## 78  346
## 79  346
## 80  346
## 81  346
## 82  346
## 83  346
## 84  346
## 85  346
## 86  346
## 87  346
## 88  346
## 89  346
## 90  346
## 91  346
## 92  347
## 93  347
## 94  347
## 95  347
## 96  347
## 97  347
## 98  347
## 99  347
## 100 347
## 101 347
## 102 347
## 103 347
## 104 347
## 105 347
## 106 347
## 107 347
## 108 347
## 109 347
## 110 347
## 111 347
## 112 347
## 113 347
## 114 347
data2play[,1]
##   [1] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [18] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [35] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [52] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [69] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [86] 346 346 346 346 346 346 347 347 347 347 347 347 347 347 347 347 347
## [103] 347 347 347 347 347 347 347 347 347 347 347 347
data2play[1,]
##   day time  temp activ
## 1 346  840 36.33     0
data2play$day
##   [1] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [18] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [35] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [52] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [69] 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346 346
##  [86] 346 346 346 346 346 346 347 347 347 347 347 347 347 347 347 347 347
## [103] 347 347 347 347 347 347 347 347 347 347 347 347
data2play["day"]
##     day
## 1   346
## 2   346
## 3   346
## 4   346
## 5   346
## 6   346
## 7   346
## 8   346
## 9   346
## 10  346
## 11  346
## 12  346
## 13  346
## 14  346
## 15  346
## 16  346
## 17  346
## 18  346
## 19  346
## 20  346
## 21  346
## 22  346
## 23  346
## 24  346
## 25  346
## 26  346
## 27  346
## 28  346
## 29  346
## 30  346
## 31  346
## 32  346
## 33  346
## 34  346
## 35  346
## 36  346
## 37  346
## 38  346
## 39  346
## 40  346
## 41  346
## 42  346
## 43  346
## 44  346
## 45  346
## 46  346
## 47  346
## 48  346
## 49  346
## 50  346
## 51  346
## 52  346
## 53  346
## 54  346
## 55  346
## 56  346
## 57  346
## 58  346
## 59  346
## 60  346
## 61  346
## 62  346
## 63  346
## 64  346
## 65  346
## 66  346
## 67  346
## 68  346
## 69  346
## 70  346
## 71  346
## 72  346
## 73  346
## 74  346
## 75  346
## 76  346
## 77  346
## 78  346
## 79  346
## 80  346
## 81  346
## 82  346
## 83  346
## 84  346
## 85  346
## 86  346
## 87  346
## 88  346
## 89  346
## 90  346
## 91  346
## 92  347
## 93  347
## 94  347
## 95  347
## 96  347
## 97  347
## 98  347
## 99  347
## 100 347
## 101 347
## 102 347
## 103 347
## 104 347
## 105 347
## 106 347
## 107 347
## 108 347
## 109 347
## 110 347
## 111 347
## 112 347
## 113 347
## 114 347

Importar datos a R

# subir csv file to R. 

dataset = read.csv(file = "data/dataset.csv", header = T)

# examinar dataset  
head(dataset)
tail(dataset)

str(dataset)

Objectos

# Objeto 
num
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##        2        4        5        6        7        5

Funciones

# Funciones 
# functionNombre = function(argumentos) { 
  # codigo operacion , 
  # resultado }


sueldoMes = function(lista, sueldo){
  saldoFinal = lista + sueldo
  return(saldoFinal)
}

sueldoMes(num, 0.14)
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##     2.14     4.14     5.14     6.14     7.14     5.14
mes1 = c(0.15,0.13,0.16,0.14,0.3,0.23)

sueldoMes(num, mes1)
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##     2.15     4.13     5.16     6.14     7.30     5.23

loops

mes2 = c()

 for ( i in 1:length(num)){  
  mes2[i] = num[i] + 0.43
  names(mes2)[i]  = names(num)[i]
}

mes2
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##     2.43     4.43     5.43     6.43     7.43     5.43

sapply / lapply

# sapply
sapply(num, function(x) x + 0.13)
## Michelle    Diana  Gabriel  Horacio    Pablo     Sara 
##     2.13     4.13     5.13     6.13     7.13     5.13
# lapply
lapply(num, function(x) x + 0.13)
## $Michelle
## [1] 2.13
## 
## $Diana
## [1] 4.13
## 
## $Gabriel
## [1] 5.13
## 
## $Horacio
## [1] 6.13
## 
## $Pablo
## [1] 7.13
## 
## $Sara
## [1] 5.13

Exploración de datos

summary(), dim()

summary(data2play)
##       day             time             temp           activ        
##  Min.   :346.0   Min.   :   0.0   Min.   :36.33   Min.   :0.00000  
##  1st Qu.:346.0   1st Qu.: 932.5   1st Qu.:36.76   1st Qu.:0.00000  
##  Median :346.0   Median :1415.0   Median :36.87   Median :0.00000  
##  Mean   :346.2   Mean   :1312.0   Mean   :36.86   Mean   :0.05263  
##  3rd Qu.:346.0   3rd Qu.:1887.5   3rd Qu.:36.96   3rd Qu.:0.00000  
##  Max.   :347.0   Max.   :2350.0   Max.   :37.53   Max.   :1.00000
dim(data2play)
## [1] 114   4

Exploración de datos

aggregate()

aggregate(time~day, mean, data =data2play)
##   day      time
## 1 346 1601.3187
## 2 347  167.3913
aggregate(temp~day, max, data =data2play)
##   day  temp
## 1 346 37.53
## 2 347 37.15
aggregate(temp~day, min, data =data2play)
##   day  temp
## 1 346 36.33
## 2 347 36.70

Exploración de datos

melt()

meltData =reshape::melt(data2play)
## Using  as id variables
meltData
##     variable   value
## 1        day  346.00
## 2        day  346.00
## 3        day  346.00
## 4        day  346.00
## 5        day  346.00
## 6        day  346.00
## 7        day  346.00
## 8        day  346.00
## 9        day  346.00
## 10       day  346.00
## 11       day  346.00
## 12       day  346.00
## 13       day  346.00
## 14       day  346.00
## 15       day  346.00
## 16       day  346.00
## 17       day  346.00
## 18       day  346.00
## 19       day  346.00
## 20       day  346.00
## 21       day  346.00
## 22       day  346.00
## 23       day  346.00
## 24       day  346.00
## 25       day  346.00
## 26       day  346.00
## 27       day  346.00
## 28       day  346.00
## 29       day  346.00
## 30       day  346.00
## 31       day  346.00
## 32       day  346.00
## 33       day  346.00
## 34       day  346.00
## 35       day  346.00
## 36       day  346.00
## 37       day  346.00
## 38       day  346.00
## 39       day  346.00
## 40       day  346.00
## 41       day  346.00
## 42       day  346.00
## 43       day  346.00
## 44       day  346.00
## 45       day  346.00
## 46       day  346.00
## 47       day  346.00
## 48       day  346.00
## 49       day  346.00
## 50       day  346.00
## 51       day  346.00
## 52       day  346.00
## 53       day  346.00
## 54       day  346.00
## 55       day  346.00
## 56       day  346.00
## 57       day  346.00
## 58       day  346.00
## 59       day  346.00
## 60       day  346.00
## 61       day  346.00
## 62       day  346.00
## 63       day  346.00
## 64       day  346.00
## 65       day  346.00
## 66       day  346.00
## 67       day  346.00
## 68       day  346.00
## 69       day  346.00
## 70       day  346.00
## 71       day  346.00
## 72       day  346.00
## 73       day  346.00
## 74       day  346.00
## 75       day  346.00
## 76       day  346.00
## 77       day  346.00
## 78       day  346.00
## 79       day  346.00
## 80       day  346.00
## 81       day  346.00
## 82       day  346.00
## 83       day  346.00
## 84       day  346.00
## 85       day  346.00
## 86       day  346.00
## 87       day  346.00
## 88       day  346.00
## 89       day  346.00
## 90       day  346.00
## 91       day  346.00
## 92       day  347.00
## 93       day  347.00
## 94       day  347.00
## 95       day  347.00
## 96       day  347.00
## 97       day  347.00
## 98       day  347.00
## 99       day  347.00
## 100      day  347.00
## 101      day  347.00
## 102      day  347.00
## 103      day  347.00
## 104      day  347.00
## 105      day  347.00
## 106      day  347.00
## 107      day  347.00
## 108      day  347.00
## 109      day  347.00
## 110      day  347.00
## 111      day  347.00
## 112      day  347.00
## 113      day  347.00
## 114      day  347.00
## 115     time  840.00
## 116     time  850.00
## 117     time  900.00
## 118     time  910.00
## 119     time  920.00
## 120     time  930.00
## 121     time  940.00
## 122     time  950.00
## 123     time 1000.00
## 124     time 1010.00
## 125     time 1020.00
## 126     time 1030.00
## 127     time 1040.00
## 128     time 1050.00
## 129     time 1100.00
## 130     time 1110.00
## 131     time 1120.00
## 132     time 1130.00
## 133     time 1140.00
## 134     time 1150.00
## 135     time 1200.00
## 136     time 1210.00
## 137     time 1220.00
## 138     time 1230.00
## 139     time 1240.00
## 140     time 1250.00
## 141     time 1300.00
## 142     time 1310.00
## 143     time 1320.00
## 144     time 1330.00
## 145     time 1340.00
## 146     time 1350.00
## 147     time 1400.00
## 148     time 1410.00
## 149     time 1420.00
## 150     time 1430.00
## 151     time 1440.00
## 152     time 1450.00
## 153     time 1500.00
## 154     time 1510.00
## 155     time 1520.00
## 156     time 1530.00
## 157     time 1540.00
## 158     time 1550.00
## 159     time 1600.00
## 160     time 1610.00
## 161     time 1620.00
## 162     time 1630.00
## 163     time 1640.00
## 164     time 1650.00
## 165     time 1700.00
## 166     time 1710.00
## 167     time 1720.00
## 168     time 1730.00
## 169     time 1740.00
## 170     time 1750.00
## 171     time 1800.00
## 172     time 1810.00
## 173     time 1820.00
## 174     time 1830.00
## 175     time 1840.00
## 176     time 1850.00
## 177     time 1900.00
## 178     time 1910.00
## 179     time 1920.00
## 180     time 1930.00
## 181     time 1940.00
## 182     time 1950.00
## 183     time 2000.00
## 184     time 2010.00
## 185     time 2020.00
## 186     time 2030.00
## 187     time 2040.00
## 188     time 2050.00
## 189     time 2100.00
## 190     time 2110.00
## 191     time 2120.00
## 192     time 2130.00
## 193     time 2140.00
## 194     time 2150.00
## 195     time 2200.00
## 196     time 2210.00
## 197     time 2230.00
## 198     time 2240.00
## 199     time 2250.00
## 200     time 2300.00
## 201     time 2310.00
## 202     time 2320.00
## 203     time 2330.00
## 204     time 2340.00
## 205     time 2350.00
## 206     time    0.00
## 207     time   10.00
## 208     time   20.00
## 209     time   30.00
## 210     time   40.00
## 211     time   50.00
## 212     time  100.00
## 213     time  110.00
## 214     time  120.00
## 215     time  130.00
## 216     time  140.00
## 217     time  150.00
## 218     time  200.00
## 219     time  210.00
## 220     time  220.00
## 221     time  230.00
## 222     time  240.00
## 223     time  250.00
## 224     time  300.00
## 225     time  310.00
## 226     time  320.00
## 227     time  330.00
## 228     time  340.00
## 229     temp   36.33
## 230     temp   36.34
## 231     temp   36.35
## 232     temp   36.42
## 233     temp   36.55
## 234     temp   36.69
## 235     temp   36.71
## 236     temp   36.75
## 237     temp   36.81
## 238     temp   36.88
## 239     temp   36.89
## 240     temp   36.91
## 241     temp   36.85
## 242     temp   36.89
## 243     temp   36.89
## 244     temp   36.67
## 245     temp   36.50
## 246     temp   36.74
## 247     temp   36.77
## 248     temp   36.76
## 249     temp   36.78
## 250     temp   36.82
## 251     temp   36.89
## 252     temp   36.99
## 253     temp   36.92
## 254     temp   36.99
## 255     temp   36.89
## 256     temp   36.94
## 257     temp   36.92
## 258     temp   36.97
## 259     temp   36.91
## 260     temp   36.79
## 261     temp   36.77
## 262     temp   36.69
## 263     temp   36.62
## 264     temp   36.54
## 265     temp   36.55
## 266     temp   36.67
## 267     temp   36.69
## 268     temp   36.62
## 269     temp   36.64
## 270     temp   36.59
## 271     temp   36.65
## 272     temp   36.75
## 273     temp   36.80
## 274     temp   36.81
## 275     temp   36.87
## 276     temp   36.87
## 277     temp   36.89
## 278     temp   36.94
## 279     temp   36.98
## 280     temp   36.95
## 281     temp   37.00
## 282     temp   37.07
## 283     temp   37.05
## 284     temp   37.00
## 285     temp   36.95
## 286     temp   37.00
## 287     temp   36.94
## 288     temp   36.88
## 289     temp   36.93
## 290     temp   36.98
## 291     temp   36.97
## 292     temp   36.85
## 293     temp   36.92
## 294     temp   36.99
## 295     temp   37.01
## 296     temp   37.10
## 297     temp   37.09
## 298     temp   37.02
## 299     temp   36.96
## 300     temp   36.84
## 301     temp   36.87
## 302     temp   36.85
## 303     temp   36.85
## 304     temp   36.87
## 305     temp   36.89
## 306     temp   36.86
## 307     temp   36.91
## 308     temp   37.53
## 309     temp   37.23
## 310     temp   37.20
## 311     temp   37.25
## 312     temp   37.20
## 313     temp   37.21
## 314     temp   37.24
## 315     temp   37.10
## 316     temp   37.20
## 317     temp   37.18
## 318     temp   36.93
## 319     temp   36.83
## 320     temp   36.93
## 321     temp   36.83
## 322     temp   36.80
## 323     temp   36.75
## 324     temp   36.71
## 325     temp   36.73
## 326     temp   36.75
## 327     temp   36.72
## 328     temp   36.76
## 329     temp   36.70
## 330     temp   36.82
## 331     temp   36.88
## 332     temp   36.94
## 333     temp   36.79
## 334     temp   36.78
## 335     temp   36.80
## 336     temp   36.82
## 337     temp   36.84
## 338     temp   36.86
## 339     temp   36.88
## 340     temp   36.93
## 341     temp   36.97
## 342     temp   37.15
## 343    activ    0.00
## 344    activ    0.00
## 345    activ    0.00
## 346    activ    0.00
## 347    activ    0.00
## 348    activ    0.00
## 349    activ    0.00
## 350    activ    0.00
## 351    activ    0.00
## 352    activ    0.00
## 353    activ    0.00
## 354    activ    0.00
## 355    activ    0.00
## 356    activ    0.00
## 357    activ    0.00
## 358    activ    0.00
## 359    activ    0.00
## 360    activ    0.00
## 361    activ    0.00
## 362    activ    0.00
## 363    activ    0.00
## 364    activ    0.00
## 365    activ    0.00
## 366    activ    0.00
## 367    activ    0.00
## 368    activ    0.00
## 369    activ    0.00
## 370    activ    0.00
## 371    activ    0.00
## 372    activ    0.00
## 373    activ    0.00
## 374    activ    0.00
## 375    activ    0.00
## 376    activ    0.00
## 377    activ    0.00
## 378    activ    0.00
## 379    activ    0.00
## 380    activ    0.00
## 381    activ    0.00
## 382    activ    0.00
## 383    activ    0.00
## 384    activ    0.00
## 385    activ    0.00
## 386    activ    0.00
## 387    activ    0.00
## 388    activ    0.00
## 389    activ    0.00
## 390    activ    0.00
## 391    activ    0.00
## 392    activ    0.00
## 393    activ    0.00
## 394    activ    0.00
## 395    activ    0.00
## 396    activ    1.00
## 397    activ    0.00
## 398    activ    0.00
## 399    activ    0.00
## 400    activ    0.00
## 401    activ    0.00
## 402    activ    0.00
## 403    activ    0.00
## 404    activ    0.00
## 405    activ    0.00
## 406    activ    0.00
## 407    activ    0.00
## 408    activ    0.00
## 409    activ    0.00
## 410    activ    1.00
## 411    activ    0.00
## 412    activ    0.00
## 413    activ    0.00
## 414    activ    0.00
## 415    activ    0.00
## 416    activ    0.00
## 417    activ    0.00
## 418    activ    0.00
## 419    activ    0.00
## 420    activ    0.00
## 421    activ    0.00
## 422    activ    1.00
## 423    activ    0.00
## 424    activ    0.00
## 425    activ    1.00
## 426    activ    0.00
## 427    activ    0.00
## 428    activ    1.00
## 429    activ    0.00
## 430    activ    0.00
## 431    activ    0.00
## 432    activ    0.00
## 433    activ    0.00
## 434    activ    0.00
## 435    activ    0.00
## 436    activ    0.00
## 437    activ    0.00
## 438    activ    0.00
## 439    activ    0.00
## 440    activ    0.00
## 441    activ    0.00
## 442    activ    0.00
## 443    activ    0.00
## 444    activ    0.00
## 445    activ    0.00
## 446    activ    0.00
## 447    activ    0.00
## 448    activ    0.00
## 449    activ    0.00
## 450    activ    0.00
## 451    activ    0.00
## 452    activ    0.00
## 453    activ    0.00
## 454    activ    0.00
## 455    activ    0.00
## 456    activ    1.00
# factores
unique(meltData$variable)
## [1] day   time  temp  activ
## Levels: day time temp activ

Visualización de datos en R

par(mfrow = c(1,2))
plot(activ~temp, data = data2play)
plot(data2play$activ~data2play$time)

Visualización de datos en R

Plots base

Histograms

hist(data2play$temp, breaks = 10)

Plots base

Boxplot

data2play2 = datasets::beaver2

boxplot(data2play2$temp, data2play$temp, 
        xlab  = "Castores", ylab = "T (ºc)",
        main = "Diferencia de temperatura entre especies de castor")

legend("bottomleft", " T = ªC")

Plots base

Boxplot

Plots base

Scatterplot

plot(data2play$temp~data2play$time,
     xlab = "Hora del día",
     ylab = "Temperatura", 
     ylim = c(35,39))
points(data2play2$temp~data2play2$time,
       pch = 16, 
       col = "red")
abline(h = mean(data2play$temp))
abline(h = mean(data2play2$temp), col = "red")
legend("bottomleft", c("Species1", "Species2"),
       pch =  c(1,16), col = c("black", "red"), bty =  "n")

Plots base

Scatterplot

Colores

Colores

par(las = 1)
col = c("#8da54f", "#bb5542")
plot(data2play$temp~data2play$time,
     xlab = "Hora del día",
     ylab = "Temperatura", 
     ylim = c(35,39),
     col = col[1],
     pch = 16)
points(data2play2$temp~data2play2$time,
       pch = 16, 
       col = col[2])
abline(h = mean(data2play$temp))
abline(h = mean(data2play2$temp), col = "red")
legend("bottomleft", c("Species1", "Species2"),
       pch =  c(16,16), col = col , bty =  "n")
legend("bottomright", c("x_Species1", "x_Species2"),lty = 1,
       col = col , bty =  "n")

Colores

Plots base

  • ?plot
  • ?legend
  • help(abline)
  • ?par

Layout

layout(matrix(c(1,2,3,3),2, 2, byrow  = T))
par(mar = c(4,4,2,2), las = 1)
boxplot(data2play$temp, data2play2$temp, 
        xlab = "Castores ", 
        ylab = "temperatura",
        col = col )

hist(data2play2$day, xlim = c(300,360),
     xlab = "dia")
hist(data2play$day, add = T)
plot(data2play$temp~data2play$time,
     xlab = "Hora del día",
     ylab = "Temperatura", 
     ylim = c(35,39),
     col = col[1],
     pch = 16)
points(data2play2$temp~data2play2$time,
       pch = 16, 
       col = col[2])
abline(h = mean(data2play$temp))
abline(h = mean(data2play2$temp), col = "red")
legend("bottomleft", c("Species1", "Species2"),
       pch =  c(16,16), col = col , bty =  "n")
legend("bottomright", c("x_Species1", "x_Species2"),lty = 1, col = col , bty =  "n")

Layout

# Add boxplots to a scatterplot
par(fig=c(0,0.8,0,0.8))
plot(data2play$temp~data2play$time,
     xlab = "Hora del día",
     ylab = "Temperatura", 
     ylim = c(35,39),
     col = col[1],
     pch = 16)
points(data2play2$temp~data2play2$time,
       pch = 16, 
       col = col[2])
abline(h = mean(data2play$temp))
abline(h = mean(data2play2$temp), col = "red")
legend("bottomleft", c("Species1", "Species2"),
       pch =  c(16,16), col = col , bty =  "n")
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(data2play$time, data2play2$time, 
        col = col ,
        yaxt = "n",
        axes = F, horizontal = T)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(data2play$temp, data2play2$temp, 
        col = col ,
        yaxt = "n",
        axes = F)

Exportar figuras

  res = 200 
  width = 1500
  height = 1200
  units = "px"
 
png(filename = "figs/figura1.png", res = res, width = width, height = height, units = units)


layout(matrix(c(1,2,3,3),2, 2, byrow  = T))
par(mar = c(4,4,2,2), las = 1)
boxplot(data2play$temp, data2play2$temp, 
        xlab = "Castores ", 
        ylab = "temperatura",
        col = col )

hist(data2play2$day, xlim = c(300,360),
     xlab = "dia")
hist(data2play$day, add = T)
plot(data2play$temp~data2play$time,
     xlab = "Hora del día",
     ylab = "Temperatura", 
     ylim = c(35,39),
     col = col[1],
     pch = 16)
points(data2play2$temp~data2play2$time,
       pch = 16, 
       col = col[2])
abline(h = mean(data2play$temp))
abline(h = mean(data2play2$temp), col = "red")
legend("bottomleft", c("Species1", "Species2"),
       pch =  c(16,16), col = col , bty =  "n")
legend("bottomright", c("x_Species1", "x_Species2"),lty = 1, col = col , bty =  "n")
dev.off()
## quartz_off_screen 
##                 2

Exportar figuras

pdf(file = "figs/figure1.pdf",paper = )
# Add boxplots to a scatterplot
par(fig=c(0,0.8,0,0.8))
plot(data2play$temp~data2play$time,
     xlab = "Hora del día",
     ylab = "Temperatura", 
     ylim = c(35,39),
     col = col[1],
     pch = 16)
points(data2play2$temp~data2play2$time,
       pch = 16, 
       col = col[2])
abline(h = mean(data2play$temp))
abline(h = mean(data2play2$temp), col = "red")
legend("bottomleft", c("Species1", "Species2"),
       pch =  c(16,16), col = col , bty =  "n")
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(data2play$time, data2play2$time, 
        col = col ,
        yaxt = "n",
        axes = F, horizontal = T)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(data2play$temp, data2play2$temp, 
        col = col ,
        yaxt = "n",
        axes = F)