Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Created February 23, 2017 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisDVA/3e45462f654e9717e37dffbc86ded4aa to your computer and use it in GitHub Desktop.
Save luisDVA/3e45462f654e9717e37dffbc86ded4aa to your computer and use it in GitHub Desktop.
# crear tabla de datos
ventas <- data.frame(vend = LETTERS[sample(15,replace=F)],
artsVend = sample(9:70, 15, replace = T),
totalv = sample(300:7900, 15, replace=T))
# cargar paquetes adicionales (instalar primero si es necesario)
library(ggplot2)
devtools::install_github("hrbrmstr/ggalt")
devtools::install_github("hrbrmstr/hrbrthemes")
library(ggalt)
library(gridExtra)
library(ggrepel)
library(hrbrthemes)
library(extrafont)
# para que se puedan usar más tipos de letras
loadfonts(device="win")
# graficar no. de transacciones
artslp <-
ggplot(ventas,aes(x=reorder(vend,artsVend),y=artsVend))+
geom_lollipop(point.colour = "#8D96E8")+coord_flip()+
labs(title="Número de transacciones por vendedor",
x="vendedor", y="no. de transacciones",
caption="*vendedores organizados según el no. de transacciones")+
theme_ipsum(grid = "X")
# graficar total de ventas
totlp <-
ggplot(ventas,aes(x=reorder(vend,artsVend),y=totalv))+
geom_lollipop(point.colour = "#C0C9FF")+coord_flip()+
labs(title="Ventas totales",
x="vendedor", y="total ventas (MXN)",
caption="*vendedores organizados según el no. de transacciones")+
theme_ipsum(grid="X")
# ponerlos lado a lado
grid.arrange(artslp,totlp,nrow=1)
# calcular monto por transacción
ventas$porTran <- ventas$totalv/ventas$artsVend
# graficarlo
ggplot(ventas,aes(x=reorder(vend,artsVend),y=porTran))+
geom_lollipop(point.colour = "#F28DCA")+coord_flip()+
labs(x="vendedor", y="$ por transacción", title="monto por transacción",
caption="*vendedores organizados según el no. de transacciones")+
theme_ipsum(grid="X")
# 'bubble' chart
ggplot(ventas, aes(x=reorder(vend, -artsVend), y=artsVend,fill=totalv,label=totalv))+
geom_point(aes(size=sqrt(totalv/pi)), pch=21, show.legend = F)+
scale_size_continuous(range=c(1, 19))+
labs(x="vendedor",y="no. de transacciones",title="ventas y totales",
subtitle="el tamaño y color de los círculos refleja las ventas totales")+
scale_fill_gradient(low="#f6f4f7",high="#60385c")+
theme_ipsum()+geom_text_repel( point.padding = unit(1.2, "lines"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment