我稍微弄乱了你的数据以使其更友好:
## 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))
看起来像这样:
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
现在的情节:
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()
我还应该在相关轴上添加“时间”和“ID”标签。