0

I am making a bar chart using ggplot2. The code is as follows:

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

However since my x-axis variable labels are really long the chart looks like this.

enter image description here

How do i reduce the plot size and make my x-axis labels more readable? Can I reduce the size of the plot by half giving enough room for the x-axis labels?

The reproducible code is as follows:

  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")
Apricot
  • 2,925
  • 5
  • 42
  • 88
  • Please provide a reproducible example. And you probably mean y-axis instead of x-axis? – tonytonov Apr 12 '15 at 10:51
  • More info on how to [provide a reproducable example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jaap Apr 12 '15 at 18:47
  • Thank you Jaap and tonytonov. I have edited the post with the reproducible code. It is not the y-axis...the x-axis is flipped. Essentially I want to reduce the plot area to provide more room for the longer variables. – Apricot Apr 13 '15 at 04:15

1 Answers1

2

Your labels look like that not because there's not enough space for them, but because there's a str_wrap usage with width=5 that forces line breaks. Instead use

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

and you'll be fine:

enter image description here

tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • Many thanks tonytonov....I tried till 15...since couldn't see visible change...i thought it might have got to do with something else. Now it is clear. Thank you again. – Apricot Apr 14 '15 at 02:05