Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 increase spacing between stacked text labels

Tags:

r

ggplot2

I'm trying to find a way to increase the spacing between the text labels in my stacked barcharts so that they don't overlap. No amount of vertical adjustment will space them further. geom_text(size = 6, position = position_stack(), vjust = -0.5) :

enter image description here

like image 890
LostLin Avatar asked Jul 12 '26 09:07

LostLin


1 Answers

Here is an example using geom_text_repel() from the ggrepel package.

library(ggplot2)
library(ggrepel)
library(reshape2)   

DF <- read.table(text="Rank F1     F2     
1    300    150    50
2    400    10    10
", header=TRUE)    

DF1 <- melt(DF, id.var="Rank")

ggplot(DF1, aes(x = Rank, y = value, fill = variable)) + 
  geom_bar(stat = "identity")+
  geom_text_repel(aes(label = variable),
                  nudge_y=0.5,
                  nudge_x=0)

enter image description here

like image 110
J.Con Avatar answered Jul 13 '26 22:07

J.Con