unidad 5: grÁficos de puntos, lÍneas e histograms en rfran/tib/semana5.pdf · unidad 5: grÁficos...

22
UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática Master en Lógica, Computación e Inteligencia Artificial Dpto. Ciencias de la Computación e Inteligencia Artificial Francisco J. Romero Campero Universidad de Sevilla

Upload: trancong

Post on 28-Sep-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R

Técnicas Inteligentes en BioinformáticaMaster en Lógica, Computación e Inteligencia ArtificialDpto. Ciencias de la Computación e Inteligencia Artificial

Francisco J. Romero Campero

Universidad de Sevilla

Page 2: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

CONTENIDOS

1. Nubes de puntos: plot

2. Gráficos de líneas: plot

3. Histogramas: hist

4. Gráficos de sectores: pie

5. Gráficos de barras: barplot

6. Gráficos de cajas y bigotes: bloxplot

Page 3: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

CONTENIDOS

1. Nubes de puntos: plot

2. Gráficos de líneas: plot

3. Histogramas: hist

4. Gráficos de sectores: pie

5. Gráficos de barras: barplot

6. Gráficos de cajas y bigotes: bloxplot

Page 4: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

Nubes de puntos: la función plot

La función básica para realizar gráficos en R es plot. La mayoría de los argumentos de plot son

compartidos por el resto de las funciones gráficas de R.

Cuando recibe como argumentos dos vectores x e y de la misma longitud construye la nube de puntos (x[i],y[i]).

> light.intensity <- c(20, 20, 20, 20, 21, 24, 44, 60, 90, 94, 101)

> growth.rate <- c(1.73, 1.65, 2.02, 1.89, 2.61, 1.36, 2.37, 2.08, 2.69, 2.32, 3.67)

> plot(light.intensity,growth.rate)

Page 5: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática
Page 6: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

El parámetro pch que recibe como valor un entero 0:18 cambia el formato de los puntos de la nube.

> plot(light.intensity,growth.rate,pch=0)

> plot(light.intensity,growth.rate,pch=1)

> plot(light.intensity,growth.rate,pch=2)

> plot(light.intensity,growth.rate,pch=3)

Page 7: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

El parámetro col recibe como valor una cadena de caracteres que especifica el color a utilizar (blue, red, green, black, etc) en los puntos de la nube.

> plot(light.intensity,growth.rate,pch=1,col="blue")

> plot(light.intensity,growth.rate,pch=1,col="red")

> plot(light.intensity,growth.rate,pch=1,col="green")

> plot(light.intensity,growth.rate,pch=1,col="black")

Page 8: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

Los parámetros xlab/ylab reciben como valor cadenas de caracteres para etiquetar el eje x e y respectivamente.

> plot(light.intensity,growth.rate,pch=1,col="blue",

xlab="Light intensity",ylab="Growth rate")

Page 9: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

El parámetro main recibe como valor una cadena de caracteres para poner un título al gráfico.

> plot(light.intensity,growth.rate,pch=1,col="blue", xlab="Light intensity",ylab="Growth rate", main="Chlorella growth with different light intensities",ylim=c(0,5))

Page 10: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

Una vez creado un gráfico se pueden añadir más puntos si no cerramos la ventana del gráfico utilizando la función points que puede recibir los mismo argumentos que plot.

> chlamy.growth.rate <- c(2.13, 1.97, 1.82, 2.15, 2.91, 1.76, 2.17, 1.98, 2.89, 2.12, 4.15)

> points(light.intensity, chlamy.growth.rate, pch=2, col="red")

Page 11: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

Sin cerrar la ventana del gráfico podemos añadir una legenda utilizando la función legend.

Su primer argumento es una cadena de caracteres para fijar su posición: "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" y "center".

Las etiquetas para cada tipo de punto se especifica en el argumento legend que recibe como valor un vector de cadenas de caracteres.

El color y el formato de cada punto en la legenda se especifica con los parámetros pch y col.

> legend("topright",legend=c("Chlorella","Chlamydomonas"), col=c("blue","red"),pch=c(1,2))

Page 12: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

CONTENIDOS

1. Nubes de puntos: plot

2. Gráficos de líneas: plot

3. Histogramas: hist

4. Gráficos de sectores: pie

5. Gráficos de barras: barplot

6. Gráficos de cajas y bigotes: bloxplot

Page 13: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

plot también se puede utilizar para construir gráficos de líneas para ello se utiliza el parámetro type. Este puede tomar los valores p (puntos, valor por defecto), l (líneas), o (puntos y líneas overplotted), etc.

> nitrogeno1 <- c(-1.075103, 1.280028, 2.683602, 6.666022, 9.116407, 9.343273, 10.664870,10.789350, 12.978650, 14.159840, 16.503100, 17.095810, 17.329490, 18.393520,19.737110, 21.092610, 22.436250, 23.544800, 24.827950, 25.769430, 26.559980,29.064630, 29.134650, 31.338990, 31.450320, 31.669930, 32.029450, 33.159360, 39.492470, 49.286340)

> ganancia1 <- c(28.31418, 28.25424, 31.63590, 26.07938, 38.90268, 25.88322, 29.84876, 33.18449, 37.94326, 39.55208, 35.84804, 41.50167, 33.24588, 35.71353, 41.86831, 26.53296, 40.95675, 34.14026, 37.96503, 41.83494, 30.56075, 37.99969, 44.49413, 39.89654, 40.72090, 36.41778, 46.13874, 45.20830, 45.27624, 54.37569)

> plot(nitrogeno1,ganancia1,type="l")

> legend("topright",legend=c("Chlorella","Chlamydomonas"), col=c("blue","red"),pch=c(1,2))

Page 14: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

El resto de parámetros son los mismos.> plot(nitrogeno1, ganancia1, type="overplotted", pch=1, col="blue", xlab="Nitrógeno", ylab="Ganancia", main="Ganancia vs Nitrógeno", ylim=c(10,60))

Page 15: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

Sin cerrar la ventana del gráfico se pueden añadir más líneas utilizando la función lines.

> nitrogeno2 <- c(-7.2839350, -2.1964150, -0.1786368, 1.1558680, 4.8274680, 6.0928080, 8.1951320, 9.0023950, 9.9906730, 10.9303900, 16.2817600, 17.4798700, 19.2580100, 21.2041200, 23.4992600,25.5497400, 27.0016200, 28.7333300, 28.7599200, 29.1683900, 32.1669900, 33.1895300, 33.9031300, 34.2451000, 35.3139300, 35.6320800, 35.6364600, 35.8048300, 38.1215400, 49.6416800)

> ganancia2 <- c(13.07851, 11.63313, 18.63174, 13.32036, 21.40557, 20.15547, 24.05262, 28.75824, 27.43614, 27.73498, 30.67290, 32.58556, 29.88930, 35.04294, 33.52377, 40.08095, 39.43911, 33.32000, 32.39222, 38.84882, 45.73129, 40.31301, 41.37395, 36.97655, 44.09940, 47.69075, 40.03095, 44.35698, 40.50994, 55.07710)

> lines(nitrogeno2,ganancia2,type="overplotted",pch=2,col="red")

> legend("topleft",legend=c("Clase1","Clase2"), pch=c(1,2),col=c("blue","red"))

Page 16: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática
Page 17: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

CONTENIDOS

1. Nubes de puntos: plot

2. Gráficos de líneas: plot

3. Histogramas: hist

4. Gráficos de sectores: pie

5. Gráficos de barras: barplot

6. Gráficos de cajas y bigotes: bloxplot

Page 18: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

Histogramas: la función hist Un histograma es una representación gráfica de las frecuencias (absolutas o relativas) de ciertos

valores en un vector.

hist es la función que permite construir histogramas y comparte la mayoría de sus parámetros con plot.

> fecundidad <- c(12.8, 21.6, 14.8, 23.1, 34.6, 19.7, 22.6, 29.6, 16.4, 20.3, 29.3, 14.9, 27.3,22.4, 27.5, 20.3, 38.7, 26.4, 23.7, 26.1, 29.5, 38.6, 44.4, 23.2, 23.6, 38.4, 32.9, 48.5, 20.9, 11.6, 22.3, 30.2, 33.4, 26.7, 39, 12.8, 14.6, 12.2, 23.1, 29.4, 16, 20.1, 23.3, 22.9, 22.5, 15.1, 31, 16.9, 16.1, 10.8, 35.4, 27.4, 19.3, 41.8, 20.3, 37.6, 36.9,37.3, 28.2, 23.4, 33.7, 29.2, 41.7, 22.6, 40.4, 34.4, 30.4, 14.9, 51.8, 33.8, 37.9, 29.5, 42.4, 36.6, 47.4)

> hist(fecundidad)

Page 19: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

La mayoría de los parámetros de plot también se utilizan en hist

> hist(fecundidad, col="grey", xlab="Fecundidad", ylab="Número de hembras",main="Fecundidad en Drosophila melanogaster")

Page 20: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

Algunos párametros específicos

El parámetro breaks se utiliza para determinar de cajas a utilizar (número de barras) por defecto utiliza el algoritmo de sturges para determinarlo.

> par(mfrow=c(1,2))

> hist(fecundidad, breaks=7, col="grey", xlab="Fecundidad", ylab="Número de hembras",main="Fecundidad en Drosophila melanogaster")

> hist(fecundidad, breaks=seq(from=10,to=60,by=10), col="grey", xlab="Fecundidad", ylab="Número de hembras", main="Fecundidad en Drosophila melanogaster")

Page 21: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

El parámetro border cambia el color del borde de las barras.

> hist(fecundidad, breaks=seq(from=10,to=60,by=10), col="grey", border="blue", xlab="Fecundidad", ylab="Número de hembras",main="Fecundidad en Drosophila melanogaster")

Page 22: UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN Rfran/TIB/semana5.pdf · UNIDAD 5: GRÁFICOS DE PUNTOS, LÍNEAS E HISTOGRAMS EN R Técnicas Inteligentes en Bioinformática

This work is licensed under the Creative Commons Attribution-NonCommercial NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/.

Estas transparencias están basadas en el material docente desarrollado por Francisco J. Romero Campero e Ignacio Pérez Hurtado de Mendoza para la asignatura Informática Aplicada a la Bioquímica del Grado Conjunto en Bioquímica por la Universidad de Sevilla y la Universidad de Málaga (Andalucía Tech). Este trabajo está liberado bajo la licencia Creative Commons Attribution-NonCommercial NoDerivs 3.0 Unported License.