基础R中的ggbiplot和biplot产生两个不同的biplots

时间:2014-08-01 19:33:21

标签: r ggbiplot

基地ggbiplot中的

biplotR正在产生两个不同的双点。我在这里错过了什么吗?在此先感谢您的帮助。

代码

library(ggbiplot)
p <- ggbiplot(
    prcomp(dat, scale. = FALSE)
  , obs.scale = 1
  , var.scale = 1
  , labels =  rownames(dat)
  , pc.biplot = FALSE
  )
print(p)

biplot(prcomp(dat, scale. = FALSE))

输出 enter image description here

enter image description here

数据

dat <-
structure(c(-123.88909334375, 403.48715665625, 181.66403165625,
330.58653165625, 79.3502816562503, -297.19034334375, 147.59653165625,
-526.13492646875, -448.60534334375, 147.69590665625, -95.4691014687497,
91.1134066562503, 275.77986478125, 508.57653165625, -713.09159334375,
-37.6997183437503, 251.50528165625, 223.24653165625, -59.6047183437501,
-338.917218343751, 639.43132290625, 962.63257290625, 132.50944790625,
-676.94305209375, 280.14569790625, 1028.65507290625, -26.5580520937498,
-471.13951021875, -122.75992709375, 202.71632290625, 22.0179897812498,
-812.21617709375, 289.15028103125, 355.27194790625, 362.77882290625,
-482.70430209375, -380.99930209375, -1200.95805209375, -337.95930209375,
236.92819790625, 915.44132290625, 74.8425729062499, -345.73055209375,
-225.98305209375, 365.73069790625, -769.58492709375, -88.9230520937501,
-158.92951021875, 1054.47507290625, 914.07632290625, -926.64701021875,
139.61882290625, -1081.18971896875, -1465.49305209375, -762.96117709375,
152.85569790625, 569.13569790625, 734.22694790625, 657.60069790625,
247.43819790625, -331.18117709375, -408.00492709375, -150.62805209375,
164.91944790625, 17.8581979062501, 516.36757290625, 2.82944790624992,
183.27298978125, 10.7525729062502, -163.74617709375, 613.88048978125,
-137.00367709375, -202.31221896875, 384.60944790625, -24.5086770937498,
46.6831979062499, 2.53819790625016, -275.74555209375, -141.87180209375,
-108.70930209375, 330.48132290625, 1728.70757290625, 2173.55944790625,
-619.24305209375, -595.47930209375, -984.51992709375, 1877.01694790625,
-659.26451021875, -373.40992709375, 1027.09132290625, -353.60701021875,
-321.14117709375, -494.82471896875, 1596.32194790625, -712.84617709375,
-841.67930209375, 176.725697906251, -680.73305209375, -1151.08430209375,
-1122.07180209375, -268.68992709375, -1553.83867709375, 141.06319790625,
498.26069790625, 142.87444790625, 808.03382290625, 336.170697906251,
-1087.61076021875, 469.118822906251, -476.27992709375, -406.97826021875,
238.01257290625, -264.82096896875, 822.12569790625, 392.15757290625,
67.4994479062494, 606.70444790625, -171.57930209375, -225.20555209375,
-67.0180520937506, -216.15492709375, -605.45367709375, -118.90180209375,
-53.3543020937496, 7.90944790624999, 298.06882290625, 230.35569790625,
-160.05076021875, -207.52117709375, 105.45507290625, 437.28173978125,
-21.9774270937502, -154.78596896875, 116.33569790625, -305.33242709375,
640.90944790625, -257.43555209375, 372.65569790625, -414.37055209375,
306.36694790625, 85.4525729062497, 50.3038229062498, 516.03069790625,
477.45319790625, 80.3669479062504, -129.47367709375, -309.68680209375,
308.25673978125, 260.78632290625, -684.61242709375, 9.71423978125028,
133.85507290625, -264.82846896875, 72.1431979062498, -170.34992709375,
109.19194790625, -309.97805209375, 78.46319790625, -287.71305209375,
-25.3755520937503, -800.64242709375, 171.15882290625, 268.81069790625,
1.0581979062506, -48.8030520937502, 26.0813229062503, -418.05680209375,
547.161739781249, -250.33367709375, 533.49257290625, -633.33076021875,
11.6350729062499, 321.27653103125, -209.80180209375, -139.79492709375,
-193.60305209375, -1175.27305209375, 956.21819790625, 666.441947906251,
366.30444790625, -644.75492709375, -436.82867709375, 834.44819790625,
-16.6543020937498, -260.94055209375, -338.85617709375, 536.50569790625,
182.19923978125, -337.496177093749, 203.25507290625, 147.58173978125,
98.0975729062501, 407.73903103125, -505.31430209375, -18.6324270937499,
274.80944790625, -186.01055209375, 70.4556979062503, 579.30444790625,
-588.90805209375, 17.58507290625, 17.1613229062497, 316.21319790625,
541.53569790625, 651.39944790625, -2.91617709374975, -613.70430209375,
-558.26076021875, 519.26882290625, 365.59507290625, -162.30326021875,
79.8625729062502, -784.09596896875, -1113.79930209375, -522.96742709375,
13.4994479062499, 316.60444790625, 79.99569790625, 425.66944790625,
413.65694790625, 510.48132290625, 648.95757290625, -1864.54055209375,
-708.66805209375, -841.85430209375, 496.90507290625, -426.35805209375,
-850.11451021875, 178.84007290625, 1601.51632290625, -1141.65701021875,
628.28382290625, -103.69971896875, -155.65305209375, 303.22882290625,
471.69569790625, -558.57430209375, 878.44194790625, 727.56569790625,
205.20319790625, -106.18242709375, -162.18117709375, 11.8706979062501,
-47.5318020937495, -152.96805209375, -383.63367709375, 206.97819790625,
-8.42826021875021, 48.2513229062499, 61.2525729062497, -119.44576021875,
879.67007290625, 85.1615310312502, 255.45819790625, -507.85992709375,
125.88194790625, 81.7119479062501, -134.07180209375, -62.7230520937499,
-71.2105520937505, 613.60882290625, -1171.53992709375, -80.8380520937498,
193.05944790625, -141.52680209375, 273.63257290625, 176.74444790625,
65.5379897812496, 580.592572906251, -468.95617709375, -191.32951021875,
141.13632290625, 263.32778103125, 162.74944790625, -392.21867709375,
33.1481979062497, 197.32819790625, 294.06944790625, -384.60680209375,
-163.91930209375, -326.00492709375, -694.45367709375, 125.42319790625,
232.64569790625, 210.58444790625, 63.2438229062502, 49.7056979062501,
-790.72576021875, -150.69617709375, 28.95507290625, 119.10673978125,
284.87257290625, 52.86403103125, 306.51069790625, 84.8425729062498,
197.73444790625, 28.56444790625, -145.51930209375, 238.32944790625,
84.0169479062499, 288.60882290625, -59.0399270937498, -280.86305209375,
180.58444790625, 408.49819790625, -363.86742709375, -169.05555209375,
178.03798978125, 568.06757290625, 143.543822906249, -78.8295102187499,
24.46132290625, -582.54721896875, 4.42444790624972, -113.06867709375,
-312.70180209375, -190.19680209375, -197.58055209375, 373.76819790625,
177.75569790625, -363.48617709375, 226.36507290625, -287.08305209375,
486.78944790625, -97.77180209375, 88.21257290625, -342.00055209375,
213.44298978125, -21.5274270937498, 33.1488229062502, 456.62548978125,
-169.28367709375, 202.90778103125, -501.84555209375, -140.13867709375,
-193.92180209375, 720.23319790625, -308.00055209375, 54.9981979062504,
-57.6643020937504, 72.9688229062499, -87.1799270937499, -183.95305209375,
69.09444790625, -452.96680209375, -79.4824270937497, -30.5205520937502,
191.57298978125, -85.0474270937498, -447.12117709375, 101.38048978125,
267.17132290625, 181.01278103125, 330.48444790625, -57.85867709375,
180.03319790625, 198.33819790625, 149.30444790625, -362.69680209375,
45.4656979062497, 186.520072906249, 984.721322906251, -507.90180209375,
-150.70430209375, 35.5844479062506, -70.1061770937496, -625.31930209375,
1846.82423978125, -471.54617709375, -146.06992709375, -180.91826021875,
-431.82742709375, -247.11096896875, -430.98930209375, 139.04257290625,
-623.11555209375, 353.589447906251, -487.16930209375, 292.50444790625,
533.99194790625, -1162.08992709375, -16.68867709375, -273.21180209375,
820.18569790625, 506.42444790625, 115.98382290625, -328.10430209375,
-57.38576021875, -403.45617709375, -175.22992709375, -473.95326021875,
379.33757290625, 307.05403103125, -501.82430209375, 366.80757290625,
-173.12555209375, 164.67944790625, 421.14569790625, 721.66944790625,
-238.21805209375, -506.73867709375, 187.33757290625, -381.71055209375,
572.76194790625, 606.22569790625, -409.18992709375, -297.70305209375,
-27.0095102187503, -199.47992709375, 150.32132290625, 320.32298978125,
409.71382290625, 163.83028103125, -610.32305209375, 431.90882290625,
-212.19930209375, 264.53069790625, -1076.25305209375, 752.04569790625,
-138.39180209375, 118.78757290625, -312.16117709375, -96.4593020937499,
385.76319790625, -448.79805209375, 320.53632290625, -47.1768020937501,
91.5667397812496, 523.29632290625, 15.3975729062496, 22.2242397812503,
292.16507290625, -127.31846896875, 22.1281979062497, 29.6350729062498,
350.85194790625, -297.44305209375, -179.87680209375, -337.72805209375,
-325.39055209375, 835.49757290625, -658.02617709375, -546.47430209375,
1123.29819790625, -1169.66305209375, 99.6963229062504, -184.66680209375,
99.9017397812496, 844.10632290625, -1017.94242709375, 72.2092397812499,
175.525072906251, 526.86653103125, -407.06180209375, 433.82007290625,
234.18694790625, 356.69194790625, -296.56680209375, -371.04305209375,
-150.355552093751, -530.46701084375, 38.5342391562499, -135.01388584375,
226.63361415625, 2.77236415625021, -159.91826084375, -17.7813858437503,
-281.38784396875, 50.4917391562503, 129.29298915625, 247.96965603125,
107.78548915625, -229.43137771875, 702.87361415625, 411.05548915625,
210.32236415625, -398.49763584375, -41.9063858437498, 75.5173641562501,
-408.84513584375, 194.32632290625, 430.02757290625, 206.75444790625,
68.4519479062499, 229.39069790625, -797.42492709375, -190.43805209375,
768.305489781251, -414.61492709375, 354.33632290625, -347.21201021875,
326.02882290625, -562.20471896875, -604.75805209375, 111.12382290625,
-758.18430209375, 29.5456979062499, 458.13694790625, 237.83569790625,
260.57319790625, -23.90367709375, -47.4524270937495, -60.2505520937498,
101.19694790625, 247.11069790625, 92.4950729062498, -65.9180520937493,
-504.82451021875, -152.16992709375, 359.48132290625, -86.7420102187498,
-223.55117709375, -274.30971896875, 635.03694790625, 240.11882290625,
-84.18930209375, -243.10930209375, -269.04305209375, 302.98069790625,
57.04319790625, 179.20632290625, -126.74242709375, 5.60944790625013,
446.20694790625, 132.47069790625, -14.8699270937498, -195.10805209375,
401.98548978125, 167.04007290625, 263.34132290625, -271.55701021875,
-376.59117709375, -441.92471896875, -1479.97805209375, 6.72882290625034,
-567.90430209375, 450.42569790625, -77.83305209375, 826.915697906251,
672.57819790625, 210.46757290625, 821.168822906251, 120.19569790625,
-501.73180209375, 142.85694790625, -46.1586770937498, -101.37180209375,
1016.59673978125, 489.95132290625, 2.90257290624993, 72.2292397812502,
-74.4799270937502, -327.31346896875, -1427.89180209375, 246.34007290625,
-140.81805209375, -414.16305209375, 57.62819790625, -79.3980520937499,
-67.0105520937501, -138.47492709375, 388.87632290625, 187.90319790625,
295.15069790625, 564.73944790625, -332.65117709375, -304.48930209375,
167.62923978125, -88.1911770937498, -137.71492709375, -26.7382602187498,
451.577572906251, -72.1309689687499, -193.509302093751, 1.54757290624968,
-302.26055209375, 132.74444790625, 437.83569790625, -574.16555209375,
-457.67805209375, 115.69257290625, -148.60617709375, -57.9293020937502,
-75.7068020937499, 131.40694790625, 67.3663229062504, -91.9718020937498,
171.82173978125, 186.82632290625, -571.07242709375, 102.45423978125,
39.04507290625, 161.21153103125, 227.33319790625, 443.19007290625,
243.55694790625, -175.58805209375, -266.34680209375, -611.69805209375,
109.01444790625, 82.3675729062495, -556.95617709375, -403.75430209375,
-129.85680209375, 285.58194790625, -236.75867709375, -437.82180209375,
-319.90326021875, 424.32632290625, 708.10257290625, 381.60423978125,
359.89507290625, -455.46346896875, 110.63319790625, -69.2849270937502,
22.7319479062496, 457.73694790625, -466.34680209375, 313.32694790625,
-70.1605520937503, -385.14242709375, -349.46617709375, 170.41069790625,
69.3331979062505, 338.92194790625, -58.4686770937496, -51.1568020937502,
462.61173978125, -272.33367709375, -196.88242709375, 372.41923978125,
142.38507290625, 514.57653103125, 59.8231979062498, 88.1800729062502,
76.0469479062499, -209.74805209375, 107.84319790625, -133.33305209375,
-746.02055209375, -43.7086770937502, -349.68242709375, 282.71944790625,
139.94194790625, 76.2056979062501, 303.86507290625, -22.1980520937503,
929.09548978125, -201.69992709375, -772.07367709375, -202.77201021875,
-141.15617709375, 210.16028103125, 234.63194790625, 96.2888229062498,
21.6556979062495, 394.21069790625, -259.07305209375, -646.04930209375,
-50.36180209375, -287.19742709375, -198.17117709375, -78.29430209375,
-75.2218020937498, 56.8669479062501, 392.85132290625, -112.36180209375,
330.58173978125, -208.56367709375, 54.4125729062499, 911.23923978125,
-147.99492709375, 15.84653103125, 234.46819790625, -35.5249270937501,
31.5169479062498, -4.30305209374995, -307.56180209375, -248.71305209375,
-323.87555209375, 97.9025729062498, -259.57117709375, -266.69430209375,
-56.6218020937497, -450.53305209375, -91.0486770937498, 55.23819790625,
-16.3182602187501, -330.46367709375, -634.98742709375, 845.33923978125,
226.10507290625, 577.44653103125, 161.56819790625, 244.57507290625,
180.61694790625, 115.29694790625, -174.96180209375, -123.61305209375,
-99.2755520937501, -130.16242709375, 307.61382290625, 278.74069790625,
159.71319790625, 131.40194790625, -178.48867709375, -35.7268020937498,
167.59173978125, 178.47132290625, -661.50242709375, -345.52576021875,
105.690072906251, -197.14346896875, 19.0031979062497, 22.33507290625,
199.77694790625, 70.2069479062503, 64.8731979062501, -199.20305209375,
42.3344479062499, 176.92757290625, 70.95382290625, -92.51930209375,
-876.94680209375, -115.68305209375, 420.35132290625, 223.41319790625,
-0.29326021875022, -22.7636770937497, 31.8625729062496, -603.01076021875,
-858.04492709375, 434.99653103125, 626.11819790625, 296.10007290625,
-124.35805209375, -631.02805209375, 311.56319790625, 191.23694790625,
541.12444790625, 472.67757290625, 721.70382290625, -183.41930209375,
-1047.84680209375, 271.74194790625, -157.27367709375, 837.51319790625,
-199.54326021875, -1113.68867709375, -70.7124270937501, 1076.11423978125,
-845.61992709375, 710.72153103125, -593.15680209375, 251.85007290625,
-18.6080520937502, 8.07194790625006, 122.31319790625, -166.33805209375,
-76.5005520937502, 634.17757290625, 323.20382290625, -111.91930209375,
-986.34680209375, -496.75805209375, 534.22632290625, -215.98680209375,
-408.04326021875, -457.18867709375, -194.21242709375, 227.61423978125,
-779.11992709375, 342.22153103125, 1418.34319790625, -156.64992709375,
602.89194790625, -655.42805209375, 503.81319790625, -179.83805209375,
54.9994479062498, -389.73909334375, -355.71284334375, 554.16403165625,
-1335.26346834375, -730.67471834375, -44.6903433437499, 1070.09653165625,
-1238.62660146875, -221.10534334375, -618.12909334375, 88.6975735312501,
-693.03659334375, 823.30486478125, 834.42653165625, -160.56659334375,
578.97528165625, 140.65528165625, 694.89653165625, -338.75471834375,
1341.08278165625), .Dim = c(20L, 40L), .Dimnames = list(c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20"), c("1", "2", "4", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42")))

1 个答案:

答案 0 :(得分:1)

查看源代码,stats:biplot.default绘制硬编码缩放为0.8的箭头:

arrows(0, 0, y[,1L] * 0.8, y[,2L] * 0.8, col = col[2L], length=arrow.len)

ggbiplot使用更加雄心勃勃的缩放(其中pcobj是您的prcomp结果)来绘制箭头:

d <- pcobj$sdev
u <- sweep(pcobj$x, 2, 1 / (d * nobs.factor), FUN = '*')
v <- pcobj$rotation
...

# Directions
v <- sweep(v, 2, d^var.scale, FUN='*')
df.v <- as.data.frame(v[, choices])
names(df.u) <- c('xvar', 'yvar')
names(df.v) <- names(df.u)
....

r <- sqrt(qchisq(circle.prob, df = 2)) * prod(colMeans(df.u^2))^(1/4)

# Scale directions
v.scale <- rowSums(v^2)
df.v <- r * df.v / sqrt(max(v.scale))
...
# Draw directions
g <- g +
  geom_segment(data = df.v,
               aes(x = 0, y = 0, xend = xvar, yend = yvar),
               arrow = arrow(length = unit(1/2, 'picas')), 
               color = muted('red'))
...