Graficas para datos categóricos y series de tiempo
Integrantes :
Juan Camilo Perdomo Escalante-Jhon Hernadez Bello-Guisell Llanos Pabón-Laura Rodriguez Vargas
Gráficos de barras
Ejemplo 1 : Ranking de empresas en Florida central 2003
library(ggplot2)
df <- data.frame(Empresa =
c("Disney World",
"Florida Hospital",
"Publix Supermarkets Inc",
"Walmart Stores Ind",
"Univaersal Orlando"),
Asalariados =
c(51600,
19283,
14995,
14995,
12000))
knitr::kable(df)
| Disney World |
51600 |
| Florida Hospital |
19283 |
| Publix Supermarkets Inc |
14995 |
| Walmart Stores Ind |
14995 |
| Univaersal Orlando |
12000 |
ggplot(data=df, aes(x=Empresa, y=Asalariados)) + geom_bar(stat="identity")

ggplot(data=df, aes(x=Empresa, y=Asalariados)) + geom_bar(stat="identity") + coord_flip()

Ejemplo 2 :
Gráfico de barras agrupado
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
specie <- c(rep("sorgho" , 3),
rep("poacee" , 3),
rep("banana" , 3),
rep("triticum" , 3))
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie, condition, value)
knitr::kable(data)
| sorgho |
normal |
16.604052 |
| sorgho |
stress |
7.477308 |
| sorgho |
Nitrogen |
3.135098 |
| poacee |
normal |
2.656277 |
| poacee |
stress |
5.447811 |
| poacee |
Nitrogen |
28.872625 |
| banana |
normal |
10.468886 |
| banana |
stress |
8.115285 |
| banana |
Nitrogen |
9.459808 |
| triticum |
normal |
9.336181 |
| triticum |
stress |
3.981274 |
| triticum |
Nitrogen |
3.741018 |
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="dodge", stat="identity")

Gráfico de barras apilado
library(ggplot2)
library(dplyr)
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="stack", stat="identity")

Ejemplo 3 :
A continuación se presentan respectivamente los gráficos de barra agrupado y apilado correspondientes a la cantidad de estudiantes matriculados en tres especialidades de administración de empresas en los años 2000 y 2005.
df <- data.frame(Especialidad = c(rep("Finanzas", 2),
rep("Marketing", 2),
rep("Contabilidad", 2)),
Año = rep(c(2000, 2005), 3),
Matriculados = c(160, 250,
140, 200,
100, 150))
knitr::kable(df)
| Finanzas |
2000 |
160 |
| Finanzas |
2005 |
250 |
| Marketing |
2000 |
140 |
| Marketing |
2005 |
200 |
| Contabilidad |
2000 |
100 |
| Contabilidad |
2005 |
150 |
ggplot(df, aes(fill = Especialidad, y = Matriculados, x = Año)) +
geom_bar(position="dodge", stat="identity")

ggplot(df, aes(fill = Especialidad, y = Matriculados, x = Año)) +
geom_bar(position="stack", stat="identity")

Gráfico circular
Ejemplo 4 :
El gerente de una universidad pidió una desagregación de los gastos de viaje de los profesores que asistían a diversas reuniones profesionales. Se observó que el 31 por ciento de los gastos estaba representado por los costes de transporte, el 25 por ciento por los costes de alojamiento, el 12 por ciento por los gastos de alimentación, el 20 por ciento por los gastos de matrícula y el resto por costes varios. Represente gráficamente estos datos.
library(ggplot2)
library(dplyr)
data <- data.frame(group =
c("Transporte",
"Alojamiento",
"Alimentaión",
"Gastos de matricula",
"Varios"),
value = c(31, 25, 12, 20, 12))
knitr::kable(data)
| Transporte |
31 |
| Alojamiento |
25 |
| Alimentaión |
12 |
| Gastos de matricula |
20 |
| Varios |
12 |
ggplot(data, aes(x = "", y = value, fill=group)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0)

library(ggplot2)
library(dplyr)
data <- data %>%
arrange(desc(group)) %>%
mutate(prop = value / sum(data$value) *100) %>%
mutate(ypos = cumsum(prop)- 0.5*prop )
require(scales)
## Loading required package: scales
ggplot(data, aes(x="", y = prop, fill=group)) +
geom_bar(stat="identity", width=1, color="white") +
coord_polar("y", start=0) +
theme_void() +
theme(legend.position="none") +
geom_text(aes(y = ypos, label = percent(value/100)), color = "white", size=6) +
scale_fill_brewer(palette="Set1")

Diagrama de pareto
Ejemplo 5 :
Identificar las principales causas de los problemas e intentar corregirlas rápidamente con un coste mínimo a menudo.
df <- data.frame(Error =
c("Códigos de procedimientos y diagnósticos",
"Información de proveedor",
"Información del paciente",
"Tablas de precios",
"Solicitudes de contratos",
"Ajustes de los proveedores",
"Otros"),
Frecuencia =
c(40, 9, 6, 17, 37, 7, 4))
knitr::kable(df)
| Códigos de procedimientos y diagnósticos |
40 |
| Información de proveedor |
9 |
| Información del paciente |
6 |
| Tablas de precios |
17 |
| Solicitudes de contratos |
37 |
| Ajustes de los proveedores |
7 |
| Otros |
4 |
library(ggQC)
library(ggplot2)
ggplot(df, aes(x = Error, y = Frecuencia)) +
stat_pareto(point.color = "red",
point.size = 3,
line.color = "black",
bars.fill = c("blue", "orange")) +
theme(axis.text.x = element_text(angle = 10))

Diagrama de tallo y hojas
Ejemplo 6 :
x<-25
stem(x, scale = 1, width = 80, atom = 1e-08)
df <- data.frame(calificaciones = c(78, 67, 65, 87, 75, 65, 71, 54, 94, 64, 84, 82, 81,
68, 85, 76, 89, 98, 59, 57, 79, 65, 59, 80, 67))
head(df)
## calificaciones
## 1 78
## 2 67
## 3 65
## 4 87
## 5 75
## 6 65
knitr::kable(head(df))
stem(df$calificaciones)
##
## The decimal point is 1 digit(s) to the right of the |
##
## 5 | 4799
## 6 | 4555778
## 7 | 15689
## 8 | 0124579
## 9 | 48
head(ChickWeight)
## weight Time Chick Diet
## 1 42 0 1 1
## 2 51 2 1 1
## 3 59 4 1 1
## 4 64 6 1 1
## 5 76 8 1 1
## 6 93 10 1 1
knitr::kable(head(ChickWeight))
| 42 |
0 |
1 |
1 |
| 51 |
2 |
1 |
1 |
| 59 |
4 |
1 |
1 |
| 64 |
6 |
1 |
1 |
| 76 |
8 |
1 |
1 |
| 93 |
10 |
1 |
1 |
stem(ChickWeight$weight)
##
## The decimal point is 1 digit(s) to the right of the |
##
## 2 | 599999999
## 4 | 00000111111111111111111112222222222222223333456678888888899999999999+38
## 6 | 00111111122222222333334444455555666677777888888900111111222222333334+8
## 8 | 00112223344444455555566777788999990001223333566666788888889
## 10 | 0000111122233333334566667778889901122223445555667789
## 12 | 00002223333344445555667788890113444555566788889
## 14 | 11123444455556666677788890011234444555666777777789
## 16 | 00002233334444466788990000134445555789
## 18 | 12244444555677782225677778889999
## 20 | 0123444555557900245578
## 22 | 0012357701123344556788
## 24 | 08001699
## 26 | 12344569259
## 28 | 01780145
## 30 | 355798
## 32 | 12712
## 34 | 1
## 36 | 13
stem(ChickWeight$weight, scale = 2)
##
## The decimal point is 1 digit(s) to the right of the |
##
## 3 | 599999999
## 4 | 00000111111111111111111112222222222222223333456678888888899999999999
## 5 | 000000111111112222333334445555566667778888899999
## 6 | 001111111222222223333344444555556666777778888889
## 7 | 0011111122222233333444444446667778889999
## 8 | 0011222334444445555556677778899999
## 9 | 0001223333566666788888889
## 10 | 00001111222333333345666677788899
## 11 | 01122223445555667789
## 12 | 0000222333334444555566778889
## 13 | 0113444555566788889
## 14 | 1112344445555666667778889
## 15 | 0011234444555666777777789
## 16 | 0000223333444446678899
## 17 | 0000134445555789
## 18 | 1224444455567778
## 19 | 2225677778889999
## 20 | 01234445555579
## 21 | 00245578
## 22 | 00123577
## 23 | 01123344556788
## 24 | 08
## 25 | 001699
## 26 | 12344569
## 27 | 259
## 28 | 0178
## 29 | 0145
## 30 | 35579
## 31 | 8
## 32 | 127
## 33 | 12
## 34 | 1
## 35 |
## 36 | 1
## 37 | 3
Gráfico de series temporales
Ejemplo 7 :
Considere la siguiente serie de tiempo correspondiente a la evolución de la temperatura global en superficie con respecto a las temperaturas medias de 1951-1980, fuente NASA/GISS.
library(readr)
##
## Attaching package: 'readr'
## The following object is masked from 'package:scales':
##
## col_factor
library(knitr)
df <- read_csv("https://raw.githubusercontent.com/lihkir/AnalisisEstadisticoUN/main/Data/annual_csv.csv")
## Rows: 274 Columns: 3
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (1): Source
## dbl (2): Year, Mean
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
knitr::kable(head(df))
| GCAG |
2016 |
0.9363 |
| GISTEMP |
2016 |
0.9900 |
| GCAG |
2015 |
0.8998 |
| GISTEMP |
2015 |
0.8700 |
| GCAG |
2014 |
0.7408 |
| GISTEMP |
2014 |
0.7400 |
library(ggplot2)
library(dplyr)
ggplot(df, aes(x = Year, y = Mean)) +
geom_line(color="#69b3a2", size = 1) +
xlab("Year")
