data(mtcars)
attach(mtcars)
mi_tabla<- table(cyl)
mi_tabla
## cyl
## 4 6 8
## 11 7 14
par(mfrow = c(1, 2))
barplot(mi_tabla, main = "Frequencia absoluta",
col = rainbow(3))
barplot(prop.table(mi_tabla) * 100, main = "Frequencia relativa (%)",
col = rainbow(3))
par(mfrow = c(1, 1))
plot(factor(mtcars$cyl), col = rainbow(3))
barp <- barplot(mi_tabla, # Guarda los valores de X que
main = "Frequencia absoluta", # representan el centro de
col = rainbow(3)) # cada barra
lines(barp, c(5, 4, 12), type = "o", lwd = 3)
barp <- barplot(mi_tabla, col = rainbow(3), ylim = c(0, 15))
text(barp, mi_tabla + 0.5, labels = mi_tabla)
barp <- barplot(mi_tabla, col = rainbow(3), ylim = c(0, 15))
grid(nx = NA, ny = NULL, lwd = 1, lty = 1, col = "gray")
barplot(mi_tabla, col = rainbow(3), ylim = c(0, 15), add = TRUE)
barplot(mi_tabla,
main = "Gráfico de barras",
xlab = "Número de cilindros",
ylab = "Frecuencia",
border = "green",
col = c("grey", "blue", "red"))
barplot(mi_tabla, names.arg = c("cuatro", "seis", "ocho"))
barplot(mi_tabla, main = "Cambiar el ancho de las barras",
col = rainbow(3), width = c(0.4, 0.2, 1))
barplot(mi_tabla, main = "Cambiar el espacio entre barras",
col = rainbow(3), space = c(1, 1.1, 0.1))
df <- data.frame(ColorCoche = c("rojo", "verde", "rosado", "azul"),
num = c(3, 5, 9, 1))
par(mfrow = c(1, 1))
barplot(height = df$num, names = df$ColorCoche,
col = c("red", "green", "pink", "darkblue"))
x <- c(2.1, 8.6, 3.9, 4.4, 4.0, 3.7, 7.6, 3.1, 5.0, 5.5, 20.2, 1.7,
5.2, 33.7, 9.1, 1.6, 3.1, 5.6, 16.5, 15.8, 5.8, 6.8, 3.3, 40.6)
barplot(table(cut(x, breaks = seq(0, 45, by = 5))))
barplot(mi_tabla, main = "Gráfico de barras horizontal",
ylab = "Número de cilindros", xlab = "Frecuencia",
horiz = TRUE)
barplot(mi_tabla, xlab = "Número de cilindros",
col = rainbow(3),
legend.text = rownames(mi_tabla))
barplot(mi_tabla, xlab = "Número de cilindros",
col = rainbow(3),
legend.text = rownames(mi_tabla),
args.legend = list(x = "top"))
par(mar = c(5, 5, 4, 10))
barplot(mi_tabla, xlab = "Número de cilindros",
col = rainbow(3),
legend.text = rownames(mi_tabla), # Valores de la leyenda
args.legend = list(x = "topright", inset = c(-0.20, 0)))
barplot(mi_tabla, xlab = "Número de cilindos",
col = rainbow(3),
legend.text = rownames(mi_tabla), xlim = c(0, 4.25))
plot.new()
layout(rbind(1, 2), heights = c(10, 3))
barplot(mi_tabla, xlab = "Número de cilindros",
col = rainbow(3))
par(mar = c(0, 0, 0, 0))
plot.new()
legend("top", rownames(mi_tabla), lty = 1,
col = c("red", "green", "blue"), lwd = c(1, 2))
am<- factor(am)
levels(am) <- c("Automatica", "Manual")
tabla_variables <- table(cyl, am)
tabla_variables <- xtabs(~cyl + am , data = mtcars)
barplot(tabla_variables,
main = "Gráfico de barras agrupado",
xlab = "Tipo de transmisión", ylab = "Frecuencia",
col = c("darkgrey", "darkblue", "red"),
legend.text = rownames(tabla_variables),
beside = TRUE)
barplot(tabla_variables,
main = "Espacio entre grupos",
xlab = "Tipo de transmisión", ylab = "Frecuencia",
col = c("darkgrey", "darkblue", "red"),
legend.text = rownames(tabla_variables),
beside = TRUE,
space = c(0.4, 2.5))
resumen_datos <- tapply(mtcars$hp, list(cilindros = mtcars$cyl,
transmision = am),
FUN = mean, na.rm = TRUE)
resumen_datos
## transmision
## cilindros Automatica Manual
## 4 84.66667 81.8750
## 6 115.25000 131.6667
## 8 194.16667 299.5000
par(mar = c(5, 5, 4, 10))
barplot(resumen_datos, xlab = "Tipo de transmisión",
main = "Media CV",
col = rainbow(3),
beside = TRUE,
legend.text = rownames(resumen_datos),
args.legend = list(title = "Cilindros", x = "topright",
inset = c(-0.20, 0)))
plot.new()
barplot(tabla_variables,
main = "Gráfico de barras apilado",
xlab = "Tipo de transmisión", ylab = "Frecuencia",
col = c("darkgrey", "darkblue", "red"),
legend.text = rownames(tabla_variables),
beside = FALSE)
library(graphics)
mosaicplot(tabla_variables, main = "Mosaico")
spineplot(t(tabla_variables))
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.1
##
## Attaching package: 'ggplot2'
## The following object is masked from 'mtcars':
##
## mpg
df <- as.data.frame(mi_tabla)
ggplot(data = df, aes(x = cyl, y = Freq)) +
geom_bar(stat = "identity")
ggplot(data = df, aes(x = cyl, y = Freq)) +
geom_bar(stat = "identity") +
coord_flip()
distancia <- c(241.1, 284.4, 220.2, 272.4, 271.1, 268.3,
291.6, 241.6, 286.1, 285.9, 259.6, 299.6,
253.1, 239.6, 277.8, 263.8, 267.2, 272.6,
283.4, 234.5, 260.4, 264.2, 295.1, 276.4,
263.1, 251.4, 264.0, 269.2, 281.0, 283.2)
hist(distancia, main = "Histograma de frecuencias", # Frecuencia
ylab = "Frecuencia")
hist(distancia, prob = TRUE, main = "Histograma de densidad", # Densidad
ylab = "Densidad")
hist(distancia, prob = TRUE, ylab = "Densidad", main = "Grid")
grid(nx = NA, ny = NULL, lty = 2, col = "gray", lwd = 1)
hist(distancia, prob = TRUE, add = TRUE, col = "blue")
hist(distancia, main = "Cambiar color", ylab = "Frecuencia", col = "lightblue")
par(mfrow = c(1, 3))
hist(distancia, breaks = 2, main = "Pocas clases", ylab = "Frecuencia")
hist(distancia, breaks = 50, main = "Demasiadas clases", ylab = "Frecuencia")
hist(distancia, main = "Método de Sturges", ylab = "Frecuencia")
par(mfrow = c(1, 1))
library(KernSmooth)
## KernSmooth 2.23 loaded
## Copyright M. P. Wand 1997-2009
ancho_barras <- dpih(distancia)
nbarras <- seq(min(distancia) - ancho_barras,
max(distancia) + ancho_barras, by = ancho_barras)
hist(distancia, breaks = nbarras, main = "Plug-in", ylab = "Frecuencia")
set.seed(1)
x <- rnorm(1000) # Primer grupo
y <- rnorm(1000, 1) # Segundo grupo
hist(x, main = "Dos variables", ylab = "Frecuencia")
hist(y, add = TRUE, col = rgb(1, 0, 0, alpha = 0.5))
### Histograma coin curva normal, con la funcion dnorm y lines
hist(distancia, prob = TRUE,
main = "Histograma con curva normal", ylab = "Densidad")
x <- seq(min(distancia), max(distancia), length = 40)
f <- dnorm(x, mean = mean(distancia), sd = sd(distancia))
lines(x, f, col = "red", lwd = 2)
hist(distancia, freq = FALSE, main = "Curva densidad", ylab = "Densidad")
lines(density(distancia), lwd = 2, col = 'red')
histDenNorm <- function (x, ...) {
hist(x, ...) # Histograma
lines(density(x), col = "blue", lwd = 2) # Densidad
x2 <- seq(min(x), max(x), length = 40)
f <- dnorm(x2, mean(x), sd(x))
lines(x2, f, col = "red", lwd = 2) # Normal
legend("topright", c("Histograma", "Densidad", "Normal"), box.lty = 0,
lty = 1, col = c("black", "blue", "red"), lwd = c(1, 2, 2))
}
set.seed(1)
# Datos normales
x <- rnorm(n = 5000, mean = 110, sd = 5)
# Datos exponenciales
y <- rexp(n = 3000, rate = 1)
par(mfcol = c(1, 2))
histDenNorm(x, prob = TRUE, main = "Histograma de X")
histDenNorm(y, prob = TRUE, main = "Histograma de Y")
par(mfcol = c(1, 1))
hist(distancia, probability = TRUE, ylab = "", main = "",
col = rgb(1, 0, 0, alpha = 0.5), axes = FALSE)
axis(1) # Añade el eje horizontal
par(new = TRUE)
boxplot(distancia, horizontal = TRUE, axes = FALSE,
lwd = 2, col = rgb(0, 0, 0, alpha = 0.2))
library(ggplot2)
ggplot(data.frame(distancia), aes(x = distancia)) +
geom_histogram(color = "gray", fill = "white")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
nbreaks <- pretty(range(distancia), n = nclass.Sturges(distancia),
min.n = 1)
ggplot(data.frame(distancia), aes(x = distancia)) +
geom_histogram(breaks = nbreaks, color = "gray", fill = "white")
ggplot(data.frame(distancia), aes(x = distancia)) +
geom_histogram(aes(y = ..density..), breaks = nbreaks,
color = "gray", fill = "white") +
geom_density(fill = "black", alpha = 0.2)
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.1
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
###### Histograma de frecuencias
fig <- plot_ly(x = distancia, type = "histogram")
fig
fig <- plot_ly(x = distancia, type = "histogram", histnorm = "probability")
fig
### Datos de ejemplo
set.seed(1234)
# Generamos datos
x <- rnorm(500)
par(mfrow = c(1, 2))
# Creamos un histograma
hist(x, freq = FALSE, main = "Histograma y densidad",
ylab = "Densidad")
# Calculamos la densidad
dx <- density(x)
# Añadimos la línea de densidad
lines(dx, lwd = 2, col = "red")
# Curva de densidad sin histograma
plot(dx, lwd = 2, col = "red",
main = "Densidad")
# Añadimos los datos con riudo en el eje X
rug(jitter(x))
library(EnvStats)
## Warning: package 'EnvStats' was built under R version 4.3.1
##
## Attaching package: 'EnvStats'
## The following objects are masked from 'package:stats':
##
## predict, predict.lm
epdfPlot(x, epdf.col = "red")
par(mfrow = c(1, 2))
# Ventana grande
plot(density(x, bw = 20), lwd = 2,
col = "red", main = "Ventana demasiado grande")
# Ventana pequeña
plot(density(x, bw = 0.05), lwd = 2,
col = "red", main = "Ventana demasiado pequeña")
### En este caso, estamos pasando el argumento bw de la función de densidad.
# Alternativa equivalente con el paquete EnvStats
epdfPlot(x, epdf.col = "red", density.arg.list = list(bw = 0.05),
main = "Ventana demasiado pequeña")
par(mfrow = c(1, 3))
# Regla del pulgar
plot(density(x), main = "Regla del pulgar",
cex.lab = 1.5, cex.main = 1.75, lwd = 2)
# Validación cruzada insesgada
plot(density(x, bw = bw.ucv(x)), col = 2, # Igual a: bw = "UCV"
main = "Validación cruzada", cex.lab = 1.5,
cex.main = 1.75, lwd = 2)
# Plug-in
plot(density(x, bw = bw.SJ(x)), col = 4, # Igual a: bw = "SJ"
main = "Método plug-in",
cex.lab = 1.5, cex.main = 1.75, lwd = 2)
set.seed(1)
grupos <- factor(sample(c(1, 2), 100, replace = TRUE))
variable <- numeric(100)
# Grupo 1: media 3
variable[grupos == 1] <- rnorm(length(variable[grupos == 1]), 3)
# Grupo 2: media 0
variable[grupos == 2] <- rnorm(length(variable[grupos == 2]))
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:EnvStats':
##
## qqPlot
densityPlot(variable, grupos)
library(sm)
## Warning: package 'sm' was built under R version 4.3.1
## Package 'sm', version 2.2-5.7: type help(sm) for summary information
sm.density.compare(variable, grupos)
legend("topleft", levels(grupos), col = 2:4, lty = 1:2)
par(mfrow = c(1, 2))
#——————————–
#——————————–
plot(dx, lwd = 2, main = "", xlab = "",
col = "red", xlim = c(-4, 6), ylim = c(0, 0.5))
polygon(dx, col = "red")
polygon(dx$x, dx$y, col = "red") # Equivalente
set.seed(2)
y <- rnorm(500) + 2
#————————————————–
#————————————————–
plot(dx, lwd = 2, main = "", xlab = "",
col = "red", xlim = c(-4, 6), ylim = c(0, 0.5))
polygon(dx, col = rgb(1, 0, 0, alpha = 0.5))
getwd()
## [1] "C:/Users/FLOR MARIA MOROCHO/Downloads"
library(EnvStats)
epdfPlot(x, # Vector con datos
curve.fill = TRUE, # Colorear el área
curve.fill.col = rgb(1, 0, 0, alpha = 0.5), # Color del área
epdf.col = "red") # Color de la curva
epdfPlot(y, curve.fill = TRUE,
curve.fill.col = rgb(0, 0, 1, alpha = 0.5),
epdf.col = "blue",
add = TRUE)
par(mfrow = c(1, 1))
plot(dx, lwd = 2, main = "Densidad", col = "red")
polygon(c(dx$x[dx$x >= 0], 0), c(dx$y[dx$x >= 0], 0),
col = rgb(1, 0, 0, alpha = 0.5), border = "red", main = "")
library(ggplot2)
df <- data.frame(x = x)
ggplot(df, aes(x = x)) +
geom_density(color = "red", # Color de la curva
fill = "red", # Color del área sombreada
alpha = 0.5) # Transparencia del color
df <- data.frame(x = x, y = y)
df <- stack(df)
dx <- density(x)
dy <- density(y)
ggplot(df, aes(x = values, fill = ind)) +
geom_density(alpha = 0.5) + # Densidades con transparencia
xlim(c(min(dx$x, dy$x), # Límites del eje X
c(max(dx$x, dy$x)))) +
scale_fill_discrete(name = "Título de la leyenda", # Cambiar el título de la leyenda
labels = c("A", "B")) # + # Cambiar las etiquetas de la leyenda
ggplot(df, aes(x = values)) +
geom_density(aes(group = ind, fill = ind), alpha = 0.5) +
xlim(c(min(dx$x, dy$x), c(max(dx$x, dy$x)))) +
scale_fill_discrete(name = "Título de la leyenda",
labels = c("A", "B"))
x <- c(8, 5, 14, -9, 19, 12, 3, 9, 7, 4,
4, 6, 8, 12, -8, 2, 0, -1, 5, 3)
boxplot(x, horizontal = TRUE)
stripchart(x, method = "jitter", pch = 19, add = TRUE, col = "blue")
boxplot(x, notch = TRUE)
head(chickwts)
## weight feed
## 1 179 horsebean
## 2 160 horsebean
## 3 136 horsebean
## 4 227 horsebean
## 5 217 horsebean
## 6 168 horsebean
boxplot(chickwts$weight ~ chickwts$feed)
boxplot(weight ~ feed, data = chickwts) # Equivalente
stripchart(chickwts$weight ~ chickwts$feed, vertical = TRUE, method = "jitter",
pch = 19, add = TRUE, col = 1:length(levels(chickwts$feed)))
head(trees)
## Girth Height Volume
## 1 8.3 70 10.3
## 2 8.6 65 10.3
## 3 8.8 63 10.2
## 4 10.5 72 16.4
## 5 10.7 81 18.8
## 6 10.8 83 19.7
stacked_df <- stack(trees)
head(stacked_df)
## values ind
## 1 8.3 Girth
## 2 8.6 Girth
## 3 8.8 Girth
## 4 10.5 Girth
## 5 10.7 Girth
## 6 10.8 Girth
boxplot(trees, col = rainbow(ncol(trees)))
boxplot(stacked_df$values ~ stacked_df$ind,
col = rainbow(ncol(trees)))
par(mfrow = c(1, ncol(trees)))
invisible(lapply(1:ncol(trees), function(i) boxplot(trees[, i])))
par(mfrow = c(1, 2))
medianas <- reorder(chickwts$feed, chickwts$weight, median)
boxplot(chickwts$weight ~ medianas, las = 2, xlab = "", ylab = "")
medianas <- reorder(chickwts$feed, -chickwts$weight, median)
boxplot(chickwts$weight ~ medianas, las = 2, xlab = "", ylab = "")
par(mfrow = c(1, 1))
plot.new()
set.seed(1)
# Fondo gris claro
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = "#ebebeb")
# Añadimos un grid blanco
grid(nx = NULL, ny = NULL, col = "white", lty = 1,
lwd = par("lwd"), equilogs = TRUE)
# Boxplot
par(new = TRUE)
boxplot(rnorm(500), # Datos
horizontal = FALSE, # Horizontal o vertical
lwd = 2, # Lines width
col = rgb(1, 0, 0, alpha = 0.4), # Color
xlab = "Etiqueta eje X", # Etiqueta eje X
ylab = "Etiqueta eje Y", # Etiqueta eje Y
main = "Boxplot personalizado en R base", # Título
notch = TRUE, # Añade intervalos de confianza para la mediana
border = "black", # Color del borde del boxplot
outpch = 25, # Símbolo para los outliers
outbg = "green", # Color de los datos atípicos
whiskcol = "blue", # Color de los bigotes
whisklty = 2, # Tipo de línea para los bigotes
lty = 1) # Tipo de línea (caja y mediana)
# Agregamos una leyenda
legend("topright", legend = "Boxplot", # Posición y título
fill = rgb(1, 0, 0, alpha = 0.4), # Color
inset = c(0.03, 0.05), # Cambiamos los márgenes
bg = "white") # Color de fondo de la leyenda
par(mfrow = c(1, 2))
#—————–
#—————–
boxplot(x)
# Añadir línea con la media
segments(x0 = 0.8, y0 = mean(x),
x1 = 1.2, y1 = mean(x),
col = "red", lwd = 2)
# abline(h = mean(x), col = 2, lwd = 2) # Línea completa
# Añadir punto con la media
points(mean(x), col = 3, pch = 19)
#——————-
#——————-
boxplot(x, horizontal = TRUE)
# Añadir línea con la media
segments(x0 = mean(x), y0 = 0.8,
x1 = mean(x), y1 = 1.2,
col = "red", lwd = 2)
# abline(v = mean(x), col = 2, lwd = 2) # Línea completa
# Añadir punto con la media
points(mean(x), 1, col = 3, pch = 19)
par(mfrow = c(1, 1))
par(mfrow = c(1, 2))
mi_df <- trees
#—————————-
#—————————-
boxplot(mi_df, col = rgb(0, 1, 1, alpha = 0.25))
# Añadir líneas con las medias
invisible(lapply(1:ncol(mi_df),
function(i) segments(x0 = i - 0.4,
y0 = mean(mi_df[, i]),
x1 = i + 0.4,
y1 = mean(mi_df[, i]),
col = "red", lwd = 2)))
# Añadir puntos con la medias
medias <- apply(mi_df, 2, mean)
medias <- colMeans(mi_df) # Equivalente (más eficiente)
points(medias, col = "red", pch = 19)
#—————————–
#—————————–
boxplot(mi_df, col = rgb(0, 1, 1, alpha = 0.25),
horizontal = TRUE)
# Añadir líneas con la medias
invisible(lapply(1:ncol(mi_df),
function(i) segments(x0 = mean(mi_df[, i]),
y0 = i - 0.4,
x1 = mean(mi_df[, i]),
y1 = i + 0.4,
col = "red", lwd = 2)))
# Añadir puntos con la medias
medias <- apply(mi_df, 2, mean)
medias <- colMeans(mi_df) # Equivalente (más eficiente)
points(medias, 1:ncol(mi_df), col = "red", pch = 19)
par(mfrow = c(1, 1))
res <- boxplot(trees)
res
## $stats
## [,1] [,2] [,3]
## [1,] 8.30 63 10.2
## [2,] 11.05 72 19.4
## [3,] 12.90 76 24.2
## [4,] 15.25 80 37.3
## [5,] 20.60 87 58.3
##
## $n
## [1] 31 31 31
##
## $conf
## [,1] [,2] [,3]
## [1,] 11.70814 73.72979 19.1204
## [2,] 14.09186 78.27021 29.2796
##
## $out
## [1] 77
##
## $group
## [1] 3
##
## $names
## [1] "Girth" "Height" "Volume"
bxp(res)
## Boxplot e histograma en R
#### Una limitación de los gráficos de caja y bigotes es que no están diseñados para detectar multimodalidad.
par(mfrow = c(1, 1))
# Datos multimodales
n <- 20000
ii <- rbinom(n, 1, 0.5)
dat <- rnorm(n, mean = 110, sd = 11) * ii +
rnorm(n, mean = 70, sd = 5) * (1 - ii)
# Histograma
hist(dat, probability = TRUE, ylab = "", col = "grey",
axes = FALSE, main = "")
# Eje
axis(1)
# Densidad
lines(density(dat), col = "red", lwd = 2)
# Boxplot
par(new = TRUE)
boxplot(dat, horizontal = TRUE, axes = FALSE,
lwd = 2, col = rgb(0, 1, 1, alpha = 0.15))
###### install.packages("ggplot2")
library(ggplot2)
# Transformar x en un data frame
x <- data.frame(x)
# Boxplot a partir de un vector
ggplot(data = x, aes(x = "", y = x)) +
stat_boxplot(geom = "errorbar",
width = 0.2) +
geom_boxplot(fill = "#4271AE",
outlier.colour = "red",
alpha = 0.9) +
ggtitle("Boxplot a partir de un vector") +
xlab("") +
coord_flip()
# Boxplot por grupo
ggplot(data = chickwts, aes(x = feed, y = weight)) +
stat_boxplot(geom = "errorbar", # Bigotes
width = 0.2) +
geom_boxplot(fill = "#4271AE", colour = "#1F3552", # Colores
alpha = 0.9, outlier.colour = "red") +
scale_y_continuous(name = "Peso") + # Etiqueta de la variable continua
scale_x_discrete(name = "Alimentación") + # Etiqueta de los grupos
ggtitle("Boxplot por grupos en ggplot2") + # Título del plot
theme(axis.line = element_line(colour = "black", # Personalización del tema
size = 0.25))
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Boxplot a partir de un data frame
ggplot(data = stack(trees), aes(x = ind, y = values)) +
stat_boxplot(geom = "errorbar", # Bigotes
width = 0.2) +
geom_boxplot(fill = "#4271AE", colour = "#1F3552", # Colores
alpha = 0.9, outlier.colour = "red") +
scale_y_continuous(name = "Peso") + # Etiqueta de la variable continua
scale_x_discrete(name = "Alimentación") + # Etiqueta de los grupos
ggtitle("Boxplot a partir de un data frame") + # Título del plot
theme(axis.line = element_line(colour = "black", # Personalización del tema
size = 0.25))