Created
November 3, 2024 21:30
-
-
Save nosrednawall/8547740abe686004b7791cc029628e4a to your computer and use it in GitHub Desktop.
Exemplo de aplicação de Gráficos com a biblioteca ggplot e utilizando grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################### | |
########## Dados | |
########################################################################### | |
require(dplyr) | |
require(ggplot2) | |
library(tidyverse) | |
require(tidyr) | |
require(data.table) | |
setwd("/home/anderson/git/Educacional/UFPR_Estatistica/2 Periodo/CE303 - VISUALIZAÇÃO DE DADOS APLICADA/Desafios/003/") | |
atletas <- fread("Data/dataset_olympics.csv") | |
jogos_2016 <- atletas %>% | |
filter(Games %in% "2016 Summer") | |
apenas_com_medalhas <- jogos_2016 %>% | |
filter(Medal != "") | |
glimpse(apenas_com_medalhas) | |
######################################################################### | |
########## Definindo tema | |
########################################################################### | |
COR.1="black" | |
COR.2="#004B88" | |
COR.3="#70BE44" | |
COR.4="#FFD005" | |
COR.5="#03A6DD" | |
COR.6="#EE2E4E" | |
meutema <- function(){ | |
theme_bw()+ | |
theme( | |
axis.line = element_line(colour = "black"), # Adiciona uma linha ao redor do gráfico | |
axis.title = element_text(size=20, family = "Iosevka",colour=COR.2), | |
axis.text = element_text(size=20, family = "Iosevka",colour=COR.1), | |
plot.title = element_text(size=25, family = "Iosevka",colour=COR.1,face="bold",hjust=0.5), | |
plot.background = element_rect(fill=NA,colour=NA), | |
panel.background = element_rect(fill=NA,colour=NA), | |
strip.background = element_rect(fill=COR.1), | |
strip.text=element_text(family="Iosevka",size=15)) | |
} | |
######################################################################### | |
########## Graficos | |
########################################################################### | |
#################### Grafico 1 ######################## | |
g1 <- apenas_com_medalhas %>% | |
group_by(NOC) %>% | |
summarise( | |
n = n() | |
) %>% | |
top_n(20, n) %>% | |
ggplot(aes(x = NOC, y = n, label = n)) + | |
ylab("Qtde Medalhas") + | |
xlab("Países") + | |
ggtitle("Top 20 paises") + | |
geom_bar(stat = "identity") + | |
geom_label() + | |
coord_flip() + | |
theme_bw() + | |
meutema() + | |
theme( | |
) | |
g1 | |
################# GRÁFICO 2 ######################### | |
g2 <- apenas_com_medalhas %>% | |
ggplot(aes(x=Height, y = Weight, col = factor(Sex)))+ | |
ggtitle("Peso e Altura por Sexo") + | |
xlab("Altura(cm)") + | |
ylab("Peso(Kg)") + | |
geom_point(size=2) + | |
scale_color_manual("Sexo", labels = c("Feminino", "Masculino"), values = c(COR.4, COR.3)) + | |
theme_bw() + | |
meutema() + | |
theme( | |
text = element_text(size = 20), | |
legend.position = "bottom", | |
) | |
g2 | |
################ GRÁFICO 3 ########################### | |
mu <- plyr::ddply(apenas_com_medalhas, "Sex", summarise, grp.mean=mean(Age)) | |
g3 <- apenas_com_medalhas %>% | |
ggplot(aes(x=Age, fill = Sex))+ | |
ggtitle("Densidade de Idade por Sexo") + | |
geom_density(alpha=0.4)+ | |
geom_vline(data = mu, aes(xintercept = grp.mean, colour = Sex), linetype="dashed")+ | |
xlab("Idade") + | |
ylab("Densidade") + | |
scale_fill_manual("Sexo", labels = c("Feminino", "Masculino"), values = c(COR.4, COR.3)) + | |
scale_color_manual("Sexo", labels = c("Feminino", "Masculino"), values = c(COR.2, COR.6)) + | |
meutema() + | |
theme( | |
text = element_text(size = 20), | |
legend.position = "bottom", | |
) | |
g3 | |
#################### GRÁFICO 4 ###################################### | |
g4 <- ggplot(apenas_com_medalhas, aes(x = Medal, y = Height, fill = Medal)) + | |
geom_boxplot() + | |
labs(title = "Peso dos Atletas por Medalha", | |
x = "Medalha", | |
y = "Peso (kg)") + | |
scale_x_discrete(labels = c("Bronze","Ouro", "Prata" ))+ | |
scale_fill_manual(values = c(COR.5, COR.3, COR.4)) + | |
theme_bw()+ | |
meutema()+ | |
theme( | |
legend.position = "none" ) | |
############################################################################ | |
###### Grid GGPlot Layout | |
############################################################################# | |
#NOVAS FONTS NO R | |
require(extrafont) | |
#install.packages("png") | |
library(png) | |
library(grid) | |
# Defina o caminho correto para o diretório | |
setwd("/home/anderson/git/Educacional/UFPR_Estatistica/2 Periodo/CE303 - VISUALIZAÇÃO DE DADOS APLICADA/Desafios/003/") | |
# Importando imagens | |
imag1 <- readPNG("Imagens/background7.png") | |
im1 <- rasterGrob(imag1, width = unit(297, "mm"), height = unit(420, "mm")) | |
imag2 <- readPNG("Imagens/logo.png") | |
im2 <- rasterGrob(imag2, width = unit(70, "mm"), height = unit(70, "mm")) | |
# Criando painel de gráficos | |
png("painel.png", width = 297, height = 420, units = "mm", res = 500) | |
grid.newpage() | |
# Cabeçalho e textos | |
pushViewport(viewport(layout = grid.layout(1, 1))) | |
grid.draw(im1) | |
grid.text("CE303 2024.2", x = unit(297/2 + 20, "mm"), y = unit(390, "mm"), hjust = 1, | |
gp = gpar(fontfamily = "Iosevka", col = COR.2, cex = 4)) | |
grid.text("Anderson José S. Inácio", x = unit(297/2 + 20, "mm"), y = unit(370, "mm"), hjust = 1, | |
gp = gpar(fontfamily = "Iosevka", col = COR.1, cex = 2.7)) | |
grid.text("Universidade Federal do Paraná", x = unit(297/2 + 20, "mm"), y = unit(350, "mm"), hjust = 1, | |
gp = gpar(fontfamily = "Iosevka", col = COR.2, cex = 2)) | |
grid.text("Curitiba, 4 de Novembro de 2024", x = unit(297/2 + 20, "mm"), y = unit(340, "mm"), hjust = 1, | |
gp = gpar(fontfamily = "Iosevka", col = COR.1, cex = 2)) | |
# Incluindo gráficos | |
pushViewport(viewport(layout = grid.layout(16, 14))) | |
print(g1, vp = viewport(layout.pos.row = 5:15, layout.pos.col = 2:7)) | |
print(g2, vp = viewport(layout.pos.row = 5:8, layout.pos.col = 8:13)) | |
print(g3, vp = viewport(layout.pos.row = 9:12, layout.pos.col = 8:13)) | |
print(g4, vp = viewport(layout.pos.row = 13:15, layout.pos.col = 8:13)) | |
# Incluindo logo | |
pushViewport(viewport(layout.pos.row = 3, layout.pos.col = 9:14)) | |
grid.draw(im2) | |
dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment