annotate "rect" boundaries

827 views
Skip to first unread message

Michael Cawthon

unread,
Mar 14, 2015, 12:21:42 PM3/14/15
to ggplot2
Hello- Pasted below is the plot generated by the following code:

ggplot(mtcars) + geom_point(aes(x = hp, y = wt)) + geom_vline(xintercept = 150) + geom_hline(yintercept = 3) + annotate("rect", xmin = 150, xmax = 340, ymin = 3, ymax = 5.6, fill = "darkgreen", alpha = .3)

Question: How can I get the green rectangle generated by the annotate function to extend to the edge of the plot, such that it is flush with the x and y limits?  There seems to be a built-in border that I can't override, even by adding xlim and ylim arguments, and I can't seem to find a corresponding theme element.

Thank you in advance for any help.


Brandon Hurr

unread,
Mar 14, 2015, 2:38:43 PM3/14/15
to Michael Cawthon, ggplot2
If you set ymax and xmax to Inf does it do it?
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Cawthon

unread,
Mar 14, 2015, 2:55:58 PM3/14/15
to ggp...@googlegroups.com
It works perfectly.  Thank you.

Brandon Hurr

unread,
Mar 14, 2015, 2:58:30 PM3/14/15
to Michael Cawthon, ggp...@googlegroups.com
Just keep in mind that if you want a different quadrant highlighted you might have to use -Inf.

Allen Bingham & Diana Rigg (gmail)

unread,
Mar 14, 2015, 4:57:01 PM3/14/15
to Michael Cawthon, ggplot2

Try adding the expand setting for the axes scale functions (scale_x_continuous and scale_y_continuous) --- for example (I’ve split this into separate lines for clarity sake):

 

ggplot(mtcars) +

geom_point(aes(x = hp, y = wt)) +

geom_vline(xintercept = 150) +

geom_hline(yintercept = 3) +

annotate("rect", xmin = 150, xmax = 340, ymin = 3, ymax = 5.6, fill = "darkgreen", alpha = .3) +

scale_x_continuous(expand=c(0,0)) +

scale_y_continuous(expand=c(0,0))

 

This solution is not ideal as the result has a couple of points plotting on the axes, so a KLUDGEd solution to that issue that I came up with is as follows:

 

x.min <- min(mtcars$hp)

x.min <- x.min - 0.1*x.min

 

y.min <- min(mtcars$wt)

y.min <- y.min - 0.1*y.min

 

ggplot(mtcars) +

geom_point(aes(x = hp, y = wt)) +

geom_vline(xintercept = 150) +

geom_hline(yintercept = 3) +

annotate("rect", xmin = 150, xmax = 340, ymin = 3, ymax = 5.6, fill = "darkgreen", alpha = .3) +

scale_x_continuous(limits=c(x.min,340),expand=c(0,0)) +

scale_y_continuous(limits=c(y.min,5.6),expand=c(0,0))

 

Maybe someone else here will provide a more elegant solution?

 

Hope this helps-Allen

______________________________________

Allen Bingham

Bingham Statistical Consulting

aebin...@gmail.com

LinkedIn Profile: www.linkedin.com/pub/allen-bingham/3b/556/325

--

image001.png
Reply all
Reply to author
Forward
0 new messages