Skip to content

geom_area does not align with geom_line with duplicate x coords #5227

Closed

Description

@banbh

When x coordinates are duplicated geom_area does not align with geom_line. For example, in the following example, some of the x=2 coordinates are shifted back slightly (but visibly).

library(ggplot2) # ggplot2_3.4.1

data.frame(x = c(1,2,2,3), y = 4:1) |> 
  tidyr::crossing(id = c(0, 1e2)) |>
  ggplot(aes(x + id, y)) +
  facet_wrap(vars(id), scales = 'free_x') +
  geom_area() + # not aligned with lines from next geom_line
  geom_line()

Activity

  1. teunbrand commented on Mar 13, 2023

    @teunbrand
    Collaborator

    Thanks for the report, I can reproduce the issue. Here is the plot rendered:

    library(ggplot2)
    
    data.frame(x = c(1,2,2,3), y = 4:1) |> 
      tidyr::crossing(id = c(0, 1e2)) |>
      ggplot(aes(x + id, y)) +
      facet_wrap(vars(id), scales = 'free_x') +
      geom_area() + # not aligned with lines from next geom_line
      geom_line()

    Created on 2023-03-13 with reprex v2.0.2

    Ultimately, I think this turns out less than ideal due to the fudge factor of 0.001 used in this line:

    adjust <- diff(range(unique_loc, na.rm = TRUE)) * 0.001

    While each panel has a range of 2 on the x-axis, this fudge factor becomes 0.001 * 103 = 0.103 units, which is visible. I think it might be best to do the alignment calculations per panel instead of for the data as a whole. The stacking, that stat = "align" makes easier, applies on a panel-level rather than the whole data level as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions