当交互结果中有 NA 值时,如何使用 multcompLetters2 计算 ggplot2 的 TukeyHSD 字母?

问题描述 投票:0回答:1

我有一些数据框形式的数据,我想对其执行方差分析和后续的 TukeyHSD。不幸的是,缺少一些处理之间的组合:品种。我想用

multcompLetters4
计算 ggplot 的字母,但由于缺少值而无法这样做。我已经尝试了我能想到的一切,但看在上帝的份上,无法解决这个看似简单的问题:

DATAFRAME<-structure(list(V = c(7.06219881675899, 9.88910116646339, 1.1317870659264, 
1.57078355189883, -52.9430955547448, -57.0901672962763, -8.17682037308832, 
26.815645980138, 0.788201843616179, 0.451474865901965, -3.9640771146851, 
17.8844718848957, 5.45294430680211, 15.1112750882548, -21.6925594262297, 
11.2961103805241, -5.42394914323165, 7.61301138824278, 8.10394249746956, 
-45.6106470588235, -44.44904486634, 6.40516259460818, 8.0427493566116, 
18.0568840531989, -43.648496713244, -63.8082326205971, -43.3822538613106, 
24.0865666034825, 25.0129302793091, 15.8912244359362, 8.71722624268141, 
13.0866492448362, 6.44909236607213, 0.877760091215976, 1.20403895767414, 
-52.1914303986357, -18.0482289669497, -11.6326593783504, -7.54211212535917, 
5.05518299915802, 13.705856501612, -7.23530557873445, -59.4806008768777, 
-51.5970524600298, -2.89850619632625, -12.8050349970571, -1.45896254809618, 
6.53299301298379, 4.91569507418079, 2.07905105900554, -5.15016516041506, 
-3.85428486272792, 14.1687733875076, 14.5279244128517, 21.4940031639864, 
5.07728805545359, 23.252359256292, -6.68064156924537, 7.72966550356682, 
11.3932426004331, -54.2113686255877, -84.1845217911639, -0.462273658775459, 
-27.6393413098194, -0.398665145299958, -13.2607564070671, 74.0556117290192, 
12.23180148787, -13.144042103673, -22.3042252306531, 8.33147899854212, 
-58.9219547193878, -6.65521243387097, -24.8555813006873, -4.89737319671092, 
-74.2399809247813, -7.79156874935596, 20.5543774499235, -14.9430902105972, 
-58.5822702493289, -47.2280780139089, -5.3544131743844, -13.5149045679179, 
-11.8490354117997, 2.61900564732815, 1.64080876266698, 0.838626506099615, 
-9.45147333982796, -15.7871734728185, 15.4485400734317, -90.356291029689, 
26.378846246797, -15.9254881070835, -73.1516790739529, 4.37904928681832, 
-25.8355046579399, -60.681020004327, -25.5121071201899, 12.9039316351527, 
-77.3079376747483, 17.7357851940081, 32.0617244758158, -31.5362297996971, 
20.1172481248217, -9.20179807632829, 4.44290891107912, -30.4769166186184, 
0.530996770480542, -24.2089094930118, 1.63471742745606, -11.3401250803407, 
22.7973056601042, -17.0124053605953, -23.4355542332496, -5.74680866152777, 
3.4311588960326, -14.0651809962911, -2.16900865369367, -17.762180937388, 
-10.1557711088582, 15.1450255188627, 2.57354731592583, 23.0963765473911, 
1.3771279182161, -4.67423975363603, 18.5420421804193, -18.051686128854, 
27.6237378942084, 2.3785462516523, 2.92423222381474, 7.62781723251896, 
-12.1585704921549, -82.4949395373291, -15.666740461142, -14.9496346120252, 
1.50868105944227, 5.65555229466268, 4.13131714702939, -1.66560607263864, 
10.4773134271374, -34.1104533773862, 3.08365430478093, 10.5141615712954, 
12.680625872974, 0.65857938255904, 3.66577601206578, -1.44819682962014, 
-30.6212669683258, 7.10453515425672, 0.987916356324803, -8.11416821762742, 
11.7615540876035, 7.89675173454884, -5.43471637087138, 9.57544859190664, 
21.5031513919239, -11.145717255052, -1.73520831844985, 6.89159609554277, 
0.283359196457525, 7.86790946466895, 4.60646149733572, -2.64430867425072, 
-2.89260410063248, -3.1977182039986, 0.584196309822886, -0.663850572811079, 
-6.99114536268952, 6.88398302823881, 33.2127100430253, 15.3891179185055, 
1.34266638482703, -2.37259238684211, 3.52704743268192, 6.80633492326641, 
9.25367081284649, 2.18239836000144, 9.9251406627864, -40.3995351141257, 
8.17751788441444, -0.627404031817193, 17.8244950024312, -36.1461177727651, 
-6.39089633489689, 13.0094367289943, 12.0058326949306, -2.70698595061301, 
7.51913367554918, -7.68890664977582, -2.92827735612275, 17.2961276991586, 
10.73357529827, -6.33931127813437, -1.65426800462549, -3.07092621291726
), Cultivar = c("C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", 
"C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", 
"C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", 
"C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", 
"C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", 
"C3", "C3", "C3", "C3", "C3", "C3", "C3", "C3", "C2", "C2", "C2", 
"C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", 
"C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", 
"C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", 
"C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", 
"C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", 
"C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", 
"C2", "C2", "C2", "C2", "C2", "C2", "C1", "C1", "C1", "C1", "C1", 
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", 
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", 
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", 
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", 
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1"
), Treatment = c("T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", 
"T1", "T1", "T1", "T1", "T1", "T1", "T1", "T2", "T2", "T2", "T2", 
"T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", 
"T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", 
"T3", "T3", "T3", "T3", "T4", "T4", "T4", "T4", "T4", "T4", "T4", 
"T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T1", "T1", "T1", 
"T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", 
"T1", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", 
"T2", "T2", "T2", "T2", "T2", "T3", "T3", "T3", "T3", "T3", "T3", 
"T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T4", "T4", 
"T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", 
"T4", "T4", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", 
"T5", "T5", "T5", "T5", "T5", "T5", "T2", "T2", "T2", "T2", "T2", 
"T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T3", 
"T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", "T3", 
"T3", "T3", "T3", "T4", "T4", "T4", "T4", "T4", "T4", "T4", "T4", 
"T4", "T4", "T4", "T4", "T4", "T4", "T4", "T5", "T5", "T5", "T5", 
"T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5", "T5"
)), class = "data.frame", row.names = c(NA, -195L))
#Computing ANOVA and Tukey
res.aov <- aov(V ~ Cultivar*Treatment, data = DATAFRAME)
TK<-TukeyHSD(res.aov)

#Results in interaction with some NA values as shown below
$`Cultivar:Treatment`
                   diff        lwr       upr     p adj
C2:T1-C1:T1          NA         NA        NA        NA
C3:T1-C1:T1          NA         NA        NA        NA
C1:T2-C1:T1          NA         NA        NA        NA
C2:T2-C1:T1          NA         NA        NA        NA
C3:T2-C1:T1          NA         NA        NA        NA
C1:T3-C1:T1          NA         NA        NA        NA
C2:T3-C1:T1          NA         NA        NA        NA
C3:T3-C1:T1          NA         NA        NA        NA
C1:T4-C1:T1          NA         NA        NA        NA
C2:T4-C1:T1          NA         NA        NA        NA
C3:T4-C1:T1          NA         NA        NA        NA
C1:T5-C1:T1          NA         NA        NA        NA
C2:T5-C1:T1          NA         NA        NA        NA
C3:T5-C1:T1          NA         NA        NA        NA
C3:T1-C2:T1  10.5738392 -19.310933 40.458611 0.9965578
C1:T2-C2:T1  13.9292675 -15.955505 43.814040 0.9561317
C2:T2-C2:T1  -0.0882804 -29.973052 29.796492 1.0000000
C3:T2-C2:T1   6.3001587 -23.584613 36.184931 0.9999914
C1:T3-C2:T1  16.7692143 -13.115558 46.653986 0.8350577
C2:T3-C2:T1  -5.3076698 -35.192442 24.577102 0.9999990
C3:T3-C2:T1   3.6524223 -26.232350 33.537194 1.0000000
C1:T4-C2:T1  17.5449389 -12.339833 47.429711 0.7845877
C2:T4-C2:T1   6.1853767 -23.699395 36.070149 0.9999932
C3:T4-C2:T1  19.8358887 -10.048883 49.720661 0.6048081
C1:T5-C2:T1  15.1434621 -14.741310 45.028234 0.9167720
C2:T5-C2:T1  11.3072711 -18.577501 41.192043 0.9932505
C3:T5-C2:T1          NA         NA        NA        NA
C1:T2-C3:T1   3.3554283 -26.529344 33.240200 1.0000000
C2:T2-C3:T1 -10.6621196 -40.546892 19.222652 0.9962519
C3:T2-C3:T1  -4.2736805 -34.158453 25.611092 0.9999999
C1:T3-C3:T1   6.1953751 -23.689397 36.080147 0.9999931
C2:T3-C3:T1 -15.8815090 -45.766281 14.003263 0.8838835
C3:T3-C3:T1  -6.9214170 -36.806189 22.963355 0.9999725
C1:T4-C3:T1   6.9710997 -22.913672 36.855872 0.9999699
C2:T4-C3:T1  -4.3884626 -34.273235 25.496309 0.9999999
C3:T4-C3:T1   9.2620494 -20.622723 39.146821 0.9991630
C1:T5-C3:T1   4.5696228 -25.315149 34.454395 0.9999999
C2:T5-C3:T1   0.7334318 -29.151340 30.618204 1.0000000
C3:T5-C3:T1          NA         NA        NA        NA
C2:T2-C1:T2 -14.0175479 -43.902320 15.867224 0.9538463
C3:T2-C1:T2  -7.6291088 -37.513881 22.255663 0.9999104
C1:T3-C1:T2   2.8399468 -27.044825 32.724719 1.0000000
C2:T3-C1:T2 -19.2369373 -49.121709 10.647835 0.6548158
C3:T3-C1:T2 -10.2768453 -40.161617 19.607927 0.9974377
C1:T4-C1:T2   3.6156714 -26.269101 33.500443 1.0000000
C2:T4-C1:T2  -7.7438909 -37.628663 22.140881 0.9998930
C3:T4-C1:T2   5.9066211 -23.978151 35.791393 0.9999962
C1:T5-C1:T2   1.2141945 -28.670578 31.098967 1.0000000
C2:T5-C1:T2  -2.6219965 -32.506769 27.262776 1.0000000
C3:T5-C1:T2          NA         NA        NA        NA
C3:T2-C2:T2   6.3884391 -23.496333 36.273211 0.9999898
C1:T3-C2:T2  16.8574947 -13.027277 46.742267 0.8296653
C2:T3-C2:T2  -5.2193894 -35.104161 24.665383 0.9999992
C3:T3-C2:T2   3.7407027 -26.144069 33.625475 1.0000000
C1:T4-C2:T2  17.6332193 -12.251553 47.517991 0.7784245
C2:T4-C2:T2   6.2736571 -23.611115 36.158429 0.9999919
C3:T4-C2:T2  19.9241691  -9.960603 49.808941 0.5973515
C1:T5-C2:T2  15.2317425 -14.653030 45.116515 0.9132071
C2:T5-C2:T2  11.3955515 -18.489221 41.280324 0.9927172
C3:T5-C2:T2          NA         NA        NA        NA
C1:T3-C3:T2  10.4690556 -19.415716 40.353828 0.9968935
C2:T3-C3:T2 -11.6078285 -41.492601 18.276944 0.9912924
C3:T3-C3:T2  -2.6477365 -32.532509 27.237036 1.0000000
C1:T4-C3:T2  11.2447802 -18.639992 41.129552 0.9936082
C2:T4-C3:T2  -0.1147821 -29.999554 29.769990 1.0000000
C3:T4-C3:T2  13.5357299 -16.349042 43.420502 0.9653209
C1:T5-C3:T2   8.8433033 -21.041469 38.728075 0.9995009
C2:T5-C3:T2   5.0071123 -24.877660 34.891884 0.9999996
C3:T5-C3:T2          NA         NA        NA        NA
C2:T3-C1:T3 -22.0768841 -51.961656  7.807888 0.4175833
C3:T3-C1:T3 -13.1167920 -43.001564 16.767980 0.9734332
C1:T4-C1:T3   0.7757246 -29.109047 30.660497 1.0000000
C2:T4-C1:T3 -10.5838376 -40.468610 19.300934 0.9965243
C3:T4-C1:T3   3.0666744 -26.818098 32.951446 1.0000000
C1:T5-C1:T3  -1.6257522 -31.510524 28.259020 1.0000000
C2:T5-C1:T3  -5.4619433 -35.346715 24.422829 0.9999986
C3:T5-C1:T3          NA         NA        NA        NA
C3:T3-C2:T3   8.9600921 -20.924680 38.844864 0.9994215
C1:T4-C2:T3  22.8526087  -7.032163 52.737381 0.3576718
C2:T4-C2:T3  11.4930465 -18.391726 41.377819 0.9920886
C3:T4-C2:T3  25.1435585  -4.741214 55.028331 0.2096972
C1:T5-C2:T3  20.4511319  -9.433640 50.335904 0.5526465
C2:T5-C2:T3  16.6149408 -13.269831 46.499713 0.8442537
C3:T5-C2:T3          NA         NA        NA        NA
C1:T4-C3:T3  13.8925167 -15.992255 43.777289 0.9570582
C2:T4-C3:T3   2.5329544 -27.351818 32.417726 1.0000000
C3:T4-C3:T3  16.1834664 -13.701306 46.068238 0.8683985
C1:T5-C3:T3  11.4910398 -18.393732 41.375812 0.9921020
C2:T5-C3:T3   7.6548488 -22.229923 37.539621 0.9999068
C3:T5-C3:T3          NA         NA        NA        NA
C2:T4-C1:T4 -11.3595623 -41.244334 18.525210 0.9929387
C3:T4-C1:T4   2.2909497 -27.593822 32.175722 1.0000000
C1:T5-C1:T4  -2.4014769 -32.286249 27.483295 1.0000000
C2:T5-C1:T4  -6.2376679 -36.122440 23.647104 0.9999925
C3:T5-C1:T4          NA         NA        NA        NA
C3:T4-C2:T4  13.6505120 -16.234260 43.535284 0.9628046
C1:T5-C2:T4   8.9580854 -20.926687 38.842857 0.9994229
C2:T5-C2:T4   5.1218944 -24.762878 35.006666 0.9999994
C3:T5-C2:T4          NA         NA        NA        NA
C1:T5-C3:T4  -4.6924266 -34.577199 25.192345 0.9999998
C2:T5-C3:T4  -8.5286176 -38.413390 21.356154 0.9996696
C3:T5-C3:T4          NA         NA        NA        NA
C2:T5-C1:T5  -3.8361910 -33.720963 26.048581 1.0000000
C3:T5-C1:T5          NA         NA        NA        NA
C3:T5-C2:T5          NA         NA        NA        NA

使用此 TukeyHSD 输出计算字母会产生:

cld <- multcompLetters4(res.aov, TK)
Error in vec2mat(x) : 27 NAs not allowed, found in x

我煞费苦心地找到了一个解决方案,从 TukeyHSD 结果的交互中删除 NA:

  m <- c()
  i<-0
  critical<-length(TK[["Cultivar:Treatment"]])/4
  for (row in TK[["Cultivar:Treatment"]]){
    i<-i+1
    max<-ceiling(i)
    if(is.na(row)){
      m <- c(m,max)}
    if(i == critical){
      i<-0}}
  m<-unique(m)
  
  TK[["Cultivar:Treatment"]] <- TK[["Cultivar:Treatment"]][-m,]

#Results in 
$`Cultivar:Treatment`
                   diff        lwr       upr     p adj
C3:T1-C2:T1  10.5738392 -19.310933 40.458611 0.9965578
C1:T2-C2:T1  13.9292675 -15.955505 43.814040 0.9561317
C2:T2-C2:T1  -0.0882804 -29.973052 29.796492 1.0000000
C3:T2-C2:T1   6.3001587 -23.584613 36.184931 0.9999914
C1:T3-C2:T1  16.7692143 -13.115558 46.653986 0.8350577
C2:T3-C2:T1  -5.3076698 -35.192442 24.577102 0.9999990
C3:T3-C2:T1   3.6524223 -26.232350 33.537194 1.0000000
C1:T4-C2:T1  17.5449389 -12.339833 47.429711 0.7845877
C2:T4-C2:T1   6.1853767 -23.699395 36.070149 0.9999932
C3:T4-C2:T1  19.8358887 -10.048883 49.720661 0.6048081
C1:T5-C2:T1  15.1434621 -14.741310 45.028234 0.9167720
C2:T5-C2:T1  11.3072711 -18.577501 41.192043 0.9932505
C1:T2-C3:T1   3.3554283 -26.529344 33.240200 1.0000000
C2:T2-C3:T1 -10.6621196 -40.546892 19.222652 0.9962519
C3:T2-C3:T1  -4.2736805 -34.158453 25.611092 0.9999999
C1:T3-C3:T1   6.1953751 -23.689397 36.080147 0.9999931
C2:T3-C3:T1 -15.8815090 -45.766281 14.003263 0.8838835
C3:T3-C3:T1  -6.9214170 -36.806189 22.963355 0.9999725
C1:T4-C3:T1   6.9710997 -22.913672 36.855872 0.9999699
C2:T4-C3:T1  -4.3884626 -34.273235 25.496309 0.9999999
C3:T4-C3:T1   9.2620494 -20.622723 39.146821 0.9991630
C1:T5-C3:T1   4.5696228 -25.315149 34.454395 0.9999999
C2:T5-C3:T1   0.7334318 -29.151340 30.618204 1.0000000
C2:T2-C1:T2 -14.0175479 -43.902320 15.867224 0.9538463
C3:T2-C1:T2  -7.6291088 -37.513881 22.255663 0.9999104
C1:T3-C1:T2   2.8399468 -27.044825 32.724719 1.0000000
C2:T3-C1:T2 -19.2369373 -49.121709 10.647835 0.6548158
C3:T3-C1:T2 -10.2768453 -40.161617 19.607927 0.9974377
C1:T4-C1:T2   3.6156714 -26.269101 33.500443 1.0000000
C2:T4-C1:T2  -7.7438909 -37.628663 22.140881 0.9998930
C3:T4-C1:T2   5.9066211 -23.978151 35.791393 0.9999962
C1:T5-C1:T2   1.2141945 -28.670578 31.098967 1.0000000
C2:T5-C1:T2  -2.6219965 -32.506769 27.262776 1.0000000
C3:T2-C2:T2   6.3884391 -23.496333 36.273211 0.9999898
C1:T3-C2:T2  16.8574947 -13.027277 46.742267 0.8296653
C2:T3-C2:T2  -5.2193894 -35.104161 24.665383 0.9999992
C3:T3-C2:T2   3.7407027 -26.144069 33.625475 1.0000000
C1:T4-C2:T2  17.6332193 -12.251553 47.517991 0.7784245
C2:T4-C2:T2   6.2736571 -23.611115 36.158429 0.9999919
C3:T4-C2:T2  19.9241691  -9.960603 49.808941 0.5973515
C1:T5-C2:T2  15.2317425 -14.653030 45.116515 0.9132071
C2:T5-C2:T2  11.3955515 -18.489221 41.280324 0.9927172
C1:T3-C3:T2  10.4690556 -19.415716 40.353828 0.9968935
C2:T3-C3:T2 -11.6078285 -41.492601 18.276944 0.9912924
C3:T3-C3:T2  -2.6477365 -32.532509 27.237036 1.0000000
C1:T4-C3:T2  11.2447802 -18.639992 41.129552 0.9936082
C2:T4-C3:T2  -0.1147821 -29.999554 29.769990 1.0000000
C3:T4-C3:T2  13.5357299 -16.349042 43.420502 0.9653209
C1:T5-C3:T2   8.8433033 -21.041469 38.728075 0.9995009
C2:T5-C3:T2   5.0071123 -24.877660 34.891884 0.9999996
C2:T3-C1:T3 -22.0768841 -51.961656  7.807888 0.4175833
C3:T3-C1:T3 -13.1167920 -43.001564 16.767980 0.9734332
C1:T4-C1:T3   0.7757246 -29.109047 30.660497 1.0000000
C2:T4-C1:T3 -10.5838376 -40.468610 19.300934 0.9965243
C3:T4-C1:T3   3.0666744 -26.818098 32.951446 1.0000000
C1:T5-C1:T3  -1.6257522 -31.510524 28.259020 1.0000000
C2:T5-C1:T3  -5.4619433 -35.346715 24.422829 0.9999986
C3:T3-C2:T3   8.9600921 -20.924680 38.844864 0.9994215
C1:T4-C2:T3  22.8526087  -7.032163 52.737381 0.3576718
C2:T4-C2:T3  11.4930465 -18.391726 41.377819 0.9920886
C3:T4-C2:T3  25.1435585  -4.741214 55.028331 0.2096972
C1:T5-C2:T3  20.4511319  -9.433640 50.335904 0.5526465
C2:T5-C2:T3  16.6149408 -13.269831 46.499713 0.8442537
C1:T4-C3:T3  13.8925167 -15.992255 43.777289 0.9570582
C2:T4-C3:T3   2.5329544 -27.351818 32.417726 1.0000000
C3:T4-C3:T3  16.1834664 -13.701306 46.068238 0.8683985
C1:T5-C3:T3  11.4910398 -18.393732 41.375812 0.9921020
C2:T5-C3:T3   7.6548488 -22.229923 37.539621 0.9999068
C2:T4-C1:T4 -11.3595623 -41.244334 18.525210 0.9929387
C3:T4-C1:T4   2.2909497 -27.593822 32.175722 1.0000000
C1:T5-C1:T4  -2.4014769 -32.286249 27.483295 1.0000000
C2:T5-C1:T4  -6.2376679 -36.122440 23.647104 0.9999925
C3:T4-C2:T4  13.6505120 -16.234260 43.535284 0.9628046
C1:T5-C2:T4   8.9580854 -20.926687 38.842857 0.9994229
C2:T5-C2:T4   5.1218944 -24.762878 35.006666 0.9999994
C1:T5-C3:T4  -4.6924266 -34.577199 25.192345 0.9999998
C2:T5-C3:T4  -8.5286176 -38.413390 21.356154 0.9996696
C2:T5-C1:T5  -3.8361910 -33.720963 26.048581 1.0000000

再次运行字母计算结果:

cld <- multcompLetters4(res.aov, TK)
Error in value[Lvls, Lvls] : subscript out of bounds

#Debugging this error shows it is raised during 
#multcompLetters2(x$formula, x$p, data, ....)
function (formula, x, data, ...) 
{
  fm <- as.character(formula)
  fm <- fm[-1]
  fm <- strsplit(fm, ":", fixed = TRUE)
  y.z <- tapply(data[, fm[[1]]], data[, fm[[2]]], function(x) do.call(mean, 
    list(x = x)))
  oz <- order(y.z, decreasing = T)
  if (length(fm[[2]] > 1)) {
    Lvls <- levels(interaction(data[, fm[[2]]], sep = ":"))[oz]
  }
  else {
    Lvls <- levels(data[, fm[[2]]])[oz]
  }
  value <- vec2mat(x)
  value <- value[Lvls, Lvls]
  multcompLetters(value, ...)
}

我只想在我的 ggplot 中添加一些字母...

好的,所以我整合了@Allan Cameron 的评论,他的方法确实有效:

TK$`Cultivar:Treatment`[!complete.cases(TK$`Cultivar:Treatment`),] <- 0
cld <- multcompLetters4(res.aov, TK)
cld <- as.data.frame.list(cld$`Cultivar:Treatment`)
DATAFRAME$Tukey<- NA
#Loop over rows of cld and add them to our DATAFRAME
for (row in 1:nrow(cld)) {
  for (diff_row in 1:nrow(DATAFRAME)){
    if (str_split(rownames(cld[row,]), ":")[[1]][1] == DATAFRAME[diff_row,]$Cultivar &
        str_split(rownames(cld[row,]), ":")[[1]][2] ==DATAFRAME[diff_row,]$Treatment){
      DATAFRAME[diff_row,]$Tukey<-cld[row,]$Letters
    }}}

ggplot(DATAFRAME, aes(x=Treatment, y=V, fill=Treatment))+
  geom_bar(stat="summary", fun="mean", position="dodge", colour="black")+
  stat_summary(geom = "errorbar", width = .2, position=position_dodge(0.9)) +
  theme_classic()+
  facet_wrap(~Cultivar)+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
  stat_summary(aes(label=Tukey), fun = Top_of_SE, geom = "text", size = 4, vjust = -0.3)+
  guides(fill="none")+
  xlab("Treatment")+
  ylab("V values")+
  ggtitle(paste("Treatment V"))

Example of ggplot

我仍然想知道,这会影响字母显示的结果还是根本不影响?不管怎样,谢谢你:)希望它将来能帮别人省去麻烦!

r ggplot2 statistics anova tukey
1个回答
0
投票

我也遇到同样的问题,请问你找到解决办法了吗? 谢谢

© www.soinside.com 2019 - 2024. All rights reserved.