ggplot2:如何在图上将平均值写为文本?

时间:2013-07-17 16:12:52

标签: r ggplot2

我有以下数据框:

 my.data=read.table(text="         values tag class
 1   100.0000000  X3    CS
 2   100.0000000  X5    CS
 3    54.2370601  X7    CS
 4    63.4813701  X7    CS
 5    51.0402580  X7    CS
 6    60.1430236  X7    CS
 7    70.3939619  X7    CS
 8    56.5981165  X7    CS
 9    75.4148038  X7    CS
 10   88.2687052  X7    CS
 11   70.9697583  X7    CS
 12   54.2014007  X7    CS
 13   63.4325244  X7    CS
 14   51.0086771  X7    CS
 15   60.1034812  X7    CS
 16   70.3397973  X7    CS
 17   56.5630966  X7    CS
 18   75.3652206  X7    CS
 19   88.2007869  X7    CS
 20   70.9258461  X7    CS
 21   38.6325912  X9    CS
 22   27.7455102  X9    CS
 23   21.1778773  X9    CS
 24  100.0000000  X9    CS
 25   73.0803007  X9    CS
 26   55.7814809  X9    CS
 27   85.8614803  X9    CS
 28   38.5666461  X9    CS
 29   27.7114796  X9    CS
 30   21.1580450  X9    CS
 31   72.9906659  X9    CS
 32   55.7292434  X9    CS
 33   85.7810739  X9    CS
 34    0.0000000 XAS    CS
 35   49.0701633 XPW    CS
 36   40.0228822 XPW    CS
 37   23.8702273 XPW    CS
 38  100.0000000 XPW    CS
 39   89.3022370 XPW    CS
 40   53.2611491 XPW    CS
 41   69.9892886 XPW    CS
 42    0.0000000 XPW    CS
 43   14.8840750  X3    CD
 44   17.7316138  X3    CD
 45    6.1164435  X3    CD
 46    0.0000000  X3    CD
 47    1.1435141  X3    CD
 48   14.8904265  X3    CD
 49   17.7375474  X3    CD
 50    6.1241709  X3    CD
 51    1.1506441  X3    CD
 52   14.6751282  X3    CD
 53   17.5364297  X3    CD
 54    5.8621689  X3    CD
 55    0.9089743  X3    CD
 56   74.4165986  X5    CD
 57   76.7441666  X5    CD
 58   80.9582780  X5    CD
 59   58.5811936  X5    CD
 60   62.3494607  X5    CD
 61   69.1719883  X5    CD
 62   57.2509995  X5    CD
 63   61.1402872  X5    CD
 64   68.1819250  X5    CD
 65   74.3887236  X5    CD
 66   76.7211353  X5    CD
 67   80.9428400  X5    CD
 68   58.5360647  X5    CD
 69   62.3121736  X5    CD
 70   69.1469947  X5    CD
 71   57.2044213  X5    CD
 72   61.1018026  X5    CD
 73   68.1561287  X5    CD
 74   74.3425809  X5    CD
 75   76.6830205  X5    CD
 76   80.9173038  X5    CD
 77   58.4613610  X5    CD
 78   62.2504669  X5    CD
 79   69.1056522  X5    CD
 80   57.1273184  X5    CD
 81   61.0381141  X5    CD
 82   68.1134585  X5    CD
 83   66.3076784  X7    CD
 84   60.5650688  X7    CD
 85   68.2935472  X7    CD
 86   49.3767792  X7    CD
 87   40.7484217  X7    CD
 88   52.3605769  X7    CD
 89   36.4202616  X7    CD
 90   25.5835605  X7    CD
 91   40.1677332  X7    CD
 92   66.3298303  X7    CD
 93   60.5954120  X7    CD
 94   68.3131654  X7    CD
 95   49.4100626  X7    CD
 96   40.7940127  X7    CD
 97   52.3900536  X7    CD
 98   36.4620636  X7    CD
 99   25.6408202  X7    CD
 100  40.2047541  X7    CD
 101  66.1477122  X9    CD
 102  75.6876522  X9    CD
 103  81.4426220  X9    CD
 104  55.0173821  X9    CD
 105  67.6939692  X9    CD
 106  75.3411217  X9    CD
 107  51.6125069  X9    CD
 108  65.2486246  X9    CD
 109  73.4746140  X9    CD
 110  66.2054974  X9    CD
 111  75.7174719  X9    CD
 112  81.4600003  X9    CD
 113  55.0941665  X9    CD
 114  67.7335933  X9    CD
 115  75.3642139  X9    CD
 116  51.6951034  X9    CD
 117  65.2912480  X9    CD
 118  73.4994541  X9    CD
 119  25.6270138 XAS    CD
 120  45.2920118 XAS    CD
 121  44.5101287 XAS    CD
 122   0.0000000 XAS    CD
 123  17.9916840 XAS    CD
 124  16.8196263 XAS    CD
 125   7.3447585 XPW    CD
 126  24.4280115 XPW    CD
 127  54.9277702 XPW    CD
 128   0.0000000 XPW    CD
 129   4.6378241 XPW    CD
 130  43.1246155 XPW    CD
 131  39.7525599 XPW    CD
 132 100.0000000 XPW    CD")

我可以这样画:

qplot(tag,values,colour=factor(class),data=my.data,geom="jitter") + 
    stat_summary(fun.y='mean', geom='errorbarh', 
                 aes(xmin=as.integer(tag)-0.45, xmax=as.integer(tag)+0.45), 
                 height=0) + 
    facet_grid(.~class)

plot http://img11.hostingpics.net/pics/230718Rplot01.jpg

你知道如何用平均值作为文本(它的数值)替换代表均值的条形图吗?用geom ='text'替换geom ='errorbarh'不起作用。

你也知道我是否可以在每列的底部或顶部添加标准差作为数值?

1 个答案:

答案 0 :(得分:6)

fun0 <- function(x) -10

qplot(tag,values,colour=factor(class),data=my.data,geom="jitter")+
  stat_summary(fun.y='mean', geom='text', 
               aes(label=signif(..y..,4),x=as.integer(tag)))+ 
  stat_summary(fun.y="fun0",fun.ymin='sd', geom='text', 
               aes(label=paste("sd:",signif(..ymin..,2)),x=as.integer(tag)))+
  facet_grid(.~class)

enter image description here