[R] Barplot+Table

Marc Schwartz marc_schwartz at me.com
Wed Sep 23 23:53:22 CEST 2009


On Sep 23, 2009, at 3:51 PM, Andy Choens wrote:

> Marc Schwartz wrote:
>> Using the data that is in the online plot rather than the above, here
>> is a first go. Note that I am not drawing the background grid in the
>> barplot or the lines for table below it. These could be added if you
>> really need them.
>
> Note: I snipped out the syntax from Marc's earlier answer because it  
> is very
> lengthy. At the end of this email I have appended an edited version  
> of his
> syntax. The part that I have added is clearly marked. This syntax  
> runs without
> "error" but the output is clearly not lined up.
>
> Marc's original syntax works great, as long as X11() is left to it's  
> default
> dimensions (square). Unfortunately, after working with my report, I  
> realized
> that I need to make the barplot shorter and longer, rather than  
> square. When I
> try to change the shape of the output, I start running into problems.
>
> After looking through the documentation, it seemed as though X11()  
> would help
> me. I tried adding this to Marc's original syntax.
>
> X11(height=3, width=4.5, pointsize=10)
>
> Thus, things looked more like this:
> X11(height=3, width=4.5, pointsize=10)
> mp <- barplot(MyData, beside = TRUE, ylim = c(0, 100),
>               yaxt = "n", cex.names = MyCex, col = MyCols)
>
> Unfortunately, this results in two problems.
>
> 1) Because I have now changed the shape of the output area, the  
> additional
> text and labels are no longer shown on the plot. The barplot itself  
> looks
> great, but the labels do not.
>
> 2) I seem to be starting two separate X11() displays, although I  
> only want to
> start one.
>
> The following syntax demonstrates what I am playing with right now. I
> understand that the syntax no longer works because the labels are  
> falling
> outside of the shape set by X11(). Unfortunately, I haven't been  
> able to figure
> out how to fix it. If posible, I'd like to move the barplot to the  
> right, to
> give me more room to print out the labels, but I don't see how this is
> possible.
>
> # --------------------------------------------------
> # Create  data
> MyData <- matrix(c(57.1,52.3,13.5,13.9,7.9,8.8,5.4,5.6,16.1,19.4),  
> nrow=2)
>
> colnames(MyData) <- c("0 times", "1-2 times", "3-5 times", "6-9  
> Occasions",
> "10 or more\nOccasions")
>
> rownames(MyData) <- c("Androscoggin", "Maine")
>
> # Set graph margins to make room for labels
> par(mar = c(5, 8, 4, 1))
>
> # Set colors
> MyCols <- c("black", "grey80")
>
> # Set label size
> MyCex = 0.75
>
> # Set lines for table data
> MyLines <- 2:3
>
> # This is the part that I added.
> # And it seems to mess things up, although I do want the basic shape.
> # Is there a better way to get this shape?
> X11(height=3, width=4.5, pointsize=10)
>
> # do barplot, getting bar midpoints in 'mp'
> mp <- barplot(MyData, beside = TRUE, ylim = c(0, 100), yaxt = "n",  
> cex.names =
> MyCex, col = MyCols)
>
> # Put a box around it
> box()
>
> # Draw y axis tick marks and labels
> axis(2, at = seq(0, 100, 10), las = 1)
>
> # Draw values below plot
> mtext(side = 1, text = MyData,
>       at = rep(colMeans(mp), each = nrow(MyData)),
>       line = MyLines, cex = MyCex)
>
> # Get min value for the x axis. See ?par 'usr'
> min.x <- par("usr")[1]
>
> # Draw categories using mtext
> mtext(side = 1, line = MyLines, text = rownames(MyData),
>       at = min.x - max(strwidth(rownames(MyData), cex = MyCex)),
>       adj = 0, cex = MyCex)
>
> # Draw the colored boxes
> VertOff <- strheight("X", cex = MyCex) * c(6, 8)
> HorizOff <- min.x - (0.85 * max(strwidth(rownames(MyData))))
>
> # I'm sure these still exist, but they are now being drawn "off- 
> screen"
> points(rep(HorizOff, nrow(MyData)), par("usr")[3] - VertOff, bg =  
> MyCols, pch =
> 22, xpd = TRUE, cex = MyCex)


Andy,

Two issues:

1. The reason that you are getting two graphic devices being opened is  
that you are calling par(mar = c(5, 8, 4, 1)), which opens a device,  
followed later by explicitly calling X11(...), which then opens a  
second device.

2. The reason that your labels and points are not being properly  
displayed is that the margins of the plotting device have not been  
properly set, since you are calling par(mar = ...) before opening the  
X11() device. So this is directly related to #1 above.  Shift the call  
to par(mar = ...) so that it immediately follows the call to X11(...)  
and that should take care of the problem.

Also, presumably you are going to want to generate a file (PNG, PDF,  
etc.) of the graphic. Be sure to replace the call to X11() with the  
call to the desired device and then be sure to call dev.off() once the  
graphic is fully created.

HTH,

Marc Schwartz




More information about the R-help mailing list