This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Barplot labeling squeezed the bars

I am trying to make a horizontal barplot in ggplot2. I almost get there but still not satisfied the output. My code is

David_BP_combined_signifcant$Term <- factor(David_BP_combined_signifcant$Term, levels = David_BP_combined_signifcant$Term[order(David_BP_combined_signifcant$logPvalue)]) 

ggplot(David_BP_combined_signifcant, aes(x=Term, y=logPvalue)) + 
          geom_bar(position = 'identity', stat='identity', fill='red')  +
          geom_text(aes(label=Count), size = 2, position=position_stack(vjust=0.5), hjust=0.5) +
          theme_bw() +
          theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
          theme(panel.border = element_blank()) +
          coord_flip() +
          ggsave("FigureC2.tiff", width=6, height=8, dpi=300)

In the output figure the bars are too short, because the labeling of one term is too long. I have tried to add:

aes(stringr::str_wrap(Term, 15), logPvalue)

But in this case the order of the Barplot will be disrupted.

what is the better solution for this situation?

Thanks a lot

r

1 answer

Hey, try something like

p + scale_x_discrete(labels=scales::wrap_format(30))

Log in to answer this question.