Created
February 15, 2020 13:12
-
-
Save eduardofox2/6f41fe76d842eb4a4d89e0337604d03f to your computer and use it in GitHub Desktop.
Supplementary script for a colleague to beautify some plots for a press release regarding the scientific output of Brasil and Hungary of the latest years. [IN PORTUGUESE]
This file contains 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
##############Script escrito por Eduardo G P Fox em 14/02/2019 on RStudio Version 1.1.456 (OSX 10.13.6)################### | |
#Relativo a um artigo enfatizando o impacto da ciência húngara em diversas áreas, comparando ao Brasil e Singapura | |
#Publicado em: https://olivre.com.br/ | |
#idealizado pelo Prof. Marcelo Hermes de Lima (UnB) e Eduardo G P Fox (aspirante ao Butantan) | |
#Por favor enviem comentarios sobre este script para meu site pessoal | |
####(https://ofoxofox.wixsite.com/research) e/ou Twitter @ofoxofox###### | |
#Pacotes R utilizados | |
library(ggplot2) | |
library(reshape2) | |
library(cowplot) | |
library(magick) | |
library(ggsci) | |
library(scales) | |
library(plyr) | |
#Caso faltar no GUI local, utilizar install.packages() para rodar o script abaixo | |
#Caso caracteres em Português não estiverem mostrando corretamente, tem que mudar o locale | |
# no sistema operacional. | |
Sys.setlocale("LC_ALL", "pt_BR.UTF-8") | |
#Nova função para anotar o valor de R^2 e formula dos plots, obtida a partir do link | |
#https://stackoverflow.com/questions/48175294/why-the-lm-eqn-way-of-adding-regression-line-function-did-not-work-in-my-case | |
#Autoria de @Gari Guo & @CMichael | |
lm_eqn <- function(df, y, x){ | |
formula = as.formula(sprintf('%s ~ %s', y, x)) | |
m <- lm(formula, data=df); | |
# formating the values into a summary string to print out | |
# ~ give some space, but equal size and comma need to be quoted | |
eq <- substitute(italic(target) == a + b %.% | |
italic(input)*","~~italic(r)^2~"="~r2*","~~p~"="~italic(pvalue), | |
list(target = y, | |
input = x, | |
a = format(as.vector(coef(m)[1]), digits = 2), | |
b = format(as.vector(coef(m)[2]), digits = 2), | |
r2 = format(summary(m)$r.squared, digits = 3), | |
# getting the pvalue is painful | |
pvalue = format(summary(m)$coefficients[2,'Pr(>|t|)'], digits=1) | |
) | |
) | |
as.character(as.expression(eq)); | |
} | |
#Inserindo os dados, recolhidos pelo IC Matheus, a partir da scimago.com | |
#Organizados e analisados por Marcelo Hermes de Lima | |
dados.Fig.1<-data.frame( | |
Anos=seq(1996,2018,1), | |
BR=c(NA,2.37, 2.37, 2.63, 2.75, 2.25, 2.93, 2.83, 2.89, 3.96, 3.27, | |
3.33, 3.64, 3.64, 2.91, 3.45, 2.62, 2.95, 1.94, 1.59, 1.64, 1.71, | |
1.37), | |
HU=c(2.2, 2.1, 2.6, 2.4, 2.5, 3.5, 4.1, 4.3, 3.8, 4.2, 4.3, 4.5, | |
5.1, 4.5, 5.3, 5, 5.7, 5.7, 5.65, 5.7, 7.5, 7.9, 7), | |
SI=c(4.4, 3.4, 4.5, 3.4, 4.5, 4.5, 4.6, 5.4, 5.1, 6.25, 6.5, 6.5, | |
7.3, 8.2, 8.9, 9, 9.5, 9.5, 9.5, 9.25, 9.4, 9.7, 9.3) | |
) | |
dados.Fig.2<-data.frame( | |
Áreas=c("Bio-Agro","Bioquímica","Farmacologia","Micro e Imuno","Neurociência", | |
"a" , "Medicina","Psicologia","Veterinária", | |
"b" , "Física","Matemática","Meio Ambiente","Quimica","Geologia"), | |
Hungria=c(4.4,6.6,4.2,10,3.2, 0 , 9.6,4.3,2.4, 0 ,10,3.25,5.6,4.1,8.5), | |
Brasil=c(1.3,1.35,2.8,2.3,2.4, 0 , 1.25,0.65,0.8, 0 , 3.9,2.4,1.65,2.3,3.5) | |
) | |
dados.Fig.2_<-melt(dados.Fig.2) | |
dados.Fig.2_$Áreas<-factor(dados.Fig.2_$Áreas,levels=dados.Fig.2$Áreas) | |
dados.Fig.3A<-data.frame( | |
Anos=seq(2008,2018,1), | |
HU=c(3.8, 3.65, 4.65, 4.2, 4.9, 5.9, 5.3, 5.6, 7.5, 9.6, 9), | |
BR=c(1.8, 1.7, 1.85, 2.2, 1.7, 2.2, 2, 1.9, 1.9, 1.25, 1.6) | |
) | |
dados.Fig.3B<-data.frame( | |
Anos=seq(2007,2018,1), | |
HU=c(4.7, 4.35, 4.4, 4.9, 6.3, 9.5, 7.8, 5.7, 8, 9.4, 10, 9.3), | |
BR=c(2.9, 1.5, 2.4, 1.2, 3.2, 1.7, 2, 0.85, 1.95, 3.65, 3.9, 3.1) | |
) | |
dados.Fig.3C<-data.frame( | |
Anos=seq(2012,2018,1), | |
HU=c(2.5, 3.6, 3.2, 4.5, 6.4, 8.5, 9.4), | |
BR=c(2.25, 3.1, 1.8, 3.2, 3.55, 3.5, 4.2) | |
) | |
dados.Fig.4<-data.frame( | |
Áreas=c("Engenharia","Computação","Materiais","Eng. Química","Energia","a", | |
"Ciências Sociais","Artes e Human.","Adm. e Contab.","Economia", "b", "Multidisciplinar"), | |
Hungria=c(3.55,3.3,3.3,2.4,5.3, 0 , 1.95,3.2,5.8,2.3, 0, 4.3), | |
Brasil=c(3.05,2.8,2.1,2.6,2.7, 0 , 0.05,1.1,0.8,1.1, 0 , 3.4) | |
) | |
dados.Fig.4_<-melt(dados.Fig.4) | |
dados.Fig.4_$Áreas<-factor(dados.Fig.4_$Áreas,levels=dados.Fig.4$Áreas) | |
dados.Fig.5<-data.frame( | |
Anos=seq(1996,2018), | |
PIB.PPP=c(15354.64687, 15869.20022, 16526.5085, 17082.36124, 17893.93479, | |
18665.71659, 19606.46052, 20465.82873, 21500.42297, 22457.43917, | |
23399.10945, 23492.14161, 23782.36284, 22223.44986, 22421.69734, | |
22894.34764, 22674.18736, 23183.02905, 24220.73394, 25212.05311, | |
25842.87059, 27031.78002, 28464.5602), | |
rank=c(2.2, 2.1, 2.6, 2.4, 2.5, 3.5, 4.1, 4.3, 3.8, 4.2, 4.3, 4.5, | |
5.1, 4.5, 5.3, 5, 5.7, 5.7, 5.65, 5.7, 7.5, 7.9, 7) | |
) | |
dados.Fig.6<-data.frame( | |
Anos=c(1950,1960, 1973, 1976, 1989, 1998), | |
Czechoslovakia=c(76, 75, 57, 55, 50, 51.5), | |
East_Germany=c(67, 75, 60, 60, 50, 69), | |
Hungary=c(68, 66, 57, 56, 52, 55), | |
Poland=c(45, 43, 38, 36, 32, 38), | |
Romania=c(30, 36, 43, 47, 37, 30), | |
Bulgaria=c(25, 33, 35, 37, 32, 26) | |
) | |
#Fim dos dados brutos | |
#################################********* | |
#Plotando as Figuras segundo estética combinada com o primeiro autor MHL | |
#Imagens de icons obtidas a partir do website https://www.flaticon.com/packs/countrys-flags | |
#Icons made by <a href="https://www.flaticon.com/authors/freepik" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a></div> | |
#Usando ícones de bandeiras baixadas no meu computador (Ajustar locale para o de quem usar este script) | |
brazil_icon<-image_read('/Users/egoncal2/Downloads/brazil.png') | |
hungria_icon<-image_read('/Users/egoncal2/Downloads/hungary.png') | |
singapura_icon<-image_read('/Users/egoncal2/Downloads/singapore.png') | |
Fig.1<-ggplot(dados.Fig.1)+ | |
geom_point(aes(y=BR, x=Anos), shape = 21, fill = "#009C3B", colour="#ffdf00", size = 3)+ | |
geom_smooth(aes(y=BR, x=Anos), method = "loess", se = FALSE, size = 1, span = 1, colour="#ffdf00", linetype = "dotted") + | |
geom_point(aes(y=HU, x=Anos), shape = 21, fill = "#CD2A3E", colour="green", size = 3)+ | |
geom_smooth(aes(y=HU, x=Anos), method = "loess", se = FALSE, size = 1, span = 1, colour="#CD2A3E", linetype = "dotted") + | |
geom_point(aes(y=SI, x=Anos), shape = 21, fill = "white", colour="#EF3340", size = 3)+ | |
geom_smooth(aes(y=SI, x=Anos), method = "loess", se = FALSE, size = 1, span = 1, colour="#EF3340", linetype = "dotted") + | |
scale_x_continuous(limits=c(1996,2018), breaks=seq(1996,2018,3), expand = c(0.1, 0.1))+ | |
theme_cowplot(12)+ | |
labs(title = 'Rank-score\nHungria, Brasil e Singapura', x = NULL, y= NULL)+ | |
theme( | |
legend.position = "none", | |
plot.title = element_text(color="black", size=15, face="bold.italic"), | |
axis.title.x = element_text(size=20, face="bold"), | |
axis.title.y = element_text(size=20, face="bold"), | |
axis.text.y=element_text(size=12), | |
axis.text.x=element_text(size=12)) | |
Final.Fig.1<-ggdraw(Fig.1) + | |
draw_image(singapura_icon, scale=0.08, x=-0.25)+ | |
draw_image(hungria_icon, scale=0.08, x=0.21, y=0.12)+ | |
draw_image(brazil_icon, scale=0.08, x=0.21, y=-0.36) | |
Fig.2<-ggplot(dados.Fig.2_, aes(x=Áreas, y=value, fill = variable))+ | |
geom_bar(stat="identity", position = "dodge", width=0.6)+ | |
scale_fill_manual(values=c("#CD2A3E", "#009C3B"))+ | |
scale_y_continuous(name= NULL,limits=c(0,10), breaks=c(0,2.5,5.0,7.5,10.0), | |
expand = c(0, 0.1))+ | |
theme_light()+ | |
labs(title = 'Rank-scores por área de estudo', x= NULL, font="bold", size=16, fill = "País")+ | |
scale_x_discrete(breaks=melt(dados.Fig.2)$Áreas[nchar(as.character(melt(dados.Fig.2)$Áreas))!=1])+ | |
theme( | |
legend.position = "right", | |
plot.title = element_text(color="red", size=15, face="bold.italic"), | |
axis.title.x = element_text(size=20, face="bold"), | |
axis.title.y = element_text(size=20, face="bold"), | |
axis.text.x=element_text(size=14, angle = 45, hjust = 1), | |
axis.text.y=element_text(size=12)) | |
Fig.3A<-ggplot(dados.Fig.3A)+ | |
geom_smooth(aes(y=HU, x=Anos), size = 1, colour="#CD2A3E", linetype = "dashed", span = 0.3, se=FALSE)+ | |
geom_smooth(aes(y=BR, x=Anos), size = 1, colour="#ffdf00", linetype = "dashed",span = 0.3, se=FALSE)+ | |
geom_point(aes(y=BR, x=Anos), shape = 21, fill = "#009C3B", colour="#ffdf00", size = 3)+ | |
geom_point(aes(y=HU, x=Anos), shape = 21, fill = "#CD2A3E", colour="green", size = 3)+ | |
scale_y_continuous(limits=c(0,10), breaks=seq(0,10,2), expand = c(0.1, 0.1))+ | |
scale_x_continuous(limits=c(2008,2018), breaks=seq(2008,2018,3), expand = c(0.1, 0.1))+ | |
theme_cowplot(12)+ | |
labs(title = "Medicina", x=NULL, y=NULL)+ | |
theme( | |
axis.title.x = element_text(size=14, face="bold"), | |
axis.text.y=element_text(size=13), | |
axis.text.x=element_text(size=13, angle = 45, hjust = 1)) | |
Fig.3B<-ggplot(dados.Fig.3B)+ | |
geom_smooth(aes(x=Anos, y=HU), size = 1, colour="#CD2A3E", span = 2,linetype = "dashed", se=FALSE)+ | |
geom_smooth(aes(x=Anos, y=BR), size = 1, colour="#ffdf00", span = 2,linetype = "dashed", se=FALSE)+ | |
geom_point(aes(x=Anos, y=HU), shape = 21, fill = "#CD2A3E", colour="green", size = 3)+ | |
geom_point(aes(x=Anos, y=BR), shape = 21, fill = "#009C3B", colour="#ffdf00", size = 3)+ | |
scale_y_continuous(limits=c(0,10), breaks=seq(0,10,2), expand = c(0.1, 0.1))+ | |
scale_x_continuous(limits=c(2007,2019), breaks=seq(2007,2019,3), expand = c(0.1, 0.1))+ | |
theme_cowplot(12)+ | |
labs(title = "Física", x=NULL, y=NULL)+ | |
theme( | |
axis.title.x = element_text(size=14, face="bold"), | |
axis.text.y=element_text(size=13), | |
axis.text.x=element_text(size=13, angle = 45, hjust = 1)) | |
Fig.3C<-ggplot(dados.Fig.3C)+ | |
geom_smooth(aes(y=HU, x=Anos), size = 1, colour="#CD2A3E", span = 2, linetype = "dashed", se=FALSE)+ | |
geom_smooth(aes(y=BR, x=Anos), size = 1, colour="#ffdf00", span = 2, linetype = "dashed", se=FALSE)+ | |
geom_point(aes(y=HU, x=Anos), shape = 21, fill = "#CD2A3E", colour="green", size = 3)+ | |
geom_point(aes(y=BR, x=Anos), shape = 21, fill = "#009C3B", colour="#ffdf00", size = 3)+ | |
scale_y_continuous(limits=c(0,10), breaks=seq(0,10,2), expand = c(0.1, 0.1))+ | |
scale_x_continuous(limits=c(2012,2018), breaks=seq(2012,2018,2), expand = c(0.1, 0.1))+ | |
theme_cowplot(12)+ | |
labs(title = "Geologia", x=NULL, y=NULL)+ | |
theme( | |
axis.title.x = element_text(size=14, face="bold"), | |
axis.text.y=element_text(size=13), | |
axis.text.x=element_text(size=13, angle = 45, hjust = 1)) | |
Final.Fig.3<- plot_grid(Fig.3A, Fig.3B, Fig.3C, nrow=1) | |
#Adicionando texto acima : | |
title <- ggdraw() + | |
draw_label( | |
"Rank-scores\nBrasil vs. Hungria", size = 16, | |
fontface = 'bold.italic', | |
x = 0, | |
hjust = 0 | |
) + | |
theme( | |
# add margin on the left of the drawing canvas, | |
# so title is aligned with left edge of first plot | |
plot.margin = margin(0, 0, 0, 7) | |
) | |
plot_grid( | |
title, Final.Fig.3, | |
ncol = 1, | |
# rel_heights values control vertical title margins | |
rel_heights = c(0.1, 1) | |
) | |
Fig.4<-ggplot(dados.Fig.4_, aes(x=Áreas, y=value, fill = variable))+ | |
geom_bar(stat="identity", position = "dodge", width=0.6)+ | |
scale_fill_manual(values=c("#CD2A3E", "#009C3B"))+ | |
scale_y_continuous(name= NULL,limits=c(0,10), breaks=c(0,2.5,5.0,7.5,10.0), | |
expand = c(0, 0.1))+ | |
theme_light()+ | |
labs(title = 'Rank-scores por área de estudo', x= NULL, font="bold", size=16, fill = "País")+ | |
scale_x_discrete(breaks=melt(dados.Fig.4)$Áreas[nchar(as.character(melt(dados.Fig.4)$Áreas))!=1])+ | |
theme( | |
legend.position = "right", | |
plot.title = element_text(color="red", size=15, face="bold.italic"), | |
axis.title.x = element_text(size=20, face="bold"), | |
axis.title.y = element_text(size=20, face="bold"), | |
axis.text.x=element_text(size=14, angle = 45, hjust = 1), | |
axis.text.y=element_text(size=12)) | |
Fig.5A<-ggplot(dados.Fig.5[,c(1,2)],aes(y=PIB.PPP, x=Anos))+ | |
geom_point(colour="black", size = 3)+ | |
scale_y_continuous(limits=c(0,35000), breaks=seq(0,35000,5000), expand = c(0.1, 0.1))+ | |
scale_x_continuous(limits=c(1996,2020), breaks=seq(1996,2020,4), expand = c(0.1, 0.1))+ | |
theme_light()+ | |
labs(title = "Hungria:\nPIB per capita (valor 2011)", x=NULL)+ | |
theme(plot.margin = margin(0, 0.1, 0, 0, "cm"), | |
legend.position = "NULL", | |
plot.title = element_text(color="black", size=16, face="bold"), | |
axis.title.x = element_text(size=14, face="bold"), | |
axis.title.y = element_text(size=14, face="bold",color="white"), | |
axis.text.x=element_text(size=14), | |
axis.text.y=element_text(size=12)) | |
Fig.5B<-ggplot(dados.Fig.5[,c(1,3)],aes(y=rank, x=Anos))+ | |
geom_point(colour="#CD2A3E", size = 3)+ | |
scale_y_continuous(limits=c(0,10), breaks=seq(0,10,2), expand = c(0.1, 0.1))+ | |
scale_x_continuous(limits=c(1996,2020), breaks=seq(1996,2020,4), expand = c(0.1, 0.1))+ | |
theme_light()+ | |
labs(title = "Hungria:\nRank-score", x=NULL)+ | |
theme(plot.margin = margin(0, 0.1, 0, 0, "cm"), | |
legend.position = "NULL", | |
plot.title = element_text(color="black", size=16, face="bold"), | |
axis.title.x = element_text(size=14, face="bold"), | |
axis.title.y = element_text(size=14, face="bold",color="white"), | |
axis.text.x=element_text(size=14), | |
axis.text.y=element_text(size=12)) | |
Fig.5C<-ggplot(dados.Fig.5[,c(2,3)],aes(y=rank, x=PIB.PPP))+ | |
geom_point(colour="blue", size = 3)+ | |
geom_smooth(method="lm", colour="red", alpha=0.1)+ | |
geom_text(x=22000,y=0, label=lm_eqn(dados.Fig.5[,c(2,3)], 'rank', 'PIB.PPP'), parse=T)+ | |
scale_y_continuous(limits=c(0,10), breaks=seq(0,10,2), expand = c(0.1, 0.1))+ | |
scale_x_continuous(limits=c(14000, 31000), expand = c(0.01, 0.01))+ | |
theme_light()+ | |
labs(title = "Hungria\nPIB vs. Rank-score", x=NULL)+ | |
theme(plot.margin = margin(0, 0.1, 0, 0, "cm"), | |
legend.position = "NULL", | |
plot.title = element_text(color="red", size=16, face="bold.italic"), | |
axis.title.x = element_text(size=14, face="bold"), | |
axis.title.y = element_text(size=14, face="bold",color="white"), | |
axis.text.x=element_text(size=14), | |
axis.text.y=element_text(size=12)) | |
#Reparar como compor tamanhos diversos, gradualmente | |
Fig.5AB<-plot_grid(Fig.5A, Fig.5B, nrow=1) | |
Final.Fig.5<-plot_grid(Fig.5AB,Fig.5C, nrow=2) | |
Fig.6<-ggplot(melt(dados.Fig.6, id.vars = "Anos"), aes(fill=as.character(Anos), y=value, x=variable))+ | |
geom_bar(position="dodge", stat="identity", width = 0.8)+ | |
scale_fill_jco()+ | |
scale_x_discrete(name= 'Países', labels = c("Czechoslovakia", "East Germany", "Hungary", "Poland", | |
"Romania", "Bulgaria"), expand = c(0.1, 0.1))+ | |
scale_y_continuous(name= NULL, limits=c(0,100), breaks=c(0,25,50,75,100), expand = c(0, 0))+ | |
labs(title = "Percentual do Trabalho da União Europeia", font="bold", size=20, fill = "Anos")+ | |
theme_light()+ | |
theme( | |
plot.title = element_text(color="red", size=20, face="bold.italic"), | |
axis.title.x = element_text(size=14), | |
axis.text.x=element_text(size=13), | |
axis.text.y=element_text(size=13), | |
legend.title=element_text(size=12)) | |
legend.text=element_text(size=11), | |
axis.text.x=element_text(size=14, angle = 45, hjust = 1)) | |
#********************** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment