I would like to have more control over the size and placement of the axis label when building a two-dimensional table in R, but the rules for plotting seem to work differently for table objects than for data frames. To formulate my specific questions, I inserted into fake data at the end of this post using dput() . Running this code will create the dept.table object used in the graphic code below.
When you plot in a 2D table in R, it creates a mosaic plot, as shown below:
plot(dept.table, col=c("yellow","blue","red"), las=1, cex=.7, dir=c("h","v"), off=c(8,4))
There are three changes that I would like to make for axis labels in the plot created by this call.
First, change the height of Freshman, Sophomore, etc. on top so that they do not overlap (I know that I can shorten them, but I want to understand this for future reference as well, since it is not always convenient to shorten labels). I was thinking of disabling axis labels and adding them manually using the text() function. However, adding xaxt = 'n' to the plot call does not seem to work when plotting a table object (R gives "additional argument (s)" xaxt "will be ignored").
Another option is to rotate the x axis labels by setting las=2 . However, I would prefer the labels to be horizontal for readability. I should note, however, that when I set las=2 , the inscription "Graduate student" overlays the plot area.
So here are a few questions:
- How to disable x-axis labeling so that I can add labels manually?
- Even better, is there a more elegant way to make the labels alternate up and down so that they do not overlap, without having to manually set the position of each of them?
- If the label overlaps the plot area, is there a way (other than label reduction) to eliminate the overlap?
Is there a way to have different text sizes for the x-axis and y-axis labels? The actual graph that I want to do has a lot more departments (vertical axis), so I would like to use a smaller text size for the y axis than for the x axis. The only option for axis text size is cex , which adjusts both axes. Is there a way to configure each axis separately?
Y axis labels are valid. Is there a way to make them correct, so they will be close to the area of ββthe site?
By the way, although I made this plot with basic graphics, I would be wondering if there are better ways to do this using the grill, ggplot2 or, possibly, another package.
dept.table = structure(c(10, 15, 20, 200, 5, 15, 35, 200, 49, 15, 25, 20, 250, 5, 12, 34, 150, 30, 50, 108, 75, 800, 32, 39, 135, 400, 195, 80, 99, 64, 700, 47, 41, 134, 350, 160, 5, 10, 5, 110, 5, 4, 10, 30, 13), .Dim = c(9L, 5L), .Dimnames = structure(list( c("Anthropology", "Economics", "Mathematics", "Business Administration", "Geography", "Special Education, & Deaf Studies", "History", "Kinesiology & Health Science", "Family & Consumer Sciences" ), c("Freshman", "Sophomore", "Junior", "Senior", "Graduate Student" )), .Names = c("", "")), class = "table")