在ggplot2中使用geom_path和/或geom_line绘制主曲线
我有一个有250行的非线性矩阵mat1,并用principal_curve得到crv。使用plot(crv)时看起来正常,但在试图用tidyverse函数绘制时,又无法得到平滑的曲线。
我确实在为ggplot函数创建数据框df1时对crv数据进行了排序,但这仍未解决问题。有什么想法?



library(tidyverse)
library(princurve)
## Generate principal_curve object
crv <- principal_curve(mat1)
crv
## This produces a smooth curve
plot(mat1, col = "red")
lines(crv)
## matrix df
df <- data.frame(
dim1 = mat1[, 1],
dim2 = mat1[, 2]
)
## crv df
df1 <- data.frame(
dim1 = crv$s[, 1],
dim2 = crv$s[, 2],
ord = crv$ord
)
df1
## Order curve.df
df1 <- df1[order(df1$ord), ]
df1
## This line does not look good at all
p <- ggplot() +
geom_point(data = df,
aes(x = dim1, y = dim2), color = "red") +
geom_path(data = df1,
aes(x = dim1, y = dim2)) +
theme_classic()
p
## This line looks okay, but is not completely smooth
p <- ggplot() +
geom_point(data = df,
aes(x = dim1, y = dim2), color = "red") +
geom_line(data = df1,
aes(x = dim1, y = dim2)) +
theme_classic()
p
Code to produce mat1:
> dput(mat1)
structure(c(-4.28721795144398, 4.8702198499171, 1.18451156553905,
4.51614537176769, 6.41333784994762, 4.82243885931652, 5.42996373114269,
-4.68708024087269, 1.27805473742168, -7.55644784036, 2.26484098372142,
-5.3573716646703, 6.78396382269542, -4.33821473183949, -6.8587258821996,
5.74041762289684, -0.60474566283543, 6.22319617209117, 0.0129150384394414,
3.91681923803966, -2.24612901273091, 4.88563551840465, -7.53421769204457,
-4.72600159707386, 3.67160954413097, 4.68918766913097, 4.34896340307872,
-1.33932731213887, 6.90557780203502, -1.63675890031178, 3.35653462347667,
4.69368710455577, -5.16862044396717, 3.41125049528759, 5.00261750158947,
2.74561324057262, 3.09585442480724, 0.100183420750309, -2.32138476434071,
6.23894419607799, 4.59486641821544, -7.49990687432606, 5.81975903448742,
3.32962431845348, -3.50434646668751, 2.61300792631786, -1.72370109620411,
3.59197201666515, 0.819093727442432, 4.55276694235485, 5.40664257940929,
5.08501782354992, -4.22268638673146, -2.48345170083363, 0.424812936159778,
2.9428791039912, -4.95926079812367, -3.45313534798939, 3.08556427893322,
-0.777853733924221, 5.32996954855602, 2.14601721701305, 0.10046639380138,
5.84404387411754, 0.816818618151355, 1.64841082033794, 6.15064587530773,
5.56738390860241, 5.54450001654308, 4.74202647146862, 3.31146588263195,
4.08954348501842, -1.40920064511616, 6.99051680502575, 2.90074529585521,
3.68037333426158, -7.43348918023426, 4.0734635823695, -2.26588342252095,
3.08434071478527, -5.25175843301136, -2.62182769837696, -5.43799147668202,
-3.62534914079029, -3.9125109678777, -3.62532649102528, 0.639461540552784,
1.57677092489879, 6.44150223669689, 2.94542350706737, 2.91057934698742,
0.580815874907184, 7.32180943426769, 3.27217998442333, 0.878284239145923,
5.49466004309337, -3.94603357377369, 2.39347209868114, 6.5209676259486,
4.98212447104137, 2.16779580053966, 4.42620291647594, 2.65831007895153,
-3.16114387574513, -3.12895569863636, -2.24802491727192, 5.82021870550792,
1.27348437246959, -0.910182691243481, -3.45444521966298, -7.39553389611561,
3.11831607756298, 3.00172748503368, 1.9655419820277, 5.42066636023204,
4.76249661383312, 4.46146359381359, 4.98593964514415, 3.13596048292797,
-2.8755658632787, 4.37761845526378, 4.3815142148463, 3.15866031584423,
1.69382085737865, 1.98393549856823, -0.195481128123593, 4.75630726751964,
-7.28258214059193, -0.943080163625073, 6.06985249457042, 5.82366671499889,
3.65901484427135, 5.32417025503795, 1.87386979994457, 2.19627323088329,
2.98445906576793, -1.94063148560841, 1.40829470095317, 2.83081569609325,
-1.8592813974889, 0.457729839655567, 1.54879727301281, 5.95999207434337,
-6.94657216134388, 4.35926213202159, 2.11910118994396, 1.38549997744243,
-4.46464953484852, 6.41373839316051, 3.94981732306163, -3.06182680192311,
3.7420574658839, -2.91049466195424, -3.71450743737538, 3.10248150763195,
1.58008065161388, 3.50217618879955, -4.68559060159047, 2.74889316496532,
1.65527787146251, -3.26782188477833, 0.668868684145618, -0.663253731158566,
2.50191106733959, 3.03497018751781, -2.12206993165333, -0.829191482690167,
3.96375622687023, -0.418474069860768, -1.64916155400593, 4.66061844763439,
1.52290453848522, 3.96995177206676, 6.34706702170055, 4.19412865576427,
4.79909386572521, -1.64466998639424, -3.23279748025258, -7.44117436471302,
-4.67911038461049, 3.19786872801464, 4.3091002934901, 2.19765963492076,
-4.70068821969349, 6.8252250188319, 4.49565949377697, -3.90244445863087,
3.67902388510387, -4.5571859842809, 0.904220604273487, -4.28800330224354,
-7.07928118768055, -8.26978287759144, 6.16587128576915, 6.0725771420924,
4.40833105978649, -0.955983198312115, 4.25779786047618, 3.10419430670421,
4.76741518911998, 3.88360562262218, 2.02341952261608, 5.92827620443981,
1.33886971411388, 2.61481704649608, 1.58079650339763, 3.59270467695873,
4.62460818228405, 6.00425686773937, -3.15978751244862, 1.07318999704998,
-4.2253771788152, 1.7613126271693, -0.536423451331448, -4.2864533430608,
2.95205464300792, -2.11317238869984, -0.792936689284634, -3.70633492532093,
-4.50439963403065, -7.17934498849232, 4.39749016699474, 2.50188865599315,
-2.34567174973805, -2.24017689290364, -2.31786224904377, -1.38638398709614,
-4.70984778466542, 0.394893788668323, -2.31088385644276, 7.24636712011974,
3.77746500906627, -2.53205976548512, 5.38931145605724, 3.68597664770763,
-3.78018722596486, -3.17925105157216, 5.23040356573742, 3.03680148062389,
-0.992410278943371, 2.91206326422374, -4.37652573647816, 2.29945864615123,
-8.98096880975087, 5.99747624334972, 3.7764960759608, -2.69199309411366,
1.80712475714367, -5.10822138848622, 0.0642102890936619, 1.33239769888954,
-3.25391447590752, 0.32373475981788, -1.40630996273919, -5.60432028817101,
-1.21460783528252, -3.24259555386468, 3.67390894843177, -3.27521705674096,
3.56274294806556, -0.884971082677085, 0.406220912466806, -5.37502718018456,
5.0091764922245, 3.14487862540321, -0.989371002186972, -1.2854881887333,
-1.39199462579652, 3.12482047034339, -2.44928342150613, 6.69003128958778,
-1.08909988449975, 3.86366820288734, 5.02729201270179, 5.97459769202308,
4.91054940177039, 0.0327677722080166, 6.11097979498939, -2.96604740666314,
5.67463278723792, 5.9383904929264, 5.89243674231605, 0.652703046332162,
-1.9078483362691, -0.704249263276297, -0.323391676415641, 2.82580733252601,
-1.96184965269728, -0.772235751618582, -0.532509088982779, 5.27363562537269,
3.3014767165287, -1.11951237963601, 0.378520011435312, 3.76580309821204,
-0.858384132851798, 6.53891777945594, -0.737170815934378, 6.39239144278602,
-4.44977116631432, -0.985335588921744, 4.96236777258949, 4.37180876685218,
6.58689427329139, -0.600508809556205, 6.11041474295692, 4.89248490286903,
6.32338643027381, -2.28513011382981, -3.06363296555443, -0.388075352181632,
4.99632525397376, 4.3960583205326, -1.03256940888329, -0.495823026169974,
4.57520794821815, -1.26423674868508, -1.3749947552578, -3.76042401837273,
5.66978478385047, -0.850748658646781, -3.62488222168847, 6.94686341239051,
-2.44885277794762, -2.6210372452633, -0.489114761819083, 3.13767218543128,
-5.57178258942528, 6.92697644187049, -1.44810423301621, 2.40713238669471,
2.12628436041908, 0.390449761877816, 6.51246762229041, 5.20771384192542,
5.41026568366126, 6.3277742858036, -3.03445673035546, -3.29482352780266,
-3.92303895996972, -8.5206201081173, 0.2069451804264, -4.75344848679467,
-2.41024673031731, -2.39064896153374, -3.54496932076379, -0.885271311272818,
5.57165026618079, -2.49048805283471, -1.90373498993083, 4.90509438468055,
-0.554852128495414, -2.85683202790185, 6.62082600546912, 5.7021205420597,
-1.27630764292641, -0.414735913743216, -1.01047039078637, 6.92812657309608,
-0.920573890675742, 3.03659653616981, 0.0497897858722622, -1.52506786631509,
-0.662469149102408, -0.62211918877526, -0.194349050988395, -2.33251425670548,
-1.61478185700341, 5.33197617484168, 6.83552479697303, 5.6068737502201,
-1.25459510134621, -2.80152487801476, 5.69255185080604, -0.327126622666556,
-1.41118821548386, 5.39152789069251, 2.91887545538978, 5.04978156043128,
-2.00983341084881, -1.00053280638619, 6.26642727805213, -0.618663669099051,
4.78069138480262, -0.557345033158499, 6.08287024451331, 4.43369841528968,
-0.208670020569999, -3.97865963028832, 5.83761048270301, 6.42670845938758,
-3.39894855069085, -2.54343134211465, 3.78479790640907, 5.807478665839,
0.215088128577035, 6.08765530539588, 5.50543522788123, -4.99789619492455,
-0.956180513371665, 4.15034842444495, 4.37268042517738, 6.82259583426551,
6.28055405570106, -0.218894958962638, 4.2096364493473, -3.16674315976067,
-0.913203895558554, 6.07551455451087, -2.65221494482918, 6.3688251967533,
-4.82372307824059, 5.08581900549964, 4.80072474433021, 5.61992192221717,
-2.09668844984933, 6.54457020712928, 5.82764172507362, -1.90173455746933,
1.19448327971534, 5.41399216605262, -2.69507992314263, 0.312499284277719,
0.769992827902597, 6.02671599341468, -2.03834617184563, 7.04967808676795,
6.29921364737586, 1.67501282645301, -0.865586638917166, 6.22044396353797,
-4.81823730515404, -3.50462615536614, 1.33758711768226, -4.47318053292199,
5.99370074225501, -0.190230131615836, 3.19411253882484, 4.01888298941688,
-2.48675692128106, 1.83707809401588, -0.89175117062493, 3.3927886481388,
-5.12143564270898, -4.37826705025597, 6.05713629675941, 0.152837275992196,
-0.00849950360222507, 1.06599140120582, 5.52369236899452, 6.11098027182655,
-1.23180335806771, -2.97973287152215, 3.64432692481117, -0.442604899872977,
4.88499808264808, 5.98436236334876, 5.94076085043983, -1.7862852741973,
5.64864277793006, 5.25110220862464, 5.33949065161781, 4.60795092536048,
-1.82037842320367, 5.19977593375282, 1.54372286749915, 6.53758072806434,
6.09941935492591, 5.972921609412, 5.00346064520912, 0.540961741934579,
-1.53856328176423, -4.12188363121911, 6.74572968436317, -0.522887468804557,
6.53167653037147, 6.94941639853553, 3.96485161734657, 5.1150319571598,
7.09365391684608, -4.8791668419735, -0.137967944611747, 6.26956486655311,
-0.362965584267813, 6.0183932776554, -0.680152416695792, 6.04965710593299,
-1.75907676713629, -2.33700862573548, 0.713386535177987, -2.93490254925652,
-0.875560164918143, 0.178076028357309, 2.76988577796058, -3.83224415825768,
6.04500889731483, 4.62104392005042, -0.793814540375907, 3.46049857092933,
6.00861382437782), dim = c(250L, 2L), dimnames = list(c("1",
"2", "3", "4", "5", "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", "43", "44", "45", "46",
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90",
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
"101", "102", "103", "104", "105", "106", "107", "108", "109",
"110", "111", "112", "113", "114", "115", "116", "117", "118",
"119", "120", "121", "122", "123", "124", "125", "126", "127",
"128", "129", "130", "131", "132", "133", "134", "135", "136",
"137", "138", "139", "140", "141", "142", "143", "144", "145",
"146", "147", "148", "149", "150", "151", "152", "153", "154",
"155", "156", "157", "158", "159", "160", "161", "162", "163",
"164", "165", "166", "167", "168", "169", "170", "171", "172",
"173", "174", "175", "176", "177", "178", "179", "180", "181",
"182", "183", "184", "185", "186", "187", "188", "189", "190",
"191", "192", "193", "194", "195", "196", "197", "198", "199",
"200", "201", "202", "203", "204", "205", "206", "207", "208",
"209", "210", "211", "212", "213", "214", "215", "216", "217",
"218", "219", "220", "221", "222", "223", "224", "225", "226",
"227", "228", "229", "230", "231", "232", "233", "234", "235",
"236", "237", "238", "239", "240", "241", "242", "243", "244",
"245", "246", "247", "248", "249", "250"), c("umaprpca_1", "umaprpca_2")))
> structure(mat1)
umaprpca_1 umaprpca_2
1 -4.28721795 1.332397699
2 4.87021985 -3.253914476
3 1.18451157 0.323734760
4 4.51614537 -1.406309963
5 6.41333785 -5.604320288
6 4.82243886 -1.214607835
7 5.42996373 -3.242595554
8 -4.68708024 3.673908948
9 1.27805474 -3.275217057
10 -7.55644784 3.562742948
11 2.26484098 -0.884971083
12 -5.35737166 0.406220912
13 6.78396382 -5.375027180
14 -4.33821473 5.009176492
15 -6.85872588 3.144878625
16 5.74041762 -0.989371002
17 -0.60474566 -1.285488189
18 6.22319617 -1.391994626
19 0.01291504 3.124820470
20 3.91681924 -2.449283422
21 -2.24612901 6.690031290
22 4.88563552 -1.089099884
23 -7.53421769 3.863668203
24 -4.72600160 5.027292013
25 3.67160954 5.974597692
26 4.68918767 4.910549402
27 4.34896340 0.032767772
28 -1.33932731 6.110979795
29 6.90557780 -2.966047407
30 -1.63675890 5.674632787
31 3.35653462 5.938390493
32 4.69368710 5.892436742
33 -5.16862044 0.652703046
34 3.41125050 -1.907848336
35 5.00261750 -0.704249263
36 2.74561324 -0.323391676
37 3.09585442 2.825807333
38 0.10018342 -1.961849653
39 -2.32138476 -0.772235752
40 6.23894420 -0.532509089
41 4.59486642 5.273635625
42 -7.49990687 3.301476717
43 5.81975903 -1.119512380
44 3.32962432 0.378520011
45 -3.50434647 3.765803098
46 2.61300793 -0.858384133
47 -1.72370110 6.538917779
48 3.59197202 -0.737170816
49 0.81909373 6.392391443
50 4.55276694 -4.449771166
51 5.40664258 -0.985335589
52 5.08501782 4.962367773
53 -4.22268639 4.371808767
54 -2.48345170 6.586894273
55 0.42481294 -0.600508810
56 2.94287910 6.110414743
57 -4.95926080 4.892484903
58 -3.45313535 6.323386430
59 3.08556428 -2.285130114
60 -0.77785373 -3.063632966
61 5.32996955 -0.388075352
62 2.14601722 4.996325254
63 0.10046639 4.396058321
64 5.84404387 -1.032569409
65 0.81681862 -0.495823026
66 1.64841082 4.575207948
67 6.15064588 -1.264236749
68 5.56738391 -1.374994755
69 5.54450002 -3.760424018
70 4.74202647 5.669784784
71 3.31146588 -0.850748659
72 4.08954349 -3.624882222
73 -1.40920065 6.946863412
74 6.99051681 -2.448852778
75 2.90074530 -2.621037245
76 3.68037333 -0.489114762
77 -7.43348918 3.137672185
78 4.07346358 -5.571782589
79 -2.26588342 6.926976442
80 3.08434071 -1.448104233
81 -5.25175843 2.407132387
82 -2.62182770 2.126284360
83 -5.43799148 0.390449762
84 -3.62534914 6.512467622
85 -3.91251097 5.207713842
86 -3.62532649 5.410265684
87 0.63946154 6.327774286
88 1.57677092 -3.034456730
89 6.44150224 -3.294823528
90 2.94542351 -3.923038960
91 2.91057935 -8.520620108
92 0.58081587 0.206945180
93 7.32180943 -4.753448487
94 3.27217998 -2.410246730
95 0.87828424 -2.390648962
96 5.49466004 -3.544969321
97 -3.94603357 -0.885271311
98 2.39347210 5.571650266
99 6.52096763 -2.490488053
100 4.98212447 -1.903734990
101 2.16779580 4.905094385
102 4.42620292 -0.554852128
103 2.65831008 -2.856832028
104 -3.16114388 6.620826005
105 -3.12895570 5.702120542
106 -2.24802492 -1.276307643
107 5.82021871 -0.414735914
108 1.27348437 -1.010470391
109 -0.91018269 6.928126573
110 -3.45444522 -0.920573891
111 -7.39553390 3.036596536
112 3.11831608 0.049789786
113 3.00172749 -1.525067866
114 1.96554198 -0.662469149
115 5.42066636 -0.622119189
116 4.76249661 -0.194349051
117 4.46146359 -2.332514257
118 4.98593965 -1.614781857
119 3.13596048 5.331976175
120 -2.87556586 6.835524797
121 4.37761846 5.606873750
122 4.38151421 -1.254595101
123 3.15866032 -2.801524878
124 1.69382086 5.692551851
125 1.98393550 -0.327126623
126 -0.19548113 -1.411188215
127 4.75630727 5.391527891
128 -7.28258214 2.918875455
129 -0.94308016 5.049781560
130 6.06985249 -2.009833411
131 5.82366671 -1.000532806
132 3.65901484 6.266427278
133 5.32417026 -0.618663669
134 1.87386980 4.780691385
135 2.19627323 -0.557345033
136 2.98445907 6.082870245
137 -1.94063149 4.433698415
138 1.40829470 -0.208670021
139 2.83081570 -3.978659630
140 -1.85928140 5.837610483
141 0.45772984 6.426708459
142 1.54879727 -3.398948551
143 5.95999207 -2.543431342
144 -6.94657216 3.784797906
145 4.35926213 5.807478666
146 2.11910119 0.215088129
147 1.38549998 6.087655305
148 -4.46464953 5.505435228
149 6.41373839 -4.997896195
150 3.94981732 -0.956180513
151 -3.06182680 4.150348424
152 3.74205747 4.372680425
153 -2.91049466 6.822595834
154 -3.71450744 6.280554056
155 3.10248151 -0.218894959
156 1.58008065 4.209636449
157 3.50217619 -3.166743160
158 -4.68559060 -0.913203896
159 2.74889316 6.075514555
160 1.65527787 -2.652214945
161 -3.26782188 6.368825197
162 0.66886868 -4.823723078
163 -0.66325373 5.085819005
164 2.50191107 4.800724744
165 3.03497019 5.619921922
166 -2.12206993 -2.096688450
167 -0.82919148 6.544570207
168 3.96375623 5.827641725
169 -0.41847407 -1.901734557
170 -1.64916155 1.194483280
171 4.66061845 5.413992166
172 1.52290454 -2.695079923
173 3.96995177 0.312499284
174 6.34706702 0.769992828
175 4.19412866 6.026715993
176 4.79909387 -2.038346172
177 -1.64466999 7.049678087
178 -3.23279748 6.299213647
179 -7.44117436 1.675012826
180 -4.67911038 -0.865586639
181 3.19786873 6.220443964
182 4.30910029 -4.818237305
183 2.19765963 -3.504626155
184 -4.70068822 1.337587118
185 6.82522502 -4.473180533
186 4.49565949 5.993700742
187 -3.90244446 -0.190230132
188 3.67902389 3.194112539
189 -4.55718598 4.018882989
190 0.90422060 -2.486756921
191 -4.28800330 1.837078094
192 -7.07928119 -0.891751171
193 -8.26978288 3.392788648
194 6.16587129 -5.121435643
195 6.07257714 -4.378267050
196 4.40833106 6.057136297
197 -0.95598320 0.152837276
198 4.25779786 -0.008499504
199 3.10419431 1.065991401
200 4.76741519 5.523692369
201 3.88360562 6.110980272
202 2.02341952 -1.231803358
203 5.92827620 -2.979732872
204 1.33886971 3.644326925
205 2.61481705 -0.442604900
206 1.58079650 4.884998083
207 3.59270468 5.984362363
208 4.62460818 5.940760850
209 6.00425687 -1.786285274
210 -3.15978751 5.648642778
211 1.07319000 5.251102209
212 -4.22537718 5.339490652
213 1.76131263 4.607950925
214 -0.53642345 -1.820378423
215 -4.28645334 5.199775934
216 2.95205464 1.543722867
217 -2.11317239 6.537580728
218 -0.79293669 6.099419355
219 -3.70633493 5.972921609
220 -4.50439963 5.003460645
221 -7.17934499 0.540961742
222 4.39749017 -1.538563282
223 2.50188866 -4.121883631
224 -2.34567175 6.745729684
225 -2.24017689 -0.522887469
226 -2.31786225 6.531676530
227 -1.38638399 6.949416399
228 -4.70984778 3.964851617
229 0.39489379 5.115031957
230 -2.31088386 7.093653917
231 7.24636712 -4.879166842
232 3.77746501 -0.137967945
233 -2.53205977 6.269564867
234 5.38931146 -0.362965584
235 3.68597665 6.018393278
236 -3.78018723 -0.680152417
237 -3.17925105 6.049657106
238 5.23040357 -1.759076767
239 3.03680148 -2.337008626
240 -0.99241028 0.713386535
241 2.91206326 -2.934902549
242 -4.37652574 -0.875560165
243 2.29945865 0.178076028
244 -8.98096881 2.769885778
245 5.99747624 -3.832244158
246 3.77649608 6.045008897
247 -2.69199309 4.621043920
248 1.80712476 -0.793814540
249 -5.10822139 3.460498571
250 0.06421029 6.008613824
解决方案
你应该用 df1[df1$ord, ] 对 df1 进行重新排序,而不是 df1[order(df1$ord), ]。
library(tidyverse)
library(princurve)
## Generate principal_curve object
crv <- principal_curve(mat1)
crv
## This produces a smooth curve
plot(mat1, col = "red")
lines(crv)
## matrix df
df <- data.frame(
dim1 = mat1[, 1],
dim2 = mat1[, 2]
)
## crv df
df1 <- data.frame(
dim1 = crv$s[, 1],
dim2 = crv$s[, 2],
ord = crv$ord
)
df1
## Order curve.df
df1 <- df1[df1$ord, ]
df1
## This line looks good now
p <- ggplot() +
geom_point(data = df,
aes(x = dim1, y = dim2), color = "red") +
geom_path(data = df1,
aes(x = dim1, y = dim2)) +
theme_classic()
p
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。