2

The plot Im generating using this code gives me this plot

The issue is I can't able to match the order that is present in the plot

My code

p <- df %>%
  ggplot(aes(name, perc)) +
  geom_col(data = ~ filter(.x, name == "FAB") %>% rename(FAB = value), mapping = aes(fill = FAB)) +
  #scale_fill_manual(values = cols)+
  new_scale_fill() +
    geom_col(data = ~ filter(.x, name == "Sex") %>% rename(Sex = value), mapping = aes(fill = Sex)) +
  new_scale_fill() +
  geom_col(data = ~ filter(.x, name == "Age") %>% rename(Age = value), mapping = aes(fill = Age)) +
  new_scale_fill() +
  geom_col(data = ~ filter(.x, name == "BM_percentage") %>% rename(BM_percentage = value), mapping = aes(fill = BM_percentage)) +
  
  coord_flip()+theme_bw(base_size=30)+
  theme(axis.text.x=element_text(angle = 45, size=45, face="bold", hjust = 1), legend.position = "right",
        axis.text.y=element_text(angle=0, size=40, face="bold", vjust=0.5),
        plot.title = element_text(size=40, face="bold"), 
        legend.title=element_text(size=20),
        legend.key.size=unit(1, "cm"),      #Sets overall area/size of the legend
        legend.text=element_text(size=40))
p+scale_fill_manual(values=rainbow(8),guide = guide_legend(order = 1))

plot

This line of code I tired but no change in my order.

guide = guide_legend(order = 1)

How to fix the legend order any suggestion or help would be really appreciated

stefan
  • 90,330
  • 6
  • 25
  • 51
PesKchan
  • 868
  • 6
  • 14

1 Answers1

5

The issue is that you set the order only for one of your fill scales, i.e. for the fill scale you added last which is the one for BM_percentage. And as you demanded with order=1 this legend is put on top.

To put the legends in the order of the y axis categories you have to set the order for each of your four fill scales, which requires to explicitly add a scale_fill_discrete in cases where you use the default fill scale:

Using the data from one of you older posts:

library(ggplot2)
library(ggnewscale)
library(dplyr)

ggplot(df, aes(name, perc)) +
  geom_col(data = ~ filter(.x, name == "FAB") %>% rename(FAB = value), mapping = aes(fill = FAB)) +
  scale_fill_discrete(guide = guide_legend(order = 2)) +
  new_scale_fill() +
  geom_col(data = ~ filter(.x, name == "Sex") %>% rename(Sex = value), mapping = aes(fill = Sex)) +
  scale_fill_discrete(guide = guide_legend(order = 1)) +
  new_scale_fill() +
  geom_col(data = ~ filter(.x, name == "Age") %>% rename(Age = value), mapping = aes(fill = Age)) +
  scale_fill_discrete(guide = guide_legend(order = 4)) +
  new_scale_fill() +
  geom_col(data = ~ filter(.x, name == "BM_percentage") %>% rename(BM_percentage = value), mapping = aes(fill = BM_percentage)) +
  scale_fill_manual(values = rainbow(8), guide = guide_legend(order = 3)) +
  coord_flip() +
  theme_bw(base_size = 10)

DATA

df <- structure(list(name = c(
  "Age", "Age", "Age", "Age", "Age", "BM_percentage",
  "BM_percentage", "BM_percentage", "BM_percentage", "BM_percentage",
  "BM_percentage", "Cytogenetic-Code--Other-", "Cytogenetic-Code--Other-",
  "Cytogenetic-Code--Other-", "Cytogenetics", "Cytogenetics", "Cytogenetics",
  "Cytogenetics", "Cytogenetics", "Cytogenetics", "FAB", "FAB",
  "FAB", "FAB", "FAB", "Induction", "Induction", "Induction", "Induction",
  "Induction", "patient", "patient", "patient", "patient", "patient",
  "patient", "Sex", "Sex"
), value = c(
  "39", "42", "62", "63", "76",
  "68", "72", "82", "83", "88", "91", "Complex Cytogenetics", "Normal Karyotype",
  "PML-RARA", "45,XY,der(7)(t:7;12)(p11.1;p11.2),-12,-13,+mar[19]/46,XY[1]",
  "46, XX[20]", "46,XX[20]", "46,XY,del(9)(q13:q22),t(11:21)(p13;q22),t(15;17)(q22;q210[20]",
  "46,XY[20]", "47,XY,del(5)(q22q33),t(10;11)(p13~p15;q22~23),i(17)(q10)[3]/46,XY[17]",
  "M0", "M1", "M2", "M3", "M4", "7+3", "7+3+3", "7+3+AMD", "7+3+ATRA",
  "7+3+Genasense", "TCGA-AB-2849", "TCGA-AB-2856", "TCGA-AB-2872",
  "TCGA-AB-2891", "TCGA-AB-2930", "TCGA-AB-2971", "Female", "Male"
), n = c(
  1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L,
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 4L
), perc = c(
  16.6666666666667,
  33.3333333333333, 16.6666666666667, 16.6666666666667, 16.6666666666667,
  16.6666666666667, 16.6666666666667, 16.6666666666667, 16.6666666666667,
  16.6666666666667, 16.6666666666667, 33.3333333333333, 50, 16.6666666666667,
  16.6666666666667, 16.6666666666667, 16.6666666666667, 16.6666666666667,
  16.6666666666667, 16.6666666666667, 16.6666666666667, 16.6666666666667,
  16.6666666666667, 16.6666666666667, 33.3333333333333, 16.6666666666667,
  33.3333333333333, 16.6666666666667, 16.6666666666667, 16.6666666666667,
  16.6666666666667, 16.6666666666667, 16.6666666666667, 16.6666666666667,
  16.6666666666667, 16.6666666666667, 33.3333333333333, 66.6666666666667
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(
  NA,
  -38L
), groups = structure(list(name = c(
  "Age", "BM_percentage",
  "Cytogenetic-Code--Other-", "Cytogenetics", "FAB", "Induction",
  "patient", "Sex"
), .rows = structure(list(
  1:5, 6:11, 12:14, 15:20,
  21:25, 26:30, 31:36, 37:38
), ptype = integer(0), class = c(
  "vctrs_list_of",
  "vctrs_vctr", "list"
))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -8L), .drop = TRUE))
stefan
  • 90,330
  • 6
  • 25
  • 51