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, ymax = ymin)) + geom_rect(fill="grey80") Please help me before I Google again! :-) Thanks. Marsh Feldman [[alternative HTML version deleted]]
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
Marsh, Your rectangles won't be very tall with ymax=ymin! (I hope that wasn't the cause of the 5 hours.) -Peter Ehlers On 2010-04-04 14:18, 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, 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. > >-- Peter Ehlers University of Calgary
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
Thanks to David Winsemius, Peter Ehlers, and Paul Murrell who pointed out my careless error working with ggplot2's geom_rect(). Not to make excuses, but when you've done something successfully dozens of times and suddenly it doesn't work, you're more likely to look for careless errors on your part. When you've never done something before and unsure that you understand the proper use of the tool, you're more likely to think you're missing something about the tool's proper use and to overlook your own careless errors. This list is great! I posted my question, went off to do something else, and within a few hours had the answer to my problem. Thanks again Marsh Feldman