html_document

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000

Including Plots

You can also embed plots, for example:

# Calculo para todas las variables su media y desviación estándar
media_mpg <- mean(mtcars$mpg)
sd_mpg <- sd(mtcars$mpg)

# Crear entonces el gráfico
ggplot(mtcars, aes(x=mpg)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") +
  stat_function(fun = dnorm, args = list(mean = media_mpg, sd = sd_mpg), colour = "red") +
  ggtitle("Histograma de Millas por galón") +
  xlab("Millas por galón") +
  ylab("Densidad") +
  theme_minimal()
## 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.

media <- mean(mtcars$cyl)
sd <- sd(mtcars$cyl)
ggplot(mtcars, aes(x=cyl)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") + 
  stat_function(fun = dnorm, args = list(mean = media, sd = sd), colour = "red") +
  ggtitle("Histograma de Número de cilindros") +
  xlab("Número de cilindros") +
  ylab("Densidad") +
  theme_minimal()

media <- mean(mtcars$disp)
sd <- sd(mtcars$disp)
ggplot(mtcars, aes(x=disp)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") + 
  stat_function(fun = dnorm, args = list(mean = media, sd = sd), colour = "red") +
  ggtitle("Histograma de Desplazamiento por pulgadas cúbicas (cu.in.= Cubic Inches)") +
  xlab("Desplazamiento por pulgadas cúbicas (cu.in.= Cubic Inches))") +
  ylab("Densidad") +
  theme_minimal()

media <- mean(mtcars$hp)
sd <- sd(mtcars$hp)
ggplot(mtcars, aes(x=hp)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") + 
  stat_function(fun = dnorm, args = list(mean = media, sd = sd), colour = "red") +
  ggtitle("Histograma de Caballos de fuerza brutos") +
  xlab("Caballos de fuerza brutos") +
  ylab("Densidad") +
  theme_minimal()

media <- mean(mtcars$drat)
sd <- sd(mtcars$drat)
ggplot(mtcars, aes(x=drat)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") + 
  stat_function(fun = dnorm, args = list(mean = media, sd = sd), colour = "red") +
  ggtitle("Histograma de Relación del eje trasero") +
  xlab("Relación del eje trasero") +
  ylab("Densidad") +
  theme_minimal()

media <- mean(mtcars$wt)
sd <- sd(mtcars$wt)
ggplot(mtcars, aes(x=wt)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") + 
  stat_function(fun = dnorm, args = list(mean = media, sd = sd), colour = "red") +
  ggtitle("Histograma de Peso (1000 lbs)") +
  xlab("Peso (1000 lbs)") +
  ylab("Densidad") +
  theme_minimal()

media <- mean(mtcars$qsec)
sd <- sd(mtcars$qsec)
ggplot(mtcars, aes(x=qsec)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") + 
  stat_function(fun = dnorm, args = list(mean = media, sd = sd), colour = "red") +
  ggtitle("Histograma de Tiempo en 1/4 de milla") +
  xlab("Tiempo en 1/4 de milla") +
  ylab("Densidad") +
  theme_minimal()

ggplot(mtcars, aes(x=factor(vs, labels = c("en V", "recto")))) +
  geom_bar(fill = "blue", color = "black") +
  ggtitle("Diagrama de Barras de Motor (0 = en V, 1 = recto)") +
  xlab("Motor (0 = en V, 1 = recto)") +
  ylab("Frecuencia") +
  theme_minimal()

ggplot(mtcars, aes(x=factor(am, labels = c("automática", "manual")))) +
  geom_bar(fill = "blue", color = "black") +
  ggtitle("Diagrama de Barras de Transmisión (0 = automática, 1 = manual)") +
  xlab("Transmisión (0 = automática, 1 = manual)") +
  ylab("Frecuencia") +
  theme_minimal()

media <- mean(mtcars$gear)
sd <- sd(mtcars$gear)
ggplot(mtcars, aes(x=gear)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") + 
  stat_function(fun = dnorm, args = list(mean = media, sd = sd), colour = "red") +
  ggtitle("Histograma de Número de marchas adelante") +
  xlab("Número de marchas adelante") +
  ylab("Densidad") +
  theme_minimal()

media <- mean(mtcars$carb)
sd <- sd(mtcars$carb)
ggplot(mtcars, aes(x=carb)) +
  geom_histogram(aes(y=..density..), bins = 10, fill = "blue", color = "black") + 
  stat_function(fun = dnorm, args = list(mean = media, sd = sd), colour = "red") +
  ggtitle("Histograma de Número de carburadores") +
  xlab("Número de carburadores") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para variables cuantitativas continuas o cuantitativas discretas no dicotómicas + cuartiles + outliers
# Gráfica de violín para Millas por Galón (mpg)
ggplot(mtcars, aes(x = factor(1), y = mpg)) +
  geom_violin(trim = FALSE, fill = "blue") +
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín para Millas por Galón") +
  xlab("Millas por Galón") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para Número de Cilindros (cyl)
ggplot(mtcars, aes(x = factor(1), y = cyl)) +
  geom_violin(trim = FALSE, fill = "blue") + 
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín para Número de cilindros") +
  xlab("Número de cilindros") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para Número de Cilindros (disp)
ggplot(mtcars, aes(x = factor(1), y = disp)) +
  geom_violin(trim = FALSE, fill = "blue") +  
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín para Desplazamiento por pulgadas cúbicas (cu.in.= Cubic Inches)") +
  xlab("Desplazamiento por pulgadas cúbicas (cu.in.= Cubic Inches)") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para Caballos de Fuerza (hp)
ggplot(mtcars, aes(x = factor(1), y = hp)) +
  geom_violin(trim = FALSE, fill = "blue") +  
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín para Caballos de fuerza") +
  xlab("Caballos de Fuerza") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para Relación del eje trasero (drat)
ggplot(mtcars, aes(x = factor(1), y = drat)) +
  geom_violin(trim = FALSE, fill = "blue") +  
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín Relación del eje trasero") +
  xlab("Relación del eje trasero") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para Peso (1000 lbs) (wt)
ggplot(mtcars, aes(x = factor(1), y = wt)) +
  geom_violin(trim = FALSE, fill = "blue") +  
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín para Peso (1000 lbs)") +
  xlab("Peso (1000 lbs)") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para Tiempo en 1/4 de milla (qsec)
ggplot(mtcars, aes(x = factor(1), y = qsec)) +
  geom_violin(trim = FALSE, fill = "blue") +  
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín para Tiempo en 1/4 de milla") +
  xlab("Tiempo en 1/4 de milla") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para Número de marchas para adelante(gear)
ggplot(mtcars, aes(x = factor(1), y = gear)) +
  geom_violin(trim = FALSE, fill = "blue") +  
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín para Número de marchas para adelante") +
  xlab("Número de marchas para adelante") +
  ylab("Densidad") +
  theme_minimal()

# Gráfica de violín para Número de marchas para adelante(carb)
ggplot(mtcars, aes(x = factor(1), y = carb)) +
  geom_violin(trim = FALSE, fill = "blue") +  
  geom_boxplot(width = 0.1, fill = NA, color = "black", outlier.color = "red", outlier.shape = 19) +
  ggtitle("Gráfica de violín para Número de carburadores") +
  xlab("Número de carburadores") +
  ylab("Densidad") +
  theme_minimal()

# Variables cualitativas o cuantitativas discretas analizadas con tablas
variables <- c("cyl", "gear", "carb", "vs", "am")


for(var in variables) {
  cat("\n---\n")
  cat("Variable:", var, "\n")
  frecuencias <- table(mtcars[[var]])
  print("Frecuencias:")
  print(frecuencias)
  
  frecuencias_relativas <- prop.table(frecuencias)
  print("Frecuencias Relativas:")
  print(frecuencias_relativas)
  
  frecuencias_acumuladas <- cumsum(frecuencias)
  print("Frecuencias Acumuladas:")
  print(frecuencias_acumuladas)
}
## 
## ---
## Variable: cyl 
## [1] "Frecuencias:"
## 
##  4  6  8 
## 11  7 14 
## [1] "Frecuencias Relativas:"
## 
##       4       6       8 
## 0.34375 0.21875 0.43750 
## [1] "Frecuencias Acumuladas:"
##  4  6  8 
## 11 18 32 
## 
## ---
## Variable: gear 
## [1] "Frecuencias:"
## 
##  3  4  5 
## 15 12  5 
## [1] "Frecuencias Relativas:"
## 
##       3       4       5 
## 0.46875 0.37500 0.15625 
## [1] "Frecuencias Acumuladas:"
##  3  4  5 
## 15 27 32 
## 
## ---
## Variable: carb 
## [1] "Frecuencias:"
## 
##  1  2  3  4  6  8 
##  7 10  3 10  1  1 
## [1] "Frecuencias Relativas:"
## 
##       1       2       3       4       6       8 
## 0.21875 0.31250 0.09375 0.31250 0.03125 0.03125 
## [1] "Frecuencias Acumuladas:"
##  1  2  3  4  6  8 
##  7 17 20 30 31 32 
## 
## ---
## Variable: vs 
## [1] "Frecuencias:"
## 
##  0  1 
## 18 14 
## [1] "Frecuencias Relativas:"
## 
##      0      1 
## 0.5625 0.4375 
## [1] "Frecuencias Acumuladas:"
##  0  1 
## 18 32 
## 
## ---
## Variable: am 
## [1] "Frecuencias:"
## 
##  0  1 
## 19 13 
## [1] "Frecuencias Relativas:"
## 
##       0       1 
## 0.59375 0.40625 
## [1] "Frecuencias Acumuladas:"
##  0  1 
## 19 32

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(ggplot2) library(tidyr) library(dplyr)