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

theme_ipea() issue #45

Closed Joaobazzo closed 6 months ago

Joaobazzo commented 6 months ago

acredito que está relacionado ao #44 Ver reprex

library(ggplot2)
library(ipeaplot)
df <- data.frame(x1 = 1:10,y=1:10,z = c(rep(LETTERS[1],5)
                                        ,rep(LETTERS[2],5)))
df
#>    x1  y z
#> 1   1  1 A
#> 2   2  2 A
#> 3   3  3 A
#> 4   4  4 A
#> 5   5  5 A
#> 6   6  6 B
#> 7   7  7 B
#> 8   8  8 B
#> 9   9  9 B
#> 10 10 10 B
p <- ggplot(df)+
  geom_area(aes(x = x1,group = z,y = y,fill = z),
            linewidth = 0.25
            ,color = "black")
p


p +  ipeaplot::theme_ipea(legend.position = "bottom")
#> Error: object 'x1' not found

Created on 2024-03-13 with reprex v2.1.0

cavalcanti1985 commented 6 months ago

Obrigado, @Joaobazzo, vamos investigar. @PedroJorge7, por favor, coloque na sua fila de ajustes a serem feitos.

cavalcanti1985 commented 6 months ago

@PedroJorge7,

Acho que o problema está neste trecho do script e theme_ipea, alterado recentemente:

# y_var <- plot$mapping$x
  # y_var <- rlang::eval_tidy(y_var, plot$data)

  for (i in seq_along(plot$layers)) {
    if ("fill" %in% names(plot$layers[[i]]$mapping)) {
      x_var <- plot$layers[[i]]$mapping$x
      x_var <- rlang::eval_tidy(x_var, plot$layers[[i]]$data)
      break
    } else {
      x_var <- plot$mapping$x
      x_var <- rlang::eval_tidy(x_var, plot$data)
    }
  }

  for (i in seq_along(plot$layers)) {
    if ("fill" %in% names(plot$layers[[i]]$mapping)) {
      y_var <- plot$layers[[i]]$mapping$y
      y_var <- rlang::eval_tidy(y_var, plot$layers[[i]]$data)
      break
    } else {
      y_var <- plot$mapping$y
      y_var <- rlang::eval_tidy(y_var, plot$data)
    }
  }

  test <- NULL
  for (i in seq_along(plot$layers)) {
    temp <- grepl('bar',plot$layers[[i]]$constructor)
    test <- rbind(test,temp)

  }

  test <- test[grepl('TRUE',test)]

  breaks = args$x_breaks

  if(length(test) > 0 & expand_y_limit == TRUE){
    warning('Due to the existence of a bar chart, the `expand_y_limit` argument will be converted to FALSE')
    expand_y_limit = FALSE

    warning('Due to the existence of a bar chart, the `x_breaks ` argument will be converted to the number of options available')
    breaks = length(unique(x_var))
  }
PedroJorge7 commented 6 months ago

Opa @Joaobazzo. Valeu pelo apontamento. Isso acontece porque as especificações do aes estão dentro do geomarea, mas a indicação da base esta dentro do ggplot. Fizemos a inclusão de busca para procurar a base dentro do ggplot() quando não tiver especificação da base dentro do geom*

library(ggplot2)
library(ipeaplot)
df <- data.frame(x1 = 1:10,y=1:10,z = c(rep(LETTERS[1],5)
                                        ,rep(LETTERS[2],5)))
df
#>    x1  y z
#> 1   1  1 A
#> 2   2  2 A
#> 3   3  3 A
#> 4   4  4 A
#> 5   5  5 A
#> 6   6  6 B
#> 7   7  7 B
#> 8   8  8 B
#> 9   9  9 B
#> 10 10 10 B
p <- ggplot(df)+
  geom_area(aes(x = x1,group = z,y = y,fill = z),
            linewidth = 0.25
            ,color = "black") +  
  ipeaplot::theme_ipea(legend.position = "bottom")
p

Warning message:
  Due to the existence of a Area graph, the `expand_y_limit` argument will be converted to FALSE

image

Joaobazzo commented 6 months ago

obrigado!