Plotting asterix for significance in facetted bar plot

Showing 1-3 of 3 messages
Plotting asterix for significance in facetted bar plot Jokel Meyer 2/19/12 12:25 AM
Dear ggplot2 users!

I would like to create a facetted bar plot and then indicate
significance using a little asterix. Plotting works fine but I am not
sure how to add the asterix on top of the bar were dat$p<0.05 ?

Many thanks for your help!

Here is my code:


library(ggplot2)

# some dummy data
dat <- data.frame(name=c("a","b","a","c","a","b","d","c"),
val=c(1,-1.2,2.2,0.9,-0.7,2.4,-2.3,0.6),
sd=c(0.2,0.3,0.25,0.2,0.25,0.6,0.56,0.2),
p=c(0.09,0.1,0.02,0.1,0.2,0.001,0.001,0.6),
grp=c("A","A","B","B","C","C","C","C"))

ggplot(aes(x=name,y=val,width=0.5,ymin=val-sd,ymax=val+sd),data=dat)
+geom_bar(stat="identity",position="dodge") +
geom_errorbar(stat="identity",position="dodge") + facet_grid(~grp,
scales="free_x",space="free",margin=FALSE)
Re: Plotting asterix for significance in facetted bar plot winston 2/19/12 12:57 AM
You can use geom_text, and map a text column to 'label':

dat <- data.frame(name=c("a","b","a","c","a","b","d","c"),
                  val=c(1,-1.2,2.2,0.9,-0.7,2.4,-2.3,0.6),
                  sd=c(0.2,0.3,0.25,0.2,0.25,0.6,0.56,0.2),
                  p=c(0.09,0.1,0.02,0.1,0.2,0.001,0.001,0.6),
                  grp=c("A","A","B","B","C","C","C","C"))

dat$star <- ""
dat$star[dat$p <= .05]  <- "*"
dat$star[dat$p <= .01]  <- "**"
dat$star[dat$p <= .001] <- "***"

ggplot(dat, aes(x=name,y=val,width=0.5,ymin=val-sd,ymax=val+sd)) + 
  geom_bar(stat="identity", position="dodge") +
  geom_errorbar(stat="identity", position="dodge") + 
  geom_text(aes(label=star), colour="red", vjust=0, size=10) +
  facet_grid(. ~ grp, scales="free_x", space="free",margin=FALSE)


A few notes:
- I set vjust=0, so that the baseline of the text is at the y value. You could instead do something like aes(y=val+.2) to shift the text up a little.
- This looks a little funny with the positive and negative bars, since the stars end up inside the negative bars. If your data is really like this, I'd suggest not using bars... but if you must, then you could create a new column for y positions of the text, and for negative values, adjust the y value so that the text is outside the bar.

Hope this helps!
-Winston


--
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 ggp...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Re: Plotting asterix for significance in facetted bar plot Joseph Matt 1/13/15 7:08 AM
This is excellent help, Winston. 

I was having trouble setting the asterisks to specific data points, so that the asterisk only shows up when p equals 0.001, for example. 

I thought that 

dat$star[dat$p == .001] <- "***"  

would do the trick, but it still places an asterisk on my graph everywhere the p is less than or equal to 0.001. Any suggestions?

Thanks, 
Joozeman