ggplot beginner - probably easy to solve

Hello together,
I´m trying to get ggplots for a larger amount of data and so far i was a bit but not fully successful.

this is my code:

geom_col(position="stack",width=0.4) +
coord_flip() + scale_fill_viridis(option="plasma")+
facet_grid(family~.)

This is my plot

My Problem is that in the Plot all genes are repeated (U2SURP - CHERP), but only CHERP
DDX42, DHX15, PUF60 and U2SURP are 17S U2 associated. How can I prevent the repition of the other genes?

See the FAQ: How to do a minimal reproducible example `reprex` for beginners. Questions like this are much easier to troubleshoot, and far more likely to attract useful answers, with a complete `reprex`, including representative data.

Here is one approach

``````
gene=factor(letters[c(1,2,1,3)]),
family=c(1,1,2,2),
peptides=c(12,21,31,14),
p.value=(1:4)/40
))
library(viridis)
library(tidyverse)

#   geom_col(position="stack",width=0.4)+
#   coord_flip() + scale_fill_viridis(option="plasma",limits=virid_scale_extent)+
#   facet_grid(family~.)
#

group_by(family) %>%
group_split())

split_df_recode <- map(split_df,~mutate(.,
gene=factor(gene)))

library(cowplot)

base_plot_func <- function(x){
ggplot(x,aes(x=gene,y=peptides,fill=-log10(p.value))) +
geom_col(position="stack",width=0.4) +
coord_flip() + scale_fill_viridis(option="plasma",
,limits=virid_scale_extent)
}

plots <- purrr::map(split_df_recode,
~{base_plot_func(.x) +
theme(legend.position = "none")
})

g1 <- plot_grid(plotlist=plots,
nrow = 2)

# extract the legend from one of the plots
legend <- get_legend(
# create some space to the left of the legend
base_plot_func(split_df_recode[[1]]) + theme(legend.box.margin = margin(0, 0, 0, 12))
)

# add the legend to the row we made earlier. Give it one-third of
# the width of one plot (via rel_widths).
plot_grid(g1, legend, rel_widths = c(6,1))``````

Thank you very much, it works! Really a big help for me - will take me a while to really understand the process but your comments help.

p.s. sorry for not shortening my question to an example