Barplot labeling squeezed the bars
1
0
Entering edit mode
4.4 years ago
Kai_Qi ▴ 130

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.3k views
ADD COMMENT
4
Entering edit mode
4.4 years ago
MatthewP ★ 1.4k

Hey, try something like

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

Login before adding your answer.

Traffic: 2613 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6