ggplot2增加堆叠文本标签之间的间距

时间:2018-01-23 23:55:04

标签: r ggplot2

我正在尝试找到一种方法来增加堆叠条形图中文本标签之间的间距,以便它们不会重叠。没有多少垂直调整会使它们进一步分散。 geom_text(size = 6, position = position_stack(), vjust = -0.5)

enter image description here

1 个答案:

答案 0 :(得分:1)

以下是使用geom_text_repel()包中的ggrepel的示例。

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

相关问题