library(foreign)
auto<-read.dta("auto.dta")
head(auto)
##            make price mpg rep78 headroom trunk weight length turn
## 1   AMC Concord  4099  22     3      2.5    11   2930    186   40
## 2     AMC Pacer  4749  17     3      3.0    11   3350    173   40
## 3    AMC Spirit  3799  22    NA      3.0    12   2640    168   35
## 4 Buick Century  4816  20     3      4.5    16   3250    196   40
## 5 Buick Electra  7827  15     4      4.0    20   4080    222   43
## 6 Buick LeSabre  5788  18     3      4.0    21   3670    218   43
##   displacement gear_ratio  foreign
## 1          121       3.58 Domestic
## 2          258       2.53 Domestic
## 3          121       3.08 Domestic
## 4          196       2.93 Domestic
## 5          350       2.41 Domestic
## 6          231       2.73 Domestic
frecuencia<-table(auto$foreign)
barplot(frecuencia)

barplot(frecuencia, main="Grafico de Barras Simple",
        xlab="Procedencia del Auto",
        ylab="Frecuencia")

barplot(frecuencia, main="Grafico de Barras Simple",
        xlab="Procedencia del Auto",
        ylab="Frecuencia", 
        horiz = TRUE)

boxplot(auto$mpg)

boxplot(mpg ~ foreign, data = auto,
        ylab = "mpg", 
        frame = FALSE, col = "lightgray")

library(ggplot2)
ggplot(auto,aes(foreign)) + geom_bar(fill="lightblue")

ggplot(auto,aes(foreign))+geom_bar(fill="lightblue",colour="black")

ggplot(auto,aes(mpg))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(auto,aes(mpg))+geom_histogram(bins = 5)

ggplot(auto,aes(mpg))+geom_histogram(bins = 5) + facet_grid(foreign ~ .)

ggplot(auto,aes(mpg))+geom_histogram(bins = 5,fill="blue") + facet_grid(foreign ~ .)

ggplot(auto,aes(mpg))+geom_line(stat="density")

ggplot(auto,aes(mpg,fill=foreign))+geom_line(stat="density")

auto$foreign<-factor(auto$foreign)
ggplot(auto,aes(mpg,colour=foreign))+geom_line(stat="density")

ggplot(auto,aes(mpg,colour=foreign))+geom_density()

ggplot(auto,aes(mpg,fill=foreign))+geom_density(alpha=.3)

ggplot(auto,aes(weight,mpg))+geom_point()

ggplot(auto,aes(weight,mpg))+geom_smooth(method=lm) + geom_point()

ggplot(auto,aes(weight,mpg,color=foreign))+geom_point()

ggplot(auto,aes(weight,mpg,color=foreign))+geom_smooth(method=lm)

ggplot(auto,aes(weight,mpg,color=foreign))+geom_point()+geom_smooth(method=lm)

ggplot(auto, aes(x = weight, y = mpg))+
  geom_point(aes(color = foreign))+               
  geom_smooth(aes(color = foreign, fill = foreign))+
  facet_wrap(~foreign, ncol = 3, nrow = 1)+
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))+
  scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

library("ggpubr")
## Loading required package: magrittr
ggdensity(auto, x = "weight",
   add = "mean", rug = TRUE,             # Add linea media y marginal
   color = "foreign", fill = "foreign",  # Color por grupos
   palette = "jco")                      # uso de jco journal color palette

ggboxplot(
  auto, x = "foreign", y = "weight",
  color = "foreign", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
  add = "jitter"
  )+
  stat_compare_means(comparisons =list( c("Foreign","Domestic")))

ggboxplot(auto, x = "foreign", y = "weight",
          title = "Diagrama de Cajas", ylab = "Expression",
          color = "foreign", palette = "jco")

ggboxplot(auto, x = "foreign",
          y = c("mpg", "weight", "price"),
          combine = TRUE,
          ylab = "Comparación",
          color = "foreign", palette = "jco")

ggstripchart(auto, x = "foreign",
             y = c("mpg", "weight", "price"),
             combine = TRUE, 
             color = "foreign", palette = "jco",
             size = 0.1, jitter = 0.2,
             ylab = "Comparación", 
             add = "median_iqr",
             add.params = list(color = "gray"))