3
votes

I'm trying to make a simple bar chart that first, distinguishes between two groups say based on sex, male or female, and then after stats, for each sample/ individual, there is a P-value, significant or not. I know how to color code the bars between male and female, but I want R to automatically put a star above each sample/ individual who has a P-value less than 0.05 say.

I'm currently just using the simple barplot(x) function.

I've tried to look around for answers but haven't found anything for this yet.

Below is is a link to my example data set:

[url=http://www.divshare.com/download/22797284-187]DivShare File - test.csv[/url]

I'd like to put the time on the y axis, color code the bars to distinguish between Male and Female, and then for individuals in either group who has a 1 under significance, put a star above their corresponding bar.

Thanks for any suggestions in advance.

1
Could you provide a reproducible example of your data (or similar toy data) using dput? - David Robinson
Also, is each bar an individual or a group? If it's a group, then what do you mean about putting a star "above an individual"? It's not clear what your desired graph looks like. (Again, showing a reproducible example of the graph without the stars would help) - David Robinson
Some of us are waiting for the raw data because we are having concerns that a "p-value" for an individual might not be a statistically meaningful notion. We would like to teach you how to fish but worry that you might hook one of your boatmates. - IRTFM
You added your datset,but still no reproducible example of R code. - Paul Hiemstra

1 Answers

5
votes

I messed with your data a bit to make it friendlier:

## dput(read.csv("barcharttest.csv"))
x <- structure(list(ID = 1:7,
  sex = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 2L), .Label = c("female", "male"),
   class = "factor"),
  val = c(309L, 192L, 384L, 27L, 28L, 245L, 183L),
  stat = structure(c(1L, 2L, 2L, 1L, 2L, 1L, 1L), .Label = c("NS", "sig"),
    class = "factor")),
               .Names = c("ID", "sex", "val", "stat"),
               class = "data.frame", row.names = c(NA, -7L))

Which looks like this:

  ID    sex val stat
1  1 female 309   NS
2  2 female 192  sig
3  3 female 384  sig
4  4   male  27   NS
5  5   male  28  sig
6  6 female 245   NS
7  7   male 183   NS

Now the plot:

sexcols <- c("pink","blue")
## png("barplot.png")  ## for output graph
par(las=1,bty="l")  ## I prefer these settings; see ?par
b <- with(x,barplot(val,col=sexcols[sex])) ## b saves x coords of bars
legend("topright",levels(x$sex),fill=sexcols,bty="n")
## use xpd=NA to make sure that star on tallest bar doesn't get clipped;
##   pos=3 puts the text above the (x,y) location specified
text(b,x$val,ifelse(x$stat=="sig","*",""),pos=3,cex=2,xpd=NA)
axis(side=1,at=b,label=x$ID)
## dev.off()

I should also add "Time" and "ID" labels on the relevant axes.

enter image description here