|
| 1 | +--- |
| 2 | +title: "Graficos" |
| 3 | +author: "Curso de Estadística Descriptiva" |
| 4 | +date: "22/12/2018" |
| 5 | +output: html_document |
| 6 | +--- |
| 7 | + |
| 8 | +```{r setup, include=FALSE} |
| 9 | +knitr::opts_chunk$set(echo = TRUE) |
| 10 | +``` |
| 11 | + |
| 12 | +## Gráficos con la función `plot` |
| 13 | + |
| 14 | +```{r primer_plot, fig.cap="Gráfico básico explicando el uso del plot", fig.align='center'} |
| 15 | +x = c(2,6,4,9,-1) |
| 16 | +y = c(1,8,4,-2,4) |
| 17 | +plot(x,y) |
| 18 | +``` |
| 19 | + |
| 20 | +Si no incorporamos vector `y`, `R` nos va a tomar el parámetro `x` como si fuese el vector de datos `y` : `plot(1:n, x) |
| 21 | + |
| 22 | +```{r} |
| 23 | +plot(2^(1:6)) |
| 24 | +``` |
| 25 | + |
| 26 | +Si queremos representar una función $f(x)$: |
| 27 | + |
| 28 | +```{r} |
| 29 | +f <- function(x){ sqrt(x) } |
| 30 | +plot(f) |
| 31 | +``` |
| 32 | + |
| 33 | +## Parámetros |
| 34 | + |
| 35 | +```{r, echo=FALSE, fig.align='center'} |
| 36 | +n = 1:20 |
| 37 | +fib = (1/sqrt(5))*((1+sqrt(5))/2)^n - (1/sqrt(5))*((1-sqrt(5))/2)^n |
| 38 | +fib |
| 39 | +par(mfrow = c(1,2)) |
| 40 | +plot(fib, xlab = "n", ylab = expression(F[n]), |
| 41 | + main = "Sucesión de Fibonacci", pch = 21, cex = 2, |
| 42 | + col = "powderblue", bg = "orchid", log = "xy") |
| 43 | +
|
| 44 | +plot(fib, xlab = "n", ylab = expression(F[n]), |
| 45 | + main = "Sucesión de Fibonacci", pch = 21, cex = 2, |
| 46 | + col = "powderblue", bg = "orchid", log = "y") |
| 47 | +par(mfrow = c(1,1)) |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +```{r} |
| 52 | +plot(n, fib, pch = 21, col = "red", bg = "yellow", cex = 1.2, |
| 53 | + main = "Fibonacci", |
| 54 | + type = "o", lty = "dashed", lwd = 2, |
| 55 | + xlim = c(1,10), ylim = c(1, 100), |
| 56 | + xaxp = c(1,10,3), yaxp = c(0,100, 10)) |
| 57 | +``` |
| 58 | + |
| 59 | +## Cómo añadir elementos a un gráfico |
| 60 | +```{r} |
| 61 | +f <- function(x){ |
| 62 | + x^2 -2*x + sqrt(abs(x)) |
| 63 | +} |
| 64 | +plot(f, xlim = c(-3,3)) |
| 65 | +points(0,0, pch = 19) |
| 66 | +points(-3:3, (-3:3)^2, col = "blue") |
| 67 | +abline(2,3, lty = "dashed", col = "red") |
| 68 | +abline(v = 2, lty = "dotted", col = "green") |
| 69 | +abline(h = 5, lty = "dotdash", col = "gray") |
| 70 | +``` |
| 71 | + |
| 72 | +```{r} |
| 73 | +f <- function(x){x^2} |
| 74 | +plot(f, xlim = c(-3,3), col = "red", lwd = 2, ylab = expression(y^2), xlab = "x") |
| 75 | +abline(h=0:9, v = -3:3, lty="dotted", col = "grey") |
| 76 | +``` |
| 77 | + |
| 78 | +```{r} |
| 79 | +plot(tan, xlim = c(-pi, pi), ylim = c(-5,5)) |
| 80 | +abline(v = c(-pi/2, pi/2), col = "red") |
| 81 | +``` |
| 82 | + |
| 83 | +```{r} |
| 84 | +plot(0,0) |
| 85 | +text(0,0, labels = "debajo", pos = 1) |
| 86 | +text(0,0, labels = "izquierda", pos = 2) |
| 87 | +text(0,0, labels = "arriba", pos = 3) |
| 88 | +text(0,0, labels = "derecha", pos = 4) |
| 89 | +points(0,1) |
| 90 | +text(0,1, labels = "centro") |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +```{r} |
| 95 | +f <- function(x){x^2} |
| 96 | +plot(f, xlim = c(-3,3), ylim = c(-10,10)) |
| 97 | +points(-3:3, f(-3:3), pch = 19) |
| 98 | +lines(-3:3, f(-3:3), lwd = 2, lty = "dotted", col = "red") |
| 99 | +curve(x^3, lty = "dashed", col = "blue", add = TRUE) |
| 100 | +curve(x^4, lty = "dashed", col = "orangered", add=TRUE) |
| 101 | +legend("bottomright", legend = c(expression(x^2), expression(x^3), expression(x^4)), lwd = 2, col = c("red", "blue", "orangered"), lty = c("dotted", "dashed", "dashed")) |
| 102 | +``` |
| 103 | + |
0 commit comments