ggplot2长x轴变量名称 - 重新对齐绘图

时间:2015-04-12 07:37:55

标签: r ggplot2 axis-labels

我正在使用ggplot2制作条形图。代码如下:

 ggplot(rt5, aes(x = reorder(Retweet, -Freq), y = Freq, fill = Retweet)) +       geom_bar(stat = "identity") +
      xlab("Top 5 Retweets of KKRiders") +
      ggtitle("Top 5 Retweets of KKRiders \n first three days") + coord_flip() +
      scale_x_discrete(labels = function(x) str_wrap(x, width = 5)) +
      geom_text(aes(label=Freq), vjust = 1, colour = "white") + guides(fill = FALSE) +
      theme(axis.text.x = element_text(hjust = 0)) +
      theme(plot.title=element_text(size = rel(1.2), lineheight = 1, face = "bold"))

然而,由于我的x轴变量标签非常长,因此图表看起来像这样。

enter image description here

如何减小绘图大小并使我的x轴标签更具可读性?我可以将绘图的大小缩小一半,为x轴标签提供足够的空间吗?

可重现的代码如下:

  rt5 <- structure(list(Retweet = structure(c(1L, 2L, 3L, 4L, 5L), .Label = c(

 "RT @KKRiders On another note that makes 10 IPL victories in a row And we are hungry to #Go4More#KKR",
 "RT @KKRiders Yaaayy Were now the biggest IPL team on Instagram Thanks for  all your love Knight Riders #Go4More httptcoSamtCajmIk",
 "RT @RCBTweets Dazzling hitting from Surya Kumar Yadav He builds on Gambhirs 50 to hand @KKRiders a 7 wkt win in the big 2015 #PepsiIPL O",
 "RT @DabbooRatnani Congratulations @iamsrk @KKRiders Great Win last night and an amazing start to the #IPL season #AbRamAtEden",
 "RT @t2telegraph Little AbRam makes his #EdenGardens debut at @IPL 8s opening match between @KKRiders amp @mipaltan #IPL @iamsrk httptc"),
 class = "factor"), 
 Freq = c(334L, 203L, 153L, 149L, 100L)), .Names = c("Retweet", 
 "Freq"), row.names = c(1L, 2L, 3L, 4L, 5L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

您的标签看起来不是因为没有足够的空间,但因为str_wrap使用width=5会强制换行。 而是使用

scale_x_discrete(labels = function(x) str_wrap(x, width = 25)) +

你会没事的:

enter image description here