post hoc刻字不匹配ggplot2

时间:2018-04-25 08:37:23

标签: r ggplot2 letters posthoc

我一直试图绘制一个lsmeans模型的结果,其中方框表示LS的意思。误差棒表示LS均值的95%置信区间,并且共享字母的均值没有显着差异。我想用ggplot2:

绘制下表cld.mixed.lme
dput(cld.mixed.lme)

structure(list(hor = structure(c(3L, 3L, 3L, 1L, 1L, 1L, 2L, 
2L, 2L), .Label = c("L", "F", "H"), class = "factor"), managem = structure(c(1L, 
3L, 2L, 3L, 1L, 2L, 1L, 2L, 3L), .Label = c("WTH", "CH", "CHF"
), class = "factor"), response = c(23.6794086785122, 23.8174295982324, 
24.4481975946679, 27.7814605969773, 28.6059616644958, 28.7459261527063, 
37.1161977750334, 40.0618072489354, 40.062016186989), SE = c(2.47194303396734, 
2.47194303396734, 2.47194303396734, 2.47194303396734, 2.47194303396734, 
2.47194303396734, 2.47194303396734, 2.47194303396734, 2.47194303396734
), df = c(12.8849763292624, 12.8849763292851, 12.8849763290692, 
12.8849763293197, 12.8849763292728, 12.8849763291023, 12.8849763292846, 
12.88497632933, 12.8849763292846), lower.CL = c(15.4642399103678, 
15.602260830088, 16.2330288265235, 19.5662918288329, 20.3907928963513, 
20.5307573845618, 28.901029006889, 31.846638480791, 31.8468474188446
), upper.CL = c(31.8945774466566, 32.0325983663769, 32.6633663628123, 
35.9966293651217, 36.8211304326402, 36.9610949208507, 45.3313665431779, 
48.2769760170799, 48.2771849551334), .group = c("a", "ab", "ab", 
"abc", "abcde", "abd", "bcde", "ce", "de")), .Names = c("hor", 
"managem", "response", "SE", "df", "lower.CL", "upper.CL", ".group"
), row.names = c(8L, 5L, 2L, 6L, 9L, 3L, 7L, 1L, 4L), class = "data.frame")

它看起来像这样:

----------------------------------------------------------------------------------
     hor   managem   response    SE      df     lower.CL   upper.CL   .group 
-------- ----- --------- ---------- ------- ------- ---------- ---------- --------
 **8**     H      WTH      23.68     2.472   12.88    15.46      31.89       a    

 **5**     H      CHF      23.82     2.472   12.88     15.6      32.03       ab   

 **2**     H      CH       24.45     2.472   12.88    16.23      32.66       ab   

 **6**     L      CHF      27.78     2.472   12.88    19.57        36       abc   

 **9**     L      WTH      28.61     2.472   12.88    20.39      36.82     abcde  

 **3**     L      CH       28.75     2.472   12.88    20.53      36.96      ab d  

 **7**     F      WTH      37.12     2.472   12.88     28.9      45.33      bcde  

 **1**     F      CH       40.06     2.472   12.88    31.85      48.28      c e   

 **4**     F      CHF      40.06     2.472   12.88    31.85      48.28       de   
--------------------------------------------------------------------------------- 

运行以下代码后,绘图正确显示,但由于.group字母落在错误的response上,因此不匹配。 结果图中的示例:在hor = L managem = WTH下我有.group个字母“abc”而不是“abcde”(这取代了managem = CH)。

以下是代码:

library(ggplot2)
pd = position_dodge(0.7)   
plot.mixed.lme<-ggplot(cld.mixed.lme,aes(x = hor,y=response, color=managem, label=.group))+ 
        theme_bw()+
        geom_point(shape  = 15, size   = 4, position = pd) +
        geom_errorbar(aes(ymin  =  lower.CL,ymax  =  upper.CL),width =  0.2,size  =  0.7,position = pd) +
        theme(axis.title   = element_text(face = "bold"),
              axis.text    = element_text(face = "bold"),
              plot.caption = element_text(hjust = 0)) +

        geom_text(nudge_x = c(-0.3, 0, 0.3, -0.3, 0, 0.3,-0.3, 0, 0.3),
                  nudge_y = c(4.5,  4.5, 4.5,4.5,  4.5, 4.5,4.5,  4.5, 4.5),
                  color   = "black") 
plot.mixed.lme

以下是结果图:

enter image description here

我欢迎任何建议,并提前感谢您的帮助,

BAlpine

1 个答案:

答案 0 :(得分:0)

我找到了解决办法,但这很耗时。基本上我修改了 geom_text适合表格:

geom_text(nudge_x = c(-0.3, 0.3, 0, 0.3, -0.3, 0,-0.3, 0, 0.3),

有想法自动匹配吗?

非常感谢