加星标的条形图

cra*_*ian 3 plot r bar-chart

我试图制作一个简单的条形图,首先区分两组,基于性别,男性或女性,然后在统计数据后,对于每个样本/个体,有一个P值,重要与否.我知道如何对男性和女性之间的条形码进行颜色编码,但我希望R自动在每个样本/ P值小于0.05的个体上方放置一颗星.

我目前只是使用简单的barplot(x)功能.

我试着四处寻找答案,但还没找到任何东西.

以下是我的示例数据集的链接:

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

我想把时间放在y轴上,用颜色代码区分男性和女性,然后对于任何一组中具有1个重要性的个体,将一颗星放在相应的条形图上方.

感谢您提前提出的任何建议.

Ben*_*ker 5

我稍微搞砸了你的数据,使它变得更友好:

## 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))
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

  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
Run Code Online (Sandbox Code Playgroud)

现在情节:

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()
Run Code Online (Sandbox Code Playgroud)

我还应在相关轴上添加"时间"和"ID"标签.

在此输入图像描述