#Plots
#Variables cualitativas (CategorÃas) ¬Ordinales ¬Nominales
#Cuantitativas ¬Continuas ¬Discretas
#clasificar de su dataset las variables
library(dslabs)
library(dplyr)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(viridisLite)
library(RColorBrewer)
data("stars")
#Descripción del dataset
#Variables cualitativas nominales:
"
-Star
-Type
"
## [1] "\n -Star\n -Type\n "
#Variables cuantitativas discretas:
"
-Temp
"
## [1] "\n -Temp\n "
#Variables cuantitativas Continuas:
"
-Magnitude
"
## [1] "\n -Magnitude\n "
#Diagrama de barras
#Orden especifico
tipos<-factor(stars$type,levels = c("M", "B", "K", "A", "F", "G", "DA", "DB", "DF", "O"))
tabla_tipos <- table(tipos)
#PRimero hay qeu hacer conteo
tabla_tipos
## tipos
## M B K A F G DA DB DF O
## 32 19 16 13 7 4 2 1 1 1
#Diagrama
barplot(
tabla_tipos, #Datos
main = "Gráfico de barras (Tipos de estrellas)", #TÃtulo principal
ylab ="Frecuencia", #titulo del eje X
xlab = "Tipos de estrellas", #TÃtulo del eje Y
ylim = c(0, 40), #Frecuencias del eje Y
col = viridis(4)) #Colores (en lugar de viridis se puede especificar un color "blue")
grid(nx=NA, ny=NULL, col = "lightgray", lty = "dotted", lwd = par("lwd"))
#Diagrama circular
porcentajes <- round(tabla_tipos*100/sum(tabla_tipos),1)
porcentajes
## tipos
## M B K A F G DA DB DF O
## 33.3 19.8 16.7 13.5 7.3 4.2 2.1 1.0 1.0 1.0
pie(
porcentajes,
labels = paste(porcentajes, "%"),
main = "Tipos de estrellas",
col = brewer.pal(10, "Paired"),
border = brewer.pal(10, "Paired"),
cex=0.6
)
legend("bottomright", legend = names(porcentajes), fill = brewer.pal(10, "Paired"))
#Variables cuantitativas
media <- mean(stars$temp)
mediana <-median(stars$temp)
#HISTOGRAMAS MAGNITUDES
hist(stars$temp,
breaks = "Sturges",
main = "Histogram of temperatures",
xlab= "Population in US",
ylim = c(0,50),
col = brewer.pal(8, "Pastel2"),
border = brewer.pal(8, "Set1"),
labels = TRUE
)
axis(1, at = seq(floor(min(stars$temp)), ceiling(max(stars$temp)), by = 10000))
hist(stars$temp,
breaks = "Sturges",
probability = TRUE,
main = "Histogram of temperatures",
xlab= "Population in US",
col = brewer.pal(8, "Pastel2"),
border = brewer.pal(4, "Set1"))
lines(density(stars$temp), col ="black", lwd=2)
abline(v = media, col = "red", lwd = 2, lty = 2) # LÃnea de la media (roja)
abline(v = mediana, col = "green", lwd = 2, lty = 2) # LÃnea de la mediana (verde)
legend("topright", legend = c("Media", "Mediana"),
col = c("red", "green"), lwd = 2, lty = 2)
#Es una distribución asimétrica hacia la izquierda, donde el promedio es menor que la mediana en la curva
#Interpretar los gráficos
#Agregar dos lineas verticales (Mediana, Promedio)
boxplot(stars$temp, col="Blue",ylab="Temperaturas",outline=FALSE,main="Boxplot",ylim=c(0,18500))
#Adicionar la media
points(mean(stars$temp),col="black",pch=21)
text(paste(" ", round(mean(stars$temp), 2)),x=1.1, y=8000)
############################################################3
#######################################
boxplot(stars$temp, col="Blue",ylab="Temperaturas",outline=FALSE,main="Boxplot",ylim=c(0,18000))
#Adicionar la media
points(mean(stars$temp),col="black",pch=21)
text(paste(" ", round(mean(stars$temp), 2)),x=1.1, y=8000)
points(round(min(stars$temp)),col="red",pch=18)
text(paste(" ", min(stars$temp), 2),x=1.1, y=min(stars$temp)-1000)
legend("topright", legend = c("Media", "Mediana"),
col = c("Orange", "red"), lwd = 2, lty = 2)
abline( h = mean(stars$temp), col="Orange",lwd = 1, lty = 2)
abline( h = median(stars$temp), col ="Red", lwd = 1, lty = 2)
##Comparación cuantitativas/Cualitativas
x<- stars$temp
y<- stars$magnitude
plot(x,y,main="Temperature vs Magnitude",col=as.factor(stars$type),pch=20,xlab = "Temperature",ylab="Magnitud")
legend("bottomright",legend=levels(as.factor(stars$type)),fill = unique(as.factor(stars$type)),cex=0.75)
boxplot(temp~type,data=stars,col=brewer.pal(9,"Set1"),outline=FALSE,ylim=c(2000,35000),main="Boxplot")
abline(h=mean(stars$temp),col="blue",lwd=2)
text(paste("",round(mean(stars$temp),2)),x=0.8,y=5500,col="blue")
## Tabla gráficos
par(mfrow=c(1,2))
plot(x,y,main="Temperature vs Magnitude",col=as.factor(stars$type),pch=20,xlab = "Temperature",ylab="Magnitud")
legend("bottomright",legend=levels(as.factor(stars$type)),fill = unique(as.factor(stars$type)),cex=0.75)
boxplot(temp~type,data=stars,col=brewer.pal(9,"Set1"),outline=FALSE,ylim=c(2000,35000),main="Boxplot")
abline(h=mean(stars$temp),col="blue",lwd=2)
text(paste("",round(mean(stars$temp),2)),x=0.8,y=5500,col="blue")