使用geom_text标记geom_error栏

时间:2016-05-24 06:45:37

标签: r ggplot2 label errorbar

如何使用geom_text标记geom_error标签并将标签与上下误差线对齐?

更新数据:

Interval    Effect  Estimate    SE  Sign_u  Sign_l
5   S   0.4104  0.04375 a   
5   V   0.2717  0.04090     b
5   N   0.3107  0.04091     
5   SP  0.3715  0.04374     
10  S   0.5410  0.04391 a   
10  V   0.3381  0.04098     b
10  N   0.3730  0.04101 a   
10  SP  0.5061  0.04388     b
15  S   0.7066  0.06193 a   
15  V   0.4227  0.05790     b
15  N   0.4737  0.05790 a   
15  SP  0.6556  0.06193     b
20  S   0.8422  0.07204 a   
20  V   0.5306  0.06735     b
20  N   0.5728  0.06735 a   
20  SP  0.7999  0.07204     b
30  S   1.0834  0.1041  a   
30  V   0.6785  0.09733     b
30  N   0.7141  0.09733 a   
30  SP  1.0478  0.1041      b
60  S   1.5110  0.1533  a   
60  V   0.9677  0.1433      b
60  N   1.0163  0.1433  a   
60  SP  1.4625  0.1533      b
5   S   3.7770  0.5045      
5   V   2.5857  0.4715      
5   N   2.7823  0.4718      
5   SP  3.5804  0.5043      
10  S   5.0958  0.5370  a   
10  V   3.1071  0.5009      b
10  N   3.3910  0.5015      
10  SP  4.8119  0.5365      
15  S   6.4108  0.7186  a   
15  V   4.1142  0.6719      b
15  N   4.2556  0.6719  a   
15  SP  6.2694  0.7186      b
20  S   7.5758  0.8158  a   
20  V   4.9622  0.7627      b
20  N   5.2326  0.7627      
20  SP  7.3054  0.8158      
30  S   9.3901  1.1463      
30  V   6.4235  1.0718      
30  N   6.5032  1.0718      
30  SP  9.3103  1.1463      
60  S   12.8952 1.7690      
60  V   9.7981  1.6539      
60  N   9.3402  1.6539      
60  SP  13.3532 1.7690      

我试过这个:

limits <- aes(ymax = Estimate + SE, ymin = Estimate - SE)
p <- ggplot(data, aes(x=Interval,y=Estimate, color = Effect, fill=Effect))
p + 
  geom_line() + 
  geom_errorbar(limits) +
  geom_text(data = data, aes(label = Sign_u), vjust = -2) +
  geom_text(data = data, aes(label = Sign_l), vjust = 2)

可以将vjust分配给上面定义的限制吗?

1 个答案:

答案 0 :(得分:1)

这是一个以mtcars数据集为例的解决方案:

enter image description here

data = ddply(mtcars, "cyl", summarize, Mean = mean(mpg, na.rm = T), Sd = sd(mpg, na.rm = T))

ggplot(data, aes(cyl, Mean, color = cyl)) + geom_line() + 
geom_errorbar(aes(ymin = Mean - Sd, ymax = Mean + Sd)) + 
geom_text(data = data, aes(x = cyl, y = (Mean + Sd), 
label = round(Mean + Sd)), vjust = -0.5)+ 
geom_text(data = data, aes(x = cyl, y = (Mean - Sd), 
label = round(Mean - Sd)), vjust = +1)