ipeadata-lab / ipeaplot

ipeaplot: criando graficos no padrão editorial do Ipea
https://ipeadata-lab.github.io/ipeaplot/
Other
2 stars 0 forks source link

Ajustes em theme_ipea #6

Closed cavalcanti1985 closed 1 year ago

cavalcanti1985 commented 1 year ago
PedroJorge7 commented 1 year ago

Pedro, levantar aqui alguns pontos do código em que tento ajustar cada tópico apontado

Acredito que com ggplot2::theme_gray(base_family` = "Frutiger LT 55 Roman") Consiga generalizar para todo o ggplot2 um padrão. Ai titulo e subtítulo faço manualmente

    # Adjusts the vertical alignment of the y-axis title
    axis.title.y = ggplot2::element_text(family = "Frutiger LT 47 LightCn",vjust = 1.5, size = 8, lineheight = 9.6),
    # Adjusts the vertical alignment of the x-axis title
    axis.title.x = ggplot2::element_text(family = "Frutiger LT 47 LightCn",vjust = -0.4, size = 8, lineheight = 9.6),
    # Sets the appearance of the plot title
    plot.title = ggplot2::element_text(
      # FullName (Frutiger LT 47 Light Condensed). FamillyName (Frutiger LT 47 LightCn)
      family = "Frutiger LT 47 LightCn",
      size = 10, lineheight = 12, hjust = 0,
      margin = margin(0,0,0,0, unit = 'mm')
    ),
    # Sets the appearance of the plot subtitle
    plot.subtitle = ggplot2::element_text(
      # FullName (Frutiger LT Std 57 Condensed). FamillyName (Frutiger LT Std)
      family = "Frutiger LT Std",
      size = 9, lineheight = 10.8, face = "bold", hjust = 0,
      margin = margin(0,0,3,0, unit = 'mm'),
    ),

Aqui ainda fiquei com uma dúvida, pois o editorial pede para mudar o ícone do quadrado e da linha, mas parece que o theme só tem a opção para mudar um

    #change legend key size
    #legend.key.size = unit(2, 'mm'),
    # Set key size
    legend.key.size = unit(5,"mm","line"),

Se rodar os dois juntos fica com erro

cavalcanti1985 commented 1 year ago

@PedroJorge7 Estou recebendo warnings sobre as fontes internas do gráfico:

Warning messages:
1: In grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) :
  font family not found in Windows font database
2: In grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) :
  font family not found in Windows font database
3: In grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) :
  font family not found in Windows font database
4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
6: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
7: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
8: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
9: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
10: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
11: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
PedroJorge7 commented 1 year ago

Fiz um pequeno ajuste. Veja se os warnings persistem

cavalcanti1985 commented 1 year ago

Os warnings desapareceram, mas as fontes não estão sendo reconhecidas pelo ggplot.

Os dois gráficos a seguir foram gerados com e sem as fontes do pacote. Não vejo diferença entre eles:

Rplot01 Rplot02

cavalcanti1985 commented 1 year ago

@PedroJorge7 Produzi esse gráfico usando o pacote showtext: Rplot Perceba que a diferença é grande em relação aos anteriores. E agora está muito mais próximo do padrão de fontes do editorial (apesar de necessotar de um ajuste na dimensão gráfico/titulos).

cavalcanti1985 commented 1 year ago

o ponto agora é como incorporar o uso do showtext no pacote.

rafapereirabr commented 1 year ago

@cavalcanti1985 , voce poderia por favor compartilhar aqui o codigo usado para gerar / reproduzir o grafico ?

cavalcanti1985 commented 1 year ago
  1. Adicionar as fontes ao showtex:
library(showtext)

font_add("title", regular = "/Frutiger-LT-47-Light-Condensed.ttf")
font_add("subtitle", regular = "/Frutiger-LT-57-Light-Condensed.ttf")
font_add("intern", regular = "/Frutiger-LT-55-Roman.ttf")
cavalcanti1985 commented 1 year ago
  1. Alterar o theme:

    theme <- ggplot2::theme(
    # Sets the background color of the panel to white
    panel.background = ggplot2::element_rect(fill = "white", colour = NA),
    # Sets the panel border based on the previous assignment
    panel.border = panel.border,
    # Sets the major grid lines color and size
    panel.grid.major = ggplot2::element_line(colour = "#F4F5F6", size = 1),
    # Sets the minor grid lines color and size
    panel.grid.minor = ggplot2::element_line(colour = "white", size = 1),
    # Hides the major grid lines on the x-axis
    panel.grid.major.x = ggplot2::element_blank(),
    # Sets the position of the legend based on the previous assignment
    legend.position = legend.position,
    # Sets the appearance of the legend key
    legend.key = ggplot2::element_rect(colour = "transparent", fill = "white"),
    # Sets the background color of the facet strip based on the previous assignment
    strip.background = strip.background,
    # Sets the text color of the facet strip based on the previous assignment
    strip.text = strip.text,
    # Sets the appearance of the x-axis line based on the previous assignment
    axis.line.x = axis.line.x,
    # Sets the appearance of the y-axis line based on the previous assignment
    axis.line.y = axis.line.y,
    axis.line.y.right = ggplot2::element_line(colour = "#F4F5F6", size = 1),
    # Sets the appearance of the axis text based on the previous assignment
    axis.text = axis.text,
    axis.ticks.x = axis.ticks.x,
    axis.ticks.y = axis.ticks.y,
    # Sets the appearance of the axis ticks based on the previous assignment
    axis.ticks = axis.ticks,
    # Adjusts the vertical alignment of the y-axis title
    _axis.title.y = ggplot2::element_text(family = "title",vjust = 1.5, size = 8, lineheight = 9.6)_,
    # Adjusts the vertical alignment of the x-axis title
    _axis.title.x = ggplot2::element_text(family = "title",vjust = -0.4, size = 8, lineheight = 9.6),_
    # Sets the appearance of the plot title
    plot.title = ggplot2::element_text(
      # FullName (Frutiger LT 47 Light Condensed). FamillyName (Frutiger LT 47 LightCn)
      _family = "title",_
      size = 10, lineheight = 12, hjust = 0,
      margin = margin(0,0,0,0, unit = 'mm')
    ),
    # Sets the appearance of the plot subtitle
    plot.subtitle = ggplot2::element_text(
      # FullName (Frutiger LT Std 57 Condensed). FamillyName (Frutiger LT Std)
      _family = "subtitle",_
      size = 9, lineheight = 10.8, face = "bold", hjust = 0,
      margin = margin(0,0,3,0, unit = 'mm'),
    ),
    # Spacing between faceted plots
    panel.spacing = unit(4, "mm"),
    # Sets the appearance of the legend text
    legend.text = ggplot2::element_text(size = 7),
    # Set caption position
    plot.caption = element_text(hjust = 0),
    # Set the horizontal alignment of the legend to center
    legend.justification = "center",
    # Set legend spacing y
    #legend.spacing.y = unit(2, 'mm'),
    # Set horizontal and vertical spacing between legend keys (2 right,2 bottom,1 left)
    legend.spacing = margin(0,2,2,1, unit = 'mm'),
    # Set Margin spacing
    legend.margin= margin(t= unit(3, 'mm')),
    #change legend key size
    #legend.key.size = unit(2, 'mm'),
    # Set key size
    legend.key.size = unit(5,"mm","linewidth"),
    # Adjust haste length
    axis.ticks.length = unit(2, "mm"),
    ...
    )
    
cavalcanti1985 commented 1 year ago
  1. Criar o gráfico usando showtext_auto()

    
    library(ggplot2)
    library(dplyr)
    library(ipeaplot)

graph <- abjData::pnud_uf %>% filter(substr(uf,1,1) == "3") %>% select(1:4)

_showtextauto()

ggplot(data = graph, aes(x = ufn, y = espvida, fill = as.character(ano))) + geom_bar(stat="identity", width= 0.5) + scale_fill_ipea(discrete = T) + labs(x="", y="", fill = "", title="GRÁFICO 5", subtitle="Indicadores de infraestrutura das escolas - capitais regionais do Nordeste (2018)", caption = 'Fonte: ipea') + theme_ipea(legend.position = 'bottom', geom = 'bar')

cavalcanti1985 commented 1 year ago

Talvez a segunda parte não tenha ficado muito clara..

Alterei o código do Ipeaplot na minha máquina.

Onde estava "Frutiger-LT-47-Light-Condensed", substituti por "title", Frutiger-LT-57-Light-Condensed por "subtitle" e assim por diante.

Não seria necessário, bastando colocar, em font_add, o nome da família que já estava no código.

Fiz dessa forma pq estava testando outras coisas.

PedroJorge7 commented 1 year ago

Opa pessoal, boa tarde!

Conversei aqui com o Pedro e conseguimos incluir o showtext no pacote. Agora vou fazer apenas alguns ajustes/limpeza de código