`label.x.npc` and `label.x.npc` behave strangely when using `facet_grid` and a color aesthetic

Create issue
Issue #25 resolved
Will Mayner created an issue

This example demonstrates the strange behavior:

library(tidyverse)
library(ggplot2)
library(ggpmisc)

set.seed(1)

data = tibble(
  x = rep(seq(16), 4),
  y = rep(seq(16), 4) + rnorm(64),
  group1 = factor(rep(seq(16), each = 4)),
  group2 = rep(c(rep('c', 4), rep('d', 4)), 8),
  group3 = rep(c(rep('e', 8), rep('f', 8)), 4)
)

my_plot = ggplot(
  data = data,
  mapping = aes(x = x, y = y, color = group1)
) +
geom_point() +
geom_smooth(method = 'lm', se = FALSE) +
ggpmisc::stat_poly_eq(
  label.x.npc = "right",
  label.y.npc = "top",
  formula = y ~ x,
  parse = TRUE,
) + 
facet_grid(
  rows = vars(group2),
  cols = vars(group3)
)

print(my_plot)

I would expect the annotations to be placed in the top right within each panel, without the large gaps between them.

It seems that the coordinates for the annotation are being computed in the aggregate space and then applied to the faceted panels, but they should be computed within the faceted panels.

Comments (7)

  1. Pedro J. Aphalo repo owner

    What you see is because you have mapped colour to a factor with 16 levels, rather that using the same four colours within each panel. In the current version there is no skipping of missing factor levels implemented, but I will consider this for the future.

    The example below demonstrates in which cases it works currently.

    library(tidyverse)
    library(ggplot2)
    library(ggpmisc)
    
    set.seed(1)
    
    data = tibble(
      x = rep(seq(16), 4),
      y = rep(seq(16), 4) + rnorm(64),
      group1 = factor(rep(seq(4), each = 16)), # Changed here!!
      group2 = rep(c(rep('c', 4), rep('d', 4)), 8),
      group3 = rep(c(rep('e', 8), rep('f', 8)), 4)
    )
    
    my_plot = ggplot(
      data = data,
      mapping = aes(x = x, y = y, color = group1)
    ) +
      geom_point() +
      geom_smooth(method = 'lm', se = FALSE) +
      ggpmisc::stat_poly_eq(
        label.x.npc = "left",
        label.y.npc = ,
        formula = y ~ x,
        parse = TRUE,
      ) + 
      facet_grid(
        rows = vars(group2),
        cols = vars(group3)
      )
    
    print(my_plot)
    

  2. Pedro J. Aphalo repo owner

    Add to documentation note about how mapping of aesthetics and faceting interact with the automatic positioning of labels, and give example on how to manually set positions of labels in a plot with facets.

  3. Pedro J. Aphalo repo owner

    This example demonstrates the strange behavior:

    library(tidyverse)
    library(ggplot2)
    library(ggpmisc)
    
    set.seed(1)
    
    data = tibble(
      x = rep(seq(16), 4),
      y = rep(seq(16), 4) + rnorm(64),
      group1 = factor(rep(seq(16), each = 4)),
      group2 = rep(c(rep('c', 4), rep('d', 4)), 8),
      group3 = rep(c(rep('e', 8), rep('f', 8)), 4)
    )
    
    my_plot = ggplot(
      data = data,
      mapping = aes(x = x, y = y, color = group1)
    ) +
    geom_point() +
    geom_smooth(method = 'lm', se = FALSE) +
    ggpmisc::stat_poly_eq(
      label.x.npc = "right",
      label.y.npc = "top",
      formula = y ~ x,
      parse = TRUE,
    ) + 
    facet_grid(
      rows = vars(group2),
      cols = vars(group3)
    )
    
    print(my_plot)
    

    I would expect the annotations to be placed in the top right within each panel, without the large gaps between them.

    It seems that the coordinates for the annotation are being computed in the aggregate space and then applied to the faceted panels, but they should be computed within the faceted panels.

  4. Pedro J. Aphalo repo owner

    After some thought I have decided to not modify the behaviour, but instead document it and describe the solution. The geom is already trying to guess the positions using grouping. Using grouping to shift positions is easy enough and consistent with ggplot2. Dropping levels would be counter-intuitive, as this is not how the grammar of graphics in general works.

  5. Samer MOUKSASSI

    library(tidyverse)
    library(ggplot2)
    library(ggpmisc)

    set.seed(1)

    data = tibble(
    x = rep(seq(16), 4),
    y = rep(seq(16), 4) + rnorm(64),
    group1 = factor(rep(seq(4), each = 16)), # Changed here!!
    group2 = rep(c(rep('c', 4), rep('d', 4)), 8),
    group3 = rep(c(rep('e', 8), rep('f', 8)), 4)
    )

    my_plot = ggplot(
    data = data,
    mapping = aes(x = x, y = y, color = group1)
    ) +
    geom_point() +
    geom_smooth(method = 'lm', se = FALSE) +
    ggpmisc::stat_poly_eq(
    label.x.npc = "left",
    label.y.npc = Inf,
    formula = y ~ x,
    parse = TRUE,
    ) +
    facet_grid(
    cols = vars(group1)
    )

    print(my_plot)

    specifying label.y.npc = Inf will presvent the label to change position by color even when colors are in different facets

  6. Log in to comment