2015-04-12 414 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