如何手动绘制Box和Whisker图?

时间:2013-08-02 06:41:52

标签: r plot boxplot

Box和Whisker图表显示以下信息:max,min,mean,75th百分位数,第25百分位数。如果我有这些信息,我可以绘制相应的B& W图吗?

我有一个名为TP.df的数据框:

        pb1    ag1     pb2     ag2     pb3    ag3
Nb      498    498      85      85      68     68
Min       0      0       0       0       0      0
Max    1.72    461   2.641   260.8     0.3    144
Mean   0.06   19.2    0.15   35.35    0.02   9.11
75_p   0.06     20    0.08      33    0.02      8
25_p   0.01     10       0      14    0.01      4

文件:

,pb1,ag1,pb2,ag2,pb3,ag3
Nb,498,498,85,85,68,68
Min,0,0,0,0,0,0
Max,1.72,461,2.641,260.8,0.3,144
Mean,0.06,19.2,0.15,35.35,0.02,9.11
75_p,0.06,20,0.08,33,0.02,8
25_p,0.01,10,0,14,0.01,4

如何获得相应的Box和Whisker图表:

  • 必须显示6个方框
  • x轴:pb1ag1pb2ag2pb3ag3
  • y轴:0max(TP.df[Max,])

3 个答案:

答案 0 :(得分:7)

使用bxp从五位数摘要中制作一个箱线图。

这会做你想要的那种吗?

testdata=data.frame(R1=c(0,5,3,2,4),R2=c(1,7,3,2.8,6))
o=c(1,5,3,4,2) # the rows in increasing order
bxp.data=list(stats=data.matrix(testdata[o,]),n=rep(1,ncol(testdata)))
# the n=... parameter doesn't affect the plot, but it still needs to be there
bxp(bxp.data)

您可以使用标准图形参数(请参阅help(par))来标记轴并使其看起来很漂亮。

(注意:行号被一个人关闭,因为我在发布此答案后编辑了问题。我将保留答案,使其与前两个评论相符。)

答案 1 :(得分:1)

这可能是一个解决方案:

  #create a dummy boxplot that you can modify the data easily
  z<- boxplot(1:10)

  #look at the outbut an assign yout data to stats
  z$stats<- your_data

  #use bxp to plot, via add you can combine all three
  bxp(z)

或从头开始使用bxp函数.. 也可以看看: http://r.789695.n4.nabble.com/Box-plot-with-5th-and-95th-percentiles-instead-of-1-5-IQR-problems-implementing-an-existing-solution-td3456123.html

答案 2 :(得分:0)

使用带有ggplot的手动箱形图,您可以操纵图的美感并添加其他参数:

ggplot()+geom_boxplot(aes(x=1, y = 2:5, lower = 3, upper = 4, middle = 3.5, ymin=2, ymax=5))