tags:

views:

49

answers:

1

Hello friendly computer people,

I'm new to R and am getting a bit lost in the vast world of setting plot parameters. Currently I have a barplot that looks like this. My main issue is cleaning up the graph so that each bar is associated with a category. Right now the fonts are set so they overlap and don't show all the categories.

In other words, I would like to:

  1. Have the bar names read horizontally.
  2. Set the font paramaters so that each category is shown i.e. each bar has a name. Maybe this involves shrinking the font size?

Any help on this matter would be greatly appreciated!

Thank you :)

+3  A: 

EDIT: Very good suggestion of ucfagls included. Thx!

See the options las in ?par and cex.names in ?barplot :

# Sample dataset
x <- rpois(10,20)
names(x) <- replicate(10,
   paste(sample(LETTERS[1:10],10),collapse="")
)

# Demonstration of the options
op <- par(mar = c(5,6,4,2) + 0.1)
barplot(x,horiz=T,las=1,cex.names=0.5)
par(op)

Gives :

alt text

So adjust your own code using the options las and cex.names. Also check the option mar in the help page ?par to know how you could adjust the values in case your labels fall off.

Joris Meys
Thanks Joris. I'm assuming x represents the dataframe, no? Also, this seems to eliminate bars and rename them with letters, neither of which I want. Is there any way to do this while keeping the current names and all the data i.e. bars?
Eric Brotto
@Eric : it's just sample data to show you how it works. The only thing you should do is use las=1 and cex.names=0.5 (or another value you find suitable). All the rest is just so you could run the code.
Joris Meys
Got it. Thanks! :)
Eric Brotto
@Eric; you'll also (probably) need to add some extra space to the left-hand margin of the plot to accommodate the labels. You need to do this before the `barplot()` call **and** reset afterwards. So add `op <- par(mar = c(5,6,4,2) + 0.1)` **before** the `barplot(....)`, **and** `par(op)` **after** the `barplot(....)`. If you increase the vertical height of the plotting device (window) you'll probably be able to get away without making the bar labels smaller (i.e. you might be able to leave out the `cex.names = 0.5` bit of Joris code).
Gavin Simpson
@ucfagls : good suggestion! I added it to the code
Joris Meys
@Eric: in my comment and Joris's edited Answer, the `6` is the size of the left-hand margin (I was running out of space in the comment to include that bit of info), with `4` being the default for this margin. These are measured in lines of text, but other ways of specifying the size of the margin are available; see `?par`
Gavin Simpson