Skip to content

Instantly share code, notes, and snippets.

@robintux
Last active October 28, 2020 03:27
Show Gist options
  • Save robintux/fc57122ab8d0ddddcdda3d4710f5e041 to your computer and use it in GitHub Desktop.
Save robintux/fc57122ab8d0ddddcdda3d4710f5e041 to your computer and use it in GitHub Desktop.
#### Ejempo 1 : Salarios ####
rm(list=ls())
# Cargamos los paquetes y data
library(ggplot2)
library(car)
data(Salaries)
str(Salaries)
help("Salaries")
# Creacion del grafico : Agregamos la data y configuramos las variables
ggplot(data = Salaries, aes(x=yrs.service, y=salary))
# Creamos un diagrama de dispersion
p <- ggplot(data = Salaries, aes(x=yrs.service, y=salary))
p + geom_point()
# Modifiquemos el color de los pares ordenamos , para ello utilizamos la variables sexo
p <- ggplot(data = Salaries, aes(x=yrs.service, y=salary,fill = sex)) # modificamos la estetica
# Grafico 1
p + geom_point(shape = 25, size = 1.8)
# Grafico 2
p + geom_point(aes(shape=sex, color=sex) , size = 3)
# Grafico 3
p + geom_point(aes(shape=sex,col = sex), size = 2.5) +
scale_shape_manual(values=c(3, 16))+
scale_color_manual(values=c('red','#E69F00'))+
theme(legend.position="top")
# Grafico 4
p + geom_point(aes(shape=sex,col = sex, size = sex)) +
scale_shape_manual(values=c(3, 16))+
scale_color_manual(values=c('red','#E69F00'))+
scale_size_manual(values=c(3,1)) +
theme(legend.position="right")
# Grafico 5
p + geom_point(aes(shape=sex,col = sex, size = sex)) +
scale_shape_manual(values=c(3, 16))+
scale_color_manual(values=c('black','#E69F00'))+
scale_size_manual(values=c(3,1)) +
theme(legend.position="right") +
scale_y_continuous(labels = scales::scientific)
# Grafico6
# Puede agregar líneas de regresión capa por capa usando la función geom_smooth().
p + geom_point(aes(shape=sex,col = sex, size = sex)) +
scale_shape_manual(values=c(3, 16))+
scale_color_manual(values=c('black','#E69F00'))+
scale_size_manual(values=c(3,1)) +
theme(legend.position="right") +
scale_y_continuous(labels = scales::scientific)+
geom_smooth()
# Grafico 7 : Separamos el grafico en dos ventanas (sex : Female - Male)
p + geom_point(aes(shape=sex,col = sex, size = sex)) +
scale_shape_manual(values=c(3, 16))+
scale_color_manual(values=c('black','#E69F00'))+
scale_size_manual(values=c(3,1)) +
theme(legend.position="right") +
scale_y_continuous(labels = scales::scientific)+
geom_smooth(method = "lm", formula = y~poly(x,2))+
facet_grid(~sex)
# Grafico 8
ggplot(Salaries, aes(x=yrs.since.phd, y=salary, color=rank))+
geom_point() +
geom_smooth(method="lm", colour="black", size=0.5)+
facet_grid(~sex)
# Grafico 9
ggplot(Salaries, aes(x=yrs.since.phd, y=salary, colour=rank))+
geom_point() +
geom_smooth(aes(group=rank), method="lm", color = "black", size=0.5)+
facet_grid(~sex)
# Grafico 10
ggplot(Salaries, aes(x=yrs.since.phd, y=salary, fill=rank))+
geom_point(shape=21) +
geom_smooth(method="lm",
color="black",
size=0.5,
se = FALSE)+
facet_grid(~sex)
# Grafico 11
ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary,
linetype=sex,
shape=sex,
color=sex)) +
geom_smooth(method=lm, formula=y~poly(x,2),se=FALSE, size=1) +
geom_point(size=2)
# Grafico 12
ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary, color=rank)) +
scale_color_manual(values=c("orange", "olivedrab", "navy")) +
geom_point(size=2)
# Grafico 13
ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary, color=rank)) +
scale_color_brewer(palette="Set1") + geom_point(size=2)
# Grafico 14
p1 <- ggplot(data=Salaries, aes(x=rank)) + geom_bar()
p2 <- ggplot(data=Salaries, aes(x=sex)) + geom_bar()
p3 <- ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary)) + geom_point()
library(gridExtra)
grid.arrange(p1, p2, p3, ncol=3)
# Grafico 15
# Usemos datos simulados
set.seed(2020)
data1 <- data.frame(x = round(runif(2000), 2),
y = round(runif(2000), 2),
group = c(rep("A", 1000), rep("B", 1000)))
data2 <- data.frame(x = round(rnorm(2000), 2),
y = round(rnorm(2000), 2),
group = c(rep("A", 1000), rep("B", 1000)))
# creamos dos objetos ggplot sin leyenda
ggp1 <- ggplot(data1, aes(x = x, y = y, group = group, col = group)) +
geom_point() +
theme(legend.position = "none")
ggp2 <- ggplot(data2, aes(x = x, y = y, group = group, col = group)) +
geom_point() +
theme(legend.position = "none")
# Creamos un grafico con leyenda
ggp1_legend <- ggplot(data1, aes(x = x, y = y, group = group, col = group)) +
geom_point() +
theme(legend.position = "bottom")
# Creamos una funcion,la cual extrae las leyendas de los ggplots
extract_legend <- function(my_ggp) {
step1 <- ggplot_gtable(ggplot_build(my_ggp))
step2 <- which(sapply(step1$grobs, function(x) x$name) == "guide-box")
step3 <- step1$grobs[[step2]]
return(step3)
}
# Usamos nuestra funcion recien creada
shared_legend <- extract_legend(ggp1_legend)
# Dibujamos los graficos con la leyenda compartida
grid.arrange(arrangeGrob(ggp1, ggp2, ncol = 2),
shared_legend, nrow = 2, heights = c(10, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment