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

David Winsemius dwinsemius at comcast.net
Sun Apr 4 22:33:37 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)
>             )

I have an allergy to naming data objects with names that apply to R  
functions so I used df1 as the object name. I have not taken the time  
to learn ggplot but here is a base graphics attempt:

plot(NULL , xlim=c(min(df1$xmin), max(df1$xmax)),
             ylim=c(min(df1$ymin), max(df1$ymax)) ) # sets up blank plot
  with(df1, rect(xmin, ymin, xmax, ymax) )

-- 
David.


 >
>    ggplot(df, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax =  
> ymin)) +
>             geom_rect(fill="grey80")
>
> 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