This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to collect legend into one column in ggplot2

my code look like this;

ggplot(data=my_data, aes(x=samples, y=relative_abundance, fill=species.Name)) + geom_bar(position = "fill",stat = "identity")
+   scale_fill_manual(name=NULL, values = cols) +
          labs(x = NULL,
               y = "Relative Abundance")+
          theme_classic()+
            theme(legend.text = element_text(face = "italic"),
                  legend.key.size = unit(10, "pt"),
                  legend.background = element_rect(fill="lightgray"))+ guides(color=guide_legend(ncol =1))

and I want to collect legend into one column, I tried to add this line in my code but it did not work.

guides(color=guide_legend(ncol =1))

ggplot r barplot

1 answer

The legend is for the fill, not color aesthetic, so you need to change it to guides(fill=guide_legend(ncol =1)). Color would be the outline for the boxes.

Thanks a lot, it works )

Log in to answer this question.