Shading a region in a geom_point plot with categorical x-axis

2,762 views
Skip to first unread message

scott_chamberlain

unread,
Sep 22, 2011, 8:30:26 AM9/22/11
to ggp...@googlegroups.com
I need to shade a region in many faceted panels with a geom_point layer. Here is a brief reproducible example. I would like to shade the A group from the left axis in each C and D panel to the middle between A and B groups. Should I use geom_polygon, geom_rect, or other layer?

df <- data.frame(facet = rep(c("C","D"), each=6), 
                 group = rep(c("A","B"), 6), 
                 mean = runif(12))

ggplot(df, aes(x = group, y = mean)) + 
  geom_point() +
  facet_wrap(~ facet) +
  theme_bw()

Dennis Murphy

unread,
Sep 22, 2011, 6:27:16 PM9/22/11
to ggp...@googlegroups.com, Scott Chamberlain
Hi:

Try this:

df <- data.frame(facet = rep(c("C","D"), each=6),
group = rep(c("A","B"), 6),
mean = runif(12))

ggplot(df, aes(x = group, y = mean)) +

theme_bw() +
geom_rect(xmin = 0, xmax = 1.5, ymin = -0.5, ymax = 1.5,
fill = 'green', alpha = 0.05) +
geom_point(size = 2.5) +
facet_wrap(~ facet)

HTH,
Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

scott_chamberlain

unread,
Sep 23, 2011, 6:01:33 PM9/23/11
to ggp...@googlegroups.com, Scott Chamberlain
Thanks Dennis!

Ito, Kaori (Groton)

unread,
Mar 15, 2013, 5:26:47 PM3/15/13
to Dan Oele, ggp...@googlegroups.com

I typically use ‘annotate’ for the situation like below. Maybe you can use ‘geom_polygon’, ‘geom_rect’ (but I haven’t tried it).

For the second example, I could not figure out what is oele in “ggplot(oele)” so I couldn’t try it…sorry.

 

df <- data.frame(facet = rep(c("C","D"), each=6), 

                 group = rep(c("A","B"), 6), 

                 mean = runif(12))

 

ggplot(df, aes(x = group, y = mean)) +

  geom_point() +

  annotate("rect", xmin=1, xmax=2, ymin=-Inf, ymax=Inf, alpha=0.2, fill="blue") +

  facet_wrap(~ facet) +

theme_bw()

 

In stead of xmin=1, xmax=2, we can also do:

 

xlim <- as.numeric(unique(df$group))

xmin= xlim[1], xmax= xlim[2]

 

I hope this helps,

Kaori

 

From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Dan Oele
Sent: Friday, March 15, 2013 3:29 PM
To: ggp...@googlegroups.com
Subject: Re: Shading a region in a geom_point plot with categorical x-axis

 

I think i have a similar issue, attempting to create a shaded region/ polygon for y values with categorical X values: here is the code currently running: I cannot seem to figure out how to create shaded regions above and below the geom_hline values...any help would be greatly appreciated:

 

 

p.888 =ggplot(oele) + geom_violin(aes(x=life.stage,y=Sr88.Ca43log)) + geom_point(aes(x=life.stage,y=Sr88.Ca43log,color=qd.class,shape=life.stage),position=position_jitter(w=0.1,h=0),size=2.8)+scale_shape_manual(values=c(17,16)) + scale_colour_manual(values=qda.predict.yoy)+labs(colour = "QDFA Assignments,65 % posterior")+ggtitle('')+theme(legend.position='top')+ theme(legend.position="none")+ geom_hline(yintercept = -.5, size=.5)+geom_hline(yintercept = -2, size=.5)

 

 

best,

 

dan

--

--
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/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages