[R] ggplot2 geom_rect(): What am I missing here

David Winsemius dwinsemius at comcast.net
Sun Apr 4 23:14:06 CEST 2010


On Apr 4, 2010, at 4:18 PM, Marshall Feldman wrote:

> Hi R fans,
>
> As a newbie following the five-hour rule (after hitting my head  
> against
> the wall for five hours, post to this list), I am appealing for some
> help understanding geom_rect() in ggplot2.
>
> What I want to do is very simple. I want to generate a plot of
> rectangles. Each one represents a business cycle. The x-values will be
> pairs representing the start and end of each cycle. The y-values
> represent the duration of the cycle (in months). In other words, all
> rectangles have coordinates (start, duration) and (end, duration).
> rr
> I've spent hours trying to figure out the documentation and pouring  
> over
> Google and RSeek searches and am at an impasse. The documentation  
> refers
> to xmin, xmax, ymin, and ymax but doesn't say anything about them. The
> only example gives them both as vectors, so I assume they refer to a
> sequence of coordinates in which each rectangle's vertices is given by
> (xmin[i],ymin[i]), (xmin[i],ymax[i]), (xmax[i],ymax[i]), and
> (xmax[i],ymin[i]). But when I try to plot something simple using this
> understanding,  I get a blank plot.
>
> Here's my code:
>
>    df <- data.frame(
>             xmin = c(1,5),
>             xmax = c(2,7),
>             ymin = c(0,3),
>             ymax = c(2,5)
>             )
>    ggplot(df, aes(xmin = xmin, xmax = xmax, ymin = ymin, )) +
>             geom_rect(fill="grey80")

I took a crack at starting with the code at Hadley's help page for  
geom_rect and got what appearred to work (with a dataframe names "df1"):

  ggplot(df1, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)) +
         geom_rect(fill="grey")

Quite honestly I had a very hard time  figuring out how that was  
different than what you did (or what I did earlier that seemed to fill  
in the entire plot area) but then I noticed that you had put in "ymax  
= ymin" when you clearly meant something else.

>
> Please help me before I Google again! :-)
>
> Thanks.
>
>    Marsh Feldman
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list