En esta clase aprenderemos a crear gráficos en Barras en R.  Abajo tienes el código en R del video( Descarga los datos que usan en el tutorial aquí)

#VIDEO GRAFICO DE BARRAS

#Para crear un gráfico de barras en R,
#puedes usar la función de R base barplot

#1 creamos tabla


#pib per capita españa en datacommons

pib_mexico_per <- read.csv(«C:\\Users\\Victor-PC\\Desktop\\ECONOMIA_HISPANA\\pib_per_capita_mexico.csv»,header=TRUE, stringsAsFactors=FALSE)


pib_españa_per <- read.csv(«C:\\Users\\Victor-PC\\Desktop\\ECONOMIA_HISPANA\\pib_per_capita_españa.csv»,header=TRUE, stringsAsFactors=FALSE)


pib_argentina_per <- read.csv(«C:\\Users\\Victor-PC\\Desktop\\ECONOMIA_HISPANA\\pib_per_capita_argentina.csv»,header=TRUE, stringsAsFactors=FALSE)


pib_chile_per <- read.csv(«C:\\Users\\Victor-PC\\Desktop\\ECONOMIA_HISPANA\\pib_per_capita_chile.csv»,header=TRUE, stringsAsFactors=FALSE)

 


PIB_ESPAÑA <- head(pib_españa_per$PIB.per.cÃ.pita)[1]
PIB_ESPAÑA

PIB_ARGENTINA <- head(pib_argentina_per$PIB.per.cÃ.pita)[1]
PIB_ARGENTINA

PIB_CHILE <- head(pib_chile_per$PIB.per.cÃ.pita)[1]
PIB_CHILE

PIB_MEXICO <- head(pib_mexico_per$PIB.per.cÃ.pita)[1]
PIB_MEXICO

#paises <- c ( «ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»)

PIB_paises<- c(PIB_ESPAÑA,PIB_CHILE,PIB_ARGENTINA,PIB_MEXICO)

#rownames(PIB_paises) <- c(«ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»)
PIB_paises

#USAREMMOS LA FUNCION barplot

?barplot

#La función barplot en R
# grafico en frecuencia absoluta y porcentaje
#1 Título, etiquetas y colores del gráfico de barras
#2 Cambiar las etiquetas de cada grupo
#3 Espacio y ancho de las barras
#4 Gráfico de barras horizontal en R
# Leyenda del gráfico de barras

 

# Gráfico de barras de frecuencia absoluta

barplot(PIB_paises, main = «PIB PER CÁPITA»,
col = rainbow(4))

barplot(PIB_paises, main = «PIB PER CÁPITA»,
col = c(«green»,»blue»,»red»,»yellow»))

#CAMBIAMOS LAS ETIQUETAS DE CADA GRUPO

barplot(PIB_paises, main = «PIB PER CÁPITA»,
col = rainbow(4),names.arg = c(«ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»))

#◙ Graficos en porcentajes ( frencuencia relativa)

barplot( prop.table(PIB_paises)*100, main = «PIB PER CÁPITA»,
col = rainbow(4),names.arg = c(«ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»))


# Añadir texto

grafico_p<- barplot(PIB_paises, main = «PIB PER CÁPITA»,
col = rainbow(4),names.arg = c(«ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»))

text(grafico_p,PIB_paises-500 ,labels=PIB_paises)


#añadir rejilla con la función grid

grid(nx = NA, ny = NULL, lwd = 1, lty = 1, col = «black»)
grafico_p<- barplot(PIB_paises, main = «PIB PER CÁPITA»,
col = rainbow(4),names.arg = c(«ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»),add = TRUE)

# títulos y etiequetas X e y

barplot(PIB_paises,
main = «PIB PER CÁPITA»,
xlab= «Paises»,
ylab= «Dólares»,
col = rainbow(4),names.arg = c(«ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»))


# CAMBIAR TAMAÑO DE LAS BARRAS

barplot(PIB_paises,
main = «PIB PER CÁPITA»,
xlab= «Paises»,
ylab= «Dólares»,
width=c(0.9, 0.1, 0.4,0.2),
col = rainbow(4),names.arg = c(«ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»))

# GRAFICOS DE BARRAS EN HORIZONTAL

barplot(PIB_paises,
main = «PIB PER CÁPITA»,
#xlab= «Paises»,
#ylab= «Dólares»,
#width=c(0.3, 1, 0.4,0.2),
col = rainbow(4),
names.arg = c(«ESPAÑA»,»CHILE»,»ARGENTINA»,»MEXICO»),
horiz= TRUE)



VA<-VADeaths

mp <- barplot(VADeaths) # default
tot <- colMeans(VADeaths)
text(mp, tot + 3, format(tot), xpd = TRUE, col = «blue»)

barplot(VADeaths, beside = TRUE,
col = c(«lightblue», «mistyrose», «lightcyan»,
«lavender», «cornsilk»),
legend.text = rownames(VADeaths), ylim = c(0, 100))

title(main = «Death Rates in Virginia», font.main = 4)

 

Por Victor A.Rico

Diplomado en Ciencias Empresariales y Operador del Mercado Español de Futuros y Opciones

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *