[R] bar plot with non-zero starting level

Jim Lemon jim at bitwrit.com.au
Tue May 21 00:40:34 CEST 2013


On 05/21/2013 12:54 AM, Xianwen Chen wrote:
> Hi Jim,
>
> Thank you for the suggestion. I think overlapped rectangles will well
> present the message. I'm now trying ggplot2.
>
> Here is the code:
>
> require(ggplot2)
> rect_MNL_Delta <- data.frame(
> xmin <- c(1, 3, 5, 7, 9, 11, 13),
> xmax <- xmin + 1,
> ymin <- c(16.7026, 14.9968, 16.0630, 17.7510, -5.01694, -.44146, 1.45884),
> ymax <- c(21.1602, 18.7613, 19.1367, 23.6730, 2.26564, 3.08630, 3.39865)
> )
> rect_GMNL <- data <- data.frame(
> xmin <- c(1, 3, 5, 7, 9, 11, 13),
> xmax <- xmin + 1,
> ymin <- c(17.20, 16.32, 15.86, 18.12, -8.86, -0.03, 0.95),
> ymax <- c(20.12, 18.60, 18.14, 22.29, 3.03, 3.57, 2.81)
> )
>
> ggplot() +
> geom_rect(data = rect_MNL_Delta, aes(xmin = xmin, xmax = xmax, ymin =
> ymin, ymax = ymax), fill = "blue", alpha = 0.1) +
> geom_rect(data = rect_GMNL, aes(xmin = xmin, xmax = xmax, ymin = ymin,
> ymax = ymax), fill = "green", alpha = 0.1)
>
> The problem is that the second geom_rect() fully covered the first
> geom_rect(), instead of overlapping. Another issue is that I don't know
> how to set labels on x-axis, instead of the current ticks. I would like
> to have for instance 'Farmed cod' under the first rectangle.
>
Hi Xianwen,
The problem with the overlapping is in the last two lines. These should 
read:

         geom_rect(data = rect_MNL_Delta, aes(xmin = 
rect_MNL_Delta$xmin, xmax = rect_MNL_Delta$xmax, ymin = 
rect_MNL_Delta$ymin, ymax = rect_MNL_Delta$ymax), fill = "blue", alpha = 
0.1) +
         geom_rect(data = rect_GMNL, aes(xmin = rect_GMNL$xmin, xmax = 
rect_GMNL$xmax, ymin = rect_GMNL$ymin, ymax = rect_GMNL$ymax), fill = 
"green", alpha = 0.1)

I am not terribly familiar with ggplot, but you should be able to get 
custom tick labels on your horizontal axis. I have to go out now, but 
when I return I'll have a look at the "scale" functions, as I think this 
is hiding in there.

Jim



More information about the R-help mailing list