Skip to content

guide_legend_cross() with ignore factor levels with color and shape aes() #121

Description

@s-nicolas

Hello, when using both shape and color in the aes with guide_legend_cross, the factor levels are not kept.
@

library(tidyverse)
library(legendry)

set.seed(123)

df <- tibble(
  umap_1 = rnorm(100),
  umap_2 = rnorm(100),
  sample = rep(
    c(
      "Control D3",
      "Control D6",
      "Control D10",
      "Treated D3",
      "Treated D6",
      "Treated D10"
    ),
    length.out = 100
  ),
  condi = rep(
    c(
      "Control",
      "Treated"
    ),
    length.out = 100
  ),
  Treatment = rep(
    c(
      "D3",
      "D6",
      "D10"
    ),
    length.out = 100
  )
) %>%
  mutate(
    sample = factor(
      sample,
      levels = c(
        "Control D3",
        "Control D6",
        "Control D10",
        "Treated D3",
        "Treated D6",
        "Treated D10"
      )
    ),
    condi = factor(
      condi,
      levels = c(
        "Control",
        "Treated"
      )
    ),
    Treatment = factor(
      Treatment,
      levels = c(
        "D3",
        "D6",
        "D10"
      )
    )
  )

palette <- c(
  "Control D3" = "#377eb8",
  "Control D6" = "#6baed6",
  "Control D10" = "#deebf7",
  "Treated D3" = "#e41a1c",
  "Treated D6" = "#fb8072",
  "Treated D10" = "#fee0d2"
)

ggplot(df, aes(umap_1, umap_2)) +
  geom_point(aes(color = sample)) +
  scale_color_manual(
    values = palette,
    guide = guide_legend_cross(
      key = key_group_split(sep = " "),
      swap = TRUE
    )
  )

common <- guide_legend_cross(key = "auto", title = "Cross legend")

ggplot(df, aes(umap_1, umap_2)) +
  geom_point(aes(color = condi, shape = Treatment)) +
  guides(
    color = common,
    shape = common
  )

does any workaround exist ?

Thanks @ @

Activity

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

    🐛 bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions