how to show only one of four common legends

I have 4 plots, two UMAP plots (scatter plot), and two violin plots, there is a variable named "celltype" which is common among all of them, and the categorization is based on this variable.

first I plot each of them separately, then I used two subplots to concatenate them.
The problem is that users can see 4 legends for them, how can I show only one legend, because the variable and colors are the same. please see the attached picture.

This is the code:

#first UMAP
p_TF_umap <- plot_data_specific() %>%
            ggplot(aes(x = UMAP1, y= UMAP2, color = celltype ))+
            geom_point()+
            scale_color_manual(values = setNames(c(r_color) ,c(unique(celltype))) )+
            ggtitle("Meta")



#First Violin
p_TF_violin <- plot_data_violin() %>%
            ggplot(plot_data_violin(), mapping = aes(x=celltype, y=feature_expr, fill = factor(celltype))) +
            ggtitle("Violin Plot of Singlecell")+
            labs(y= "Expression", x = "Cell Type")+
            geom_violin()+
            scale_fill_manual(values = setNames(c(r_color) ,c(unique(celltype))))


#second UMAP
 p_TF_umap1 <- plot_data_specific1() %>%
            ggplot(aes(x = UMAP1, y= UMAP2, color = celltype))+
            geom_point()+
            scale_color_manual(values = setNames(c(r_color) ,c(unique(celltype))))+
            ggtitle("Metacell Section")


#Second violin
 p_TF_violin1 <- plot_data_violin1() %>%
            ggplot(plot_data_violin1(), mapping = aes(x=celltype, y=feature_expr, fill=factor(celltype))) +
            ggtitle("Violin Plot of Metacell")+
            labs(y= "Expression", x = "Cell Type")+
            geom_violin()+
            scale_fill_manual(values = setNames(c(r_color) ,c(unique(celltype))) )



#Subplot Part
Final_fig1 <- subplot(p_TF_violin1, p_TF_violin) %>%
            layout(title = 'A                                          B',
                   autosize =T, width = 1500, height = 900, margin = 0.1)


#Second Subplot
 Final_fig1 <- subplot(Final_fig1, titleX = T, titleY = T,
                                margin = c(0.03),
                                subplot(p_TF_umap1, p_TF_umap,
                                        margin = c(0.01, 0.04, 0.01, 0.01),
                                        widths = c(0.5, 0.5),
                                        titleX = T, titleY = T),
                                nrows = 2,
                                heights = c(0.5, 0.5))

Have you tried the {patchwork} package? There is a function to "collect" the guides together. An example is here. The following may work with your code:

library(patchwork)

(p_TF_violin1 + p_TF_violin) / (p_TF_umap1 + p_TF_umap) + # arrange plots
  plot_layout(guides = "collect") + # collect guides
  plot_annotation(tag_levels = "A") # label panels

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.