How properly use scale_fill_manual() and its arguments labels(), breaks(), limits() - colours being messed up!

67 views
Skip to first unread message

Gabriella Scatà

unread,
Nov 22, 2023, 12:57:17 AM11/22/23
to ggplot2
Hi everyone,
I am trying to plot this barplot, with selected colours (manually) for my_dataframe.

head(my_dataframe)
pop              individ     K     coefficient_q      Genotype
Region A     1               K1        0.95                K1_PURE
Region A     1               K2        0.05                K2_PURE
Region B     2               K1        0.98                K1_Mismatch
Region B     2               K2        0.02                K2_Mismatch
...                  ...              ...          ...                     ...

I filtered my_dataframe, to eliminate one of the Regions that I don't want to plot (i.e. Region A), which also means i have 0 counts now for 2 of the Genotype levels.
I want to plot each Genotype with a different colour, but I want to display in the legend only the first 4 labels of the Genotypes. The code I used is below.
The problem is that, I get each Genotype class with the colour I want if I don't add the argument "limits(c( "K1_Mismatch1","K1_PURE1", "K2_Mismatch1", "K2_PURE" )". As soon as I add "limits()", I get most of my bars (so most of my Genotype classes) coloured "grey" (which is not even listed in my colours values!!).
Why is that happening?

Isn't limits supposed to list all the labels you want to show up in the legend and not affect the colour of the factor variable you are filling your bar with?

Also, what is the difference in using "breaks" and "labels"?? How can I be sure that each Genotype factor category has been assigned the correct colour I want it to assigned? Which argument in the code of scale_fill_manual() is responsible for this?

I would appreciate some help as I am breaking my head on this!!
Thanks a lot!
Gabriella

--> CODE below:
<



my_dataframe %>%
                              mutate(pop = fct_relevel(pop, 
                                                       "Region A", "Region B"," Region C "," Region D "," Region E ",
                                                       " Region F "," Region G"," Region H"," Region I"," Region L",
                                                       " Region M"," Region N")) %>%
                              ggplot() +
                              geom_col(aes(x = indiv, y = coefficient_q, fill = Genotype)) + 
                              scale_fill_manual(
                                              values = c("lightblue", "slateblue", "rosybrown1",
"tomato", "lightblue", "slateblue", "rosybrown1","tomato"), 
                                                labels = c("K1_Mismatch1","K1_PURE1", "K2_Mismatch1", "K2_PURE",  "K1_Mismatch2", "K1_PURE2","K2_Mismatch1","K2_PURE2"),
                                                breaks = c("K1_Mismatch1","K1_PURE1", "K2_Mismatch1", "K2_PURE",  "K1_Mismatch2", "K1_PURE2", "K2_Mismatch1","K2_PURE2"),
                                                limits = c( "K1_Mismatch1","K1_PURE1", "K2_Mismatch1", "K2_PURE" )) + 
                              facet_wrap( ~ pop, scales="free_x", labeller = labeller(pop = pop)) +
labs(fill = "K")

              >
Reply all
Reply to author
Forward
0 new messages