Bar Charts

The bar chart (BarChart) is a statistical graph that allows us to represent the frequency distribution of discrete qualitative and quantitative variables, our goal is to move from pencil and paper to the use of R programming language and Python.

Data Set - mtcars

mtcars in a very popular data set that is already preloaded in the R programming language, we will focus on the variable cyl (cylinder) for the creation of the statistical line graph mentioned above.

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
summary(mtcars$cyl)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   4.000   4.000   6.000   6.188   8.000   8.000

Bar Charts - barplot()

La función barplot() nos permite crear diagramas de barras (Bar Charts) en el lenguaje de programación R, como se muestra a continuación.

x <- table(mtcars$cyl)

colors <- c("orange","blue","purple")

barplot(x,xlab="Cylinders",ylab="Frequencies",main="Number of Cylinders",col=colors)

Bar Charts - ggplot2

The ggplot2 package allows us to create high-quality statistical graphs in the R programming language, we will use this library to create a Bar Chart using the data set mtcars, as shown below.

library(ggplot2)

ggplot(mtcars,aes(cyl)) + geom_bar(fill=colors) + labs(x="Cylinders",y="Frequencies",title="Number of Cylinders") + theme_dark()

Bar Charts - lattice

Lattice is a package that allows us to create high-quality statistical graphs in the R programming language. We will use this package to create a Bar Chart.

library(lattice)

barchart(x,xlab="Cylinders",ylab="Frequencies",main="NUmber of Cylinders",col=colors,horizontal = FALSE)

Bar Charts - Matplotlib

Matplotlib is a Python library that allows us to create high quality statistical plots, we will focus on the variable cyl (cylinders) to create a bar plot.

import matplotlib.pyplot as plt

x_axis=[4,6,8]

y_axis=[11,7,14]

colors=['orange','blue','purple']

plt.bar(x_axis,height=y_axis,width=0.9,color=colors)

plt.title("Number of Cylinders")

plt.xlabel("Cylinders")

plt.ylabel("Frequencies")

plt.show()

Tutorial - YouTube

A continuación, presentamos un tutorial tomado de YouTube que muestra la configuración de Python en un documento RMarkdown

embed_url("https://www.youtube.com/watch?v=gn8oJ8FMSWY&t=93s") %>% 
  use_align("center")
LS0tDQp0aXRsZTogIkJhciBDaGFydHMiDQphdXRob3I6ICJSZW56byBDw6FjZXJlcyBSb3NzaSINCmRhdGU6ICIyMDIyLzAzLzMwIg0Kc3VidGl0bGU6IFJNYXJrZG93biAtIFIgJiBQeXRob24NCm91dHB1dDoNCiAgaHRtbF9kb2N1bWVudDoNCiAgICB0b2M6IFRSVUUNCiAgICB0b2NfZmxvYXQ6IFRSVUUNCiAgICBjb2RlX2Rvd25sb2FkOiBUUlVFDQotLS0NCg0KYGBge3Igc2V0dXAsIGluY2x1ZGU9RkFMU0V9DQoNCmxpYnJhcnkoZ2dwbG90MikNCg0KbGlicmFyeShsYXR0aWNlKQ0KDQpsaWJyYXJ5K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