2015-04-12 409 views
0

我正在使用ggplot2制作条形图。代码如下:ggplot2长x轴变量名称 - 重新对齐图

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") 
+0

请提供一个可重复的例子。你可能是指y轴而不是x轴? – tonytonov

+0

有关如何[提供可重现示例]的更多信息(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jaap

+0

谢谢Jaap和tonytonov。我用可复制的代码编辑了这篇文章。它不是y轴...... x轴被翻转。基本上我想减少绘图区域以为更长的变量提供更多空间。 – Apricot

回答

1

你的标签看起来不是因为没有足够的空间让他们,但因为有一个str_wrap使用与width=5该部队换行符。 而是使用

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

,你会被罚款:

enter image description here

+0

非常感谢tonytonov ....我尝试了直到15 ...因为看不到可见的变化...我认为这可能与其他事情有关。现在很清楚。再次感谢你。 – Apricot

+0

不客气! – tonytonov