Gráficos con la función plot

x = c(2, 6, 4, 9, -1)
y = c(1, 8, 4, -2, 4)

plot(x, y, main = "Gráfico con Leyenda", xlab = "Eje X", ylab = "Eje Y")

# Añadir la leyenda
legend("topright", legend = c("Datos de ejemplo"), col = "black", pch = 1)
Gráfico básico explicando el uso del plot

Gráfico básico explicando el uso del plot

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)

plot(2^(1:6))

Si queremos representar una función \(f(x)\):

f <- function(x) { sqrt(x) }
curve(f, from = 0, to = 10, col = "blue", lwd = 2, main = "Grafico de la funcion sqrt(x)", xlab = "x", ylab = "sqrt(x)")

# Añadir la leyenda
legend("bottomright", legend = c("sqrt(x)"), col = "blue", lwd = 2)

Parámetros

n = 1:20
fib = (1/sqrt(5)) * ((1 + sqrt(5)) / 2)^n - (1/sqrt(5)) * ((1 - sqrt(5)) / 2)^n

plot(n, fib, pch = 21, col = "red", bg = "yellow", cex = 1.2, 
     main = "Fibonacci",
     type = "o", lty = "dashed", lwd = 2, 
     xlim = c(1, 10), ylim = c(1, 100),
     xaxp = c(1, 10, 3), yaxp = c(0, 100, 10))

# Añadir la leyenda
legend("topright", legend = "Sucesion de Fibonacci", col = "red", pch = 21, pt.bg = "yellow", lty = "dashed", lwd = 2)

# Configurar el diseño de gráficos
par(mfrow = c(3, 2))

# Crear un vector numérico 'x'
x <- c(50:59)
y <- c(2, 9, 25, 3, 108, 77, 62, 54, 19, 40)

# Graficar 'x' con diferentes parámetros gráficos
plot(x, y, pch = 23, cex = 2, col = "blue", type = "p", main = "Puntos")
legend("topright", legend = "Puntos", col = "blue", pch = 23, pt.cex = 2)

plot(x, y, pch = 23, cex = 2, col = "blueviolet", type = "l", main = "Lineas")
legend("topright", legend = "Lineas", col = "blueviolet", lty = 1, lwd = 2)

plot(x, y, pch = 23, cex = 2, col = "gold", type = "b", main = "Puntos y Lineas")
legend("topright", legend = "Puntos y Lineas", col = "gold", pch = 23, pt.cex = 2, lty = 1, lwd = 2)

plot(x, y, pch = 23, cex = 2, col = "deeppink", type = "o", main = "Puntos y Lineas Superpuestas")
legend("topright", legend = "Puntos y Lineas Superpuestas", col = "deeppink", pch = 23, pt.cex = 2, lty = 1, lwd = 2)

plot(x, y, pch = 23, cex = 2, col = "springgreen", type = "h", main = "Lineas Verticales")
legend("topright", legend = "Lineas Verticales", col = "springgreen", lty = 1, lwd = 2)

plot(x, y, pch = 23, cex = 2, col = "firebrick", type = "s", main = "Pasos")
legend("topright", legend = "Pasos", col = "firebrick", lty = 1, lwd = 2)

# Restablecer el diseño de gráficos
par(mfrow = c(1, 1))
x = 2 * (1:20)
y <- (-1)^(1:20) * 5 * (1:20)

plot(x, y, main = "Ejemplo de grafico", pch = 8, cex = 1, type = "b", lty = 4, lwd = 4, xaxp = c(0, 40, 2), yaxp = c(-100, 100, 8))

# Añadir la leyenda
legend("bottomleft", legend = "Datos de ejemplo", col = "black", pch = 8, lty = 4, lwd = 4)

x = (2 * (1:20))
y <- (-1)^(1:20) * 5 * (1:20)
plot(x, y, main = "Poniendo un punto y una Recta", pch = 8, cex = 1, type = "b", lty = 4, lwd = 4, xaxp = c(0, 40, 2), yaxp = c(-100, 100, 8))
points(20,0, col = "red", cex = 4, pch = 16)
abline(h = 0, lty = 2, col = "dodgerblue")

Cómo añadir elementos a un gráfico

f <- function(x) {
  x^2 - 2*x + sqrt(abs(x))
}

plot(f, xlim = c(-3, 3), main = "Grafico de la funcion f(x)")
points(0, 0, pch = 19)
points(-3:3, (-3:3)^2, col = "blue")
abline(2, 3, lty = "dashed", col = "red")
abline(v = 2, lty = "dotted", col = "green")
abline(h = 5, lty = "dotdash", col = "gray")

# Añadir la leyenda
legend("top", legend = c("f(x)", "Punto (0,0)", "Puntos (-3:3)^2", "Linea y = 2 + 3x", "Linea vertical x = 2", "Linea horizontal y = 5"), 
       col = c("black", "black", "blue", "red", "green", "gray"), 
       pch = c(NA, 19, 1, NA, NA, NA), 
       lty = c(1, NA, NA, 2, 3, 4), 
       lwd = c(1, NA, NA, 2, 2, 2))

f <- function(x) { x^2 }

plot(f, xlim = c(-3, 3), col = "red", lwd = 2, ylab = expression(y^2), xlab = "x")
abline(h = 0:9, v = -3:3, lty = "dotted", col = "grey")

# Añadir la leyenda
# El vector 'lty' debe tener una longitud de 2, 4, 6 u 8
legend("top", legend = c("f(x) = x^2", "Lineas horizontales y verticales"), 
       col = c("red", "grey"), lty = c(1, 2), lwd = c(2, 1))

plot(tan, xlim = c(-pi, pi), ylim = c(-5, 5), main = "Grafico de la funcion tan(x)")
abline(v = c(-pi/2, pi/2), col = "red", lty = "dotted")

# Añadir la leyenda
# El vector 'lty' debe tener una longitud de 2, 4, 6 u 8
legend("top", legend = c("tan(x)", "Asintotas verticales"), 
       col = c("black", "red"), lty = c(1, 2), lwd = c(1, 1))

## Como añadir texto y curvas a un grafico

plot(0,0)
text(0,0, labels = "debajo", pos = 1)
text(0,0, labels = "izquierda", pos = 2)
text(0,0, labels = "arriba", pos = 3)
text(0,0, labels = "derecha", pos = 4)
points(0,1)
text(0,1, labels = "centro")

alumnos = c(1:10)
notas = c(2, 5, 7, 9, 8, 3, 5, 6, 10, 7)

plot(alumnos, notas, main = "Agregando Texto al Grafico", xlab = "Alumnos", ylab = "Notas")
text(alumnos, notas, labels = c("S", "A", "N", "E", "N", "S", "A", "A", "E", "N"), pos = c(rep(3, times = 8), 1, 3))

# Resaltar al alumno con la mayor nota
max_nota = which.max(notas)
points(alumnos[max_nota], notas[max_nota], col = "green", pch = 8, cex = 2)

# Resaltar al alumno con la menor nota
min_nota = which.min(notas)
points(alumnos[min_nota], notas[min_nota], col = "red", pch = 8, cex = 2)

# Añadir la leyenda
legend("bottomright", legend = c("Notas de Alumnos", "Mayor Nota", "Menor Nota"), col = c("black", "green", "red"), pch = c(1, 8, 8))

f <- function(x){x^2}
plot(f, xlim = c(-3,3),main = "Agregando lineas, Puntos y Curvas", ylim = c(-10,10))
points(-3:3, f(-3:3), pch = 19)
lines(-3:3, f(-3:3), lwd = 2, lty = "dotted", col = "red")
curve(x^3, lty = "dashed", col = "blue", add = TRUE)
curve(x^4, lty = "dashed", col = "orangered", add=TRUE)
legend("bottomleft", 
       legend = c(expression(x^2), expression(x^3), expression(x^4)), 
       lwd = 2, 
       col = c("red", "blue", "orangered"), 
       lty = c("dotted", "dashed", "dashed")
       )

## Añadiendo lineas y curvas

x = 5 * (1:20)
y = exp(-x) + (-1)^x * x / 2 * sin(x)^2

plot(x, y, type = "o", 
     col = "blue", 
     main = "Agregando Curvas, Puntos y Lineas", 
     xlab = "x", 
     ylab = "y")
lines(x, 20 * sin(x), 
      col = "green")

# Añadir la leyenda
legend("topleft", 
       legend = c("y = exp(-x) + (-1)^x * x / 2 * sin(x)^2", "20 * sin(x)"), 
       col = c("blue", "green"), 
       lty = 1, 
       pch = c(1, NA))

# Añadiendo leyenda
x = seq(0, 2 * pi, 0.1)
plot(x, sin(x), 
     type = "l", 
     col = "yellow", 
     lwd = 3, xlab = "", 
     ylab = "", 
     main = "Agregando Leyendas")

lines(x, cos(x), 
      col = "blue", 
      lwd = 3)
lines(x, tan(x), 
      col = "red", 
      lwd = 3)
legend("bottomleft", legend = c("Seno", "Coseno", "Tangente"), 
       col = c("yellow", "blue", "red"), 
       lwd = 3, 
       bty = "n")

x = c(5*(1:10))
plot(x, c(exp(-x) + (-1)^x * x / 2 * sin(x)^2), xlab = "", ylab = "", 
     main = "Grafico con varios elementos")
segments(10, 0, 40, 0, col = "red", lwd = 4)
arrows(10, 0, 40, -10, col = "blue", length = 0.5, angle = 5, code = 3)
symbols(40, 0, stars = cbind(1,.5,1,.5,1, .5,1,.5,1,.5), add = TRUE, lwd = 3, inches = 0.5)
symbols(40, 0, stars = cbind(1,.5,1,.5,1, .5,1,.5,1,.5), add = TRUE, lwd =3)
polygon(c(20, 30, 40), c(10, -10, 10), col = "gold", density = 3, angle = 90, lty = 6, border = 5)