Skip to content

Commit e52343a

Browse files
ajustes no theme
1 parent 0855ee2 commit e52343a

10 files changed

Lines changed: 128 additions & 7 deletions

‎NAMESPACE‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
S3method(ggplot_add,scale_auto_ipea)
44
S3method(ggplot_add,scale_ipea_color)
55
S3method(ggplot_add,scale_ipea_fill)
6+
export(ipea_contrast_color)
67
export(ipea_label_size)
78
export(ipea_pal)
89
export(save_ipeaplot)

‎NEWS.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* Fixed `save_ipeaplot()` silently reporting success when a Cairo-based device fails to write a file; it now errors with a clear message.
66
* New palettes `"NT"` and `"TD"`, based on the cover, section-title and chart colours of two Ipea editorial series (Nota Tecnica and Texto para Discussao). `scale_color_ipea()`/`scale_fill_ipea()` switch automatically to their qualitative 12-colour variant for discrete data.
77
* Fixed a color-accuracy bug in `ipea_palette()` affecting every palette: an incorrect string offset silently forced the low nibble of the blue channel to `F` in all generated colours.
8+
* `theme_ipea()`: default `legend.position` changed from `"right"` to `"bottom"`, matching the Ipea Editorial Manual guidelines.
9+
* `theme_ipea()`: `geom_point()` layers in line charts (i.e. plots that also have a `geom_line()`/`geom_path()`/`geom_step()` layer) now default to a solid square marker (`shape = 15`), per the Ipea Editorial Manual guidelines. Does not affect scatter plots or a shape already set by the user.
10+
* New function `ipea_contrast_color()`: given one or more fill/background colours, returns `"white"` or `"black"`, whichever is more readable, for labels drawn over bars/points per the editorial guidelines.
811

912
# ipeaplot 0.5.4
1013
* Fixed the vignette to address issues with geobr.

‎R/ipea_contrast_color.R‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#' @title Contrasting text colour for labels drawn over a fill colour
2+
#'
3+
#' @description Given one or more fill/background colours, returns `"white"`
4+
#' or `"black"`, whichever gives better contrast against that background.
5+
#' Useful for value labels drawn inside bars or over points/lines, matching
6+
#' the Ipea editorial guidelines for graphics, which alternate the colour of
7+
#' data labels according to how dark the colour behind them is. See
8+
#' <https://www.ipea.gov.br/manualeditorial/padroes/padroes-grafico-visuais/ilustracoes-pi.html>.
9+
#'
10+
#' @param background A character vector of colours (hex codes or R colour
11+
#' names) used as the fill/background behind the label.
12+
#'
13+
#' @return A character vector the same length as `background`, containing
14+
#' `"white"` or `"black"`.
15+
#' @export
16+
#' @family ggplot2 theme functions
17+
#'
18+
#' @examples
19+
#' ipea_contrast_color(c("#29112F", "#D2A0C9"))
20+
#'
21+
#' library(ggplot2)
22+
#' df <- data.frame(grupo = c("A", "B"), valor = c(22.6, 4.7),
23+
#' cor = c("#29112F", "#D2A0C9"))
24+
#' ggplot(df, aes(grupo, valor, fill = cor, label = valor)) +
25+
#' geom_col() +
26+
#' geom_text(aes(color = ipea_contrast_color(cor)), size = ipea_label_size(),
27+
#' fontface = "bold") +
28+
#' scale_fill_identity() +
29+
#' scale_color_identity() +
30+
#' theme_ipea()
31+
ipea_contrast_color <- function(background) {
32+
rgb_mat <- grDevices::col2rgb(background) / 255
33+
34+
# Luminancia relativa (WCAG): converte cada canal sRGB para linear antes de
35+
# ponderar pela sensibilidade do olho humano a cada cor.
36+
linear_channel <- function(ch) ifelse(ch <= 0.03928, ch / 12.92, ((ch + 0.055) / 1.055)^2.4)
37+
linear_rgb <- apply(rgb_mat, 2, linear_channel)
38+
luminance <- 0.2126 * linear_rgb[1, ] + 0.7152 * linear_rgb[2, ] + 0.0722 * linear_rgb[3, ]
39+
40+
# 0.179 e o limiar em que o contraste com texto preto e branco se equivale.
41+
ifelse(luminance > 0.179, "black", "white")
42+
}

‎R/theme_ipea.R‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#' axis text will be displayed in black; otherwise, they will
1212
#' be hidden.
1313
#' @param legend.position A character vector specifying the position of the
14-
#' legend. Valid options are `"right"` (default), `"left"`, `"top"`, and
15-
#' `"bottom"`.
14+
#' legend. Valid options are `"bottom"` (default, matching the Ipea
15+
#' editorial guidelines), `"right"`, `"left"`, and `"top"`.
1616
#' @param grid.adjust Defines whether the grid lines should be `"horizontal"`
1717
#' (default) or `"vertical"`.
1818
#' @param x_breaks Numeric. The number of breaks on the x-axis
@@ -48,7 +48,7 @@
4848

4949
theme_ipea <- function(axis_lines = 'full',
5050
axis_values = TRUE,
51-
legend.position = 'right',
51+
legend.position = 'bottom',
5252
grid.adjust = 'horizontal',
5353
x_breaks = NULL, # Valor padrão definido como NULL
5454
y_breaks = NULL, # Valor padrão definido como NULL
@@ -509,6 +509,31 @@ ggplot_add.scale_auto_ipea <- function(object, plot, object_name, ...) {
509509

510510
}
511511

512+
# Marcadores quadrados: o Manual Editorial do Ipea usa marcador quadrado
513+
# solido nos pontos de graficos de linha (ver referencia no topo do
514+
# arquivo). Só aplicado quando o plot tem uma camada de linha (geom_line/
515+
# geom_path/geom_step) junto com a de pontos, para não afetar graficos de
516+
# dispersao (scatter) puros. Não sobrescreve um shape ja definido (fixo ou
517+
# mapeado) pelo usuario.
518+
layer_fn_name <- function(layer) {
519+
call_str <- paste(deparse(layer$constructor), collapse = " ")
520+
sub("\\s*\\(.*$", "", trimws(call_str))
521+
}
522+
layer_fns <- vapply(plot$layers, layer_fn_name, character(1))
523+
has_line_layer <- any(layer_fns %in% c("geom_line", "geom_path", "geom_step"))
524+
525+
if (has_line_layer) {
526+
for (i in seq_along(plot$layers)) {
527+
is_point_layer <- layer_fns[i] == "geom_point"
528+
shape_already_set <- "shape" %in% names(c(plot$mapping, plot$layers[[i]]$mapping)) ||
529+
!is.null(plot$layers[[i]]$aes_params$shape)
530+
531+
if (is_point_layer && !shape_already_set) {
532+
plot$layers[[i]]$aes_params$shape <- 15
533+
}
534+
}
535+
}
536+
512537
plot <- plot + theme
513538

514539

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fig_raw <- ggplot(data = mtcars, aes(x = hp , y = mpg, color = cyl)) +
5555
# Adicionando os padrões do Ipea
5656
fig_ipea <- fig_raw +
5757
scale_color_ipea() +
58-
theme_ipea()
58+
theme_ipea(legend.position = "right")
5959
6060
# Painel lado a lado: com (esquerda) e sem (direita) o ipeaplot
6161
fig_panel <- fig_ipea | fig_raw

‎man/ipea_contrast_color.Rd‎

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/ipea_label_size.Rd‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/scale_color_ipea.Rd‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/scale_fill_ipea.Rd‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/theme_ipea.Rd‎

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)