R 中的热图显示不正确

Heatmap in R does not display properly

这是我的热图代码。

sd1<-melt(Mstressed,id.vars = "Period")

library(plotly)
P1 <- ggplot(data=sd1, aes(x=Period, y=variable, fill=value)) + 
  geom_tile() +
  ggtitle("Stress Portfolio Returns") + 
  scale_fill_gradientn(colors=colorRampPalette(c("lightgray","royalblue","seagreen","orange","red","brown"))(500),name="Returns") +
  labs(x = "Period",y="Size") +
  theme_bw()
ggplotly(P1)

这里是 sd1 已经是 melted 格式:

    Period variable         value
1        1    Size5 -1.124193e-03
2        2    Size5  2.859438e-05
3        3    Size5 -2.432560e-03
4        4    Size5 -2.544023e-03
5        5    Size5 -1.577432e-03
6        6    Size5 -1.480790e-03

这里是sd1

    > dput(sd1)
    structure(list(Period = 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, 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, 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, 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
), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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241931, 2.85943772727272e-05, 
-0.00243256027727273, -0.00254402339069767, -0.00157743184782609, 
-0.00148078985416667, -0.00331852698695652, 0.000848599395833333, 
-0.00297349628181818, -0.000345186711111111, -0.000501344534782609, 
-0.000800074080851064, -0.00221098331428571, 0.00129236565531915, 
-0.00131892916086957, -0.00177610596170213, -0.00319288334468085, 
0.00113381609787234, -0.00107374819148936, -0.00144620190638298, 
0.000141176244444444, -0.000204472952173913, -0.00153988329130435, 
3.81790045454546e-05, -0.0011685701173913, 0.000593231404255319, 
0.00081186575, 0.000795386733333333, -0.00260122034583333, -0.0021938114875, 
0.000553855472340425, -0.0059582546893617, 0.868693839468085, 
-0.002801906475, -0.00463708955833333, -0.00272099271111111, 
0.00108590003333333, -0.00247600357826087, -0.00372859965957447, 
0.000286994587234042, 0.000746639587234043, 0.000276679165957447, 
-0.001046501125, 0.0001712814375, -0.0024568735875, 0.000996786336170213, 
-0.00182627393191489, -0.000544925195652174, -0.00357635526086957, 
-0.00054329072173913, 0.00013407957826087, -0.00103177774347826, 
0.000500834638297872, 0.793977075161702, -7.5436685106383e-05, 
-0.000485326586956522, -0.000606974591304348, 0.00238437179166667, 
0.00100126108888889, -0.000513758953191489, 8.69377181818182e-05, 
-0.00123867083829787, 1.3438354628383, -0.00031518505, -0.00191560200454545, 
0.805996328804348, -2.61513454545454e-05, -0.000103250163636364, 
-0.000778098191489362, -0.0015234903, -0.00114211408636364, -0.00159511182978723, 
1.87802078087826, 6.72123086956521e-05, 0.00100891171111111, 
-0.000902390629787234, 0.0011009548, 0.000312209177777778, -0.000768029209090909, 
-0.000303889769565218, 2.55336222222222e-05, -0.000205546365217391, 
0.000216974877272727, 1.47901930388571, -0.000277860133333333, 
-0.000214537138095238, -0.00209356162222222, 8.97567761904762e-05, 
-0.00559485620909091, 0.000268375711111111, 1.39724291668889, 
0.000208154288888889, -0.000634756711111111, 1.22926746946512, 
0.00201531319534884, 0.252617265511111, -0.00113930945106383, 
0.000837345108695652, 0.00246463208780488, 0.000899249748837209, 
0.00274860853333333, 0.00547736369047619, 0.00163468337674419, 
-0.000276660977272727, -0.00149508101818182, -0.00221645797777778, 
0.00217117751363636, -0.0024888984, -0.000886121655813954, 1.04398107523478, 
0.00393339840930233, -0.000922796323809524, -0.00103749403636364, 
0.0028039828, 0.00551933685, -0.00123477008888889, -0.005628441, 
-0.00121736573043478, -0.000539308567441861, 0.000262361868181818, 
0.000343038088888889, -0.0019510897, -0.00288396375454546, 0.359000083955556, 
0.000433049466666666, 1.36924029128889, -0.000659901990909091, 
0.00304780745454545, 0.000657599762790698, -0.00163171464444444, 
-0.000950691240909091, 0.00140542772727273, -0.00107216327272727, 
0.000923795111111111, -0.000474348352173913, -0.000865860582608696, 
-0.00326246348888889, -0.0006538472, 0.000691390128571429, -9.56189619047619e-05, 
-0.00127241684651163, 0.000941450827272727, 0.000531463427906977, 
-0.0024702198, 1.36070394653333, -0.00128264022222222, -0.000889438852173913, 
-0.000750582844444444, -0.000299201959090909, -0.00191169516363636, 
-0.000670931155555555, 2.36634136363636e-05, -0.00117427344186047, 
-0.00024379075, -0.000986528844444444, 0.000683873, -0.00243973785909091, 
-0.00394431812727273, -0.00293166445, -0.00264133897021277, 0.0029811113627907, 
-0.00108674306666667, -0.00145431042727273, 0.00250975572340426, 
0.0009900039, -0.00208648798636364, -3.64380465116271e-06, 0.000535875804545454, 
0.00197072025217391, -0.00175674031111111, -0.00122569961276596, 
1.77897318181817e-05, 0.000638891873913043, -0.000582162311111111, 
0.00169907495555556, -0.0017897038826087, 0.657609502855814, 
-0.00276549255555556, 0.000570080259574468, 0.00242968832272727, 
-0.000416251471428571, 0.000278305236363636, -0.000308473874418605, 
0.0015745695875, -0.00119187120476191, 0.00315008771111111, 0.624268614481818, 
0.000191677111111111, 0.26118118567234, 0.00184649572608696, 
-0.00148222706521739, -0.00224949090232558, -0.00098840725, -0.000267329995744681, 
-0.00113342224680851, -0.000558455731914894, 0.000867598088888889, 
0.000182637295454545, 0.000666522423255814, -0.000249393468181818, 
0.00137624896744186, -0.000641461824390244, -0.000726498320930232, 
0.0002189695, 0.000304278144186047, -0.000552308133333333, -8.48839142857143e-05, 
2.33358333333333e-05, -0.000115722561904762, 0.91983811283913, 
-0.000417791762790698, -0.000322370023255814, -0.000398199302127659, 
-9.84947333333333e-05, -0.00031643990952381, -0.000323731279069767, 
-0.0006542225, -0.000273647675, 1.15457317073172e-05, -7.34662954545455e-05, 
-0.000362220495238095, -0.000537859323809524, -8.60688604651162e-05, 
-0.000820713952380952, -0.00153372003333333, -0.000662153756060606, 
-0.00275888667727273, -0.00292078759069767, -0.00191772458115942, 
-0.000874108720833333, -0.00347106492028986, 0.000208326329166667, 
-0.00348595174848485, 0.324998960088889, -0.000291810668115942, 
-0.0026285438141844, -0.00318451878095238, 0.00141962778865248, 
0.54385947323913, -0.00152278529503546, -0.00409083314468085, 
0.195643822364539, -0.000821318458156028, 0.481235884893617, 
0.000631022044444445, -0.000154888085507247, -0.00238089515797101, 
0.000598294737878788, -0.000853384650724638, 0.00126600480425532, 
0.317600938083333, 0.000601763333333333, -0.00106298107916667, 
0.316528564245833, 0.000802788139007092, -0.00681549495602837, 
0.288553514534752, -0.001983084275, -0.00462916789166667, -0.00399624291111111, 
0.00133947723333333, -0.00233989184492754, -0.00326993565957447, 
-5.17553460992909e-05, 0.000316770853900709, 0.114606201365957, 
0.311149128608333, 0.0994249547041667, -0.00179180045416667, 
0.000758236002836879, -0.00229026533191489, -0.000906085528985507, 
-0.0034218825942029, 0.411083387011594, 0.407663510978261, -0.000700014143478261, 
9.04682382978723e-05, 0.264309987961702, 4.96020482269504e-05, 
-0.00112393678695652, -0.00112170092463768, 0.00237529965833333, 
-0.000567775777777778, -0.000236103953191489, 0.000608177318181818, 
-0.00102851437163121, 0.448221119304964, 0.13397610235, -0.00241395220454545, 
0.443765600537681, -0.000226488945454545, 0.000616203103030303, 
-0.0013208753248227, -0.000716755566666667, 0.091597368380303, 
-0.00170434656312057, 0.626332195011594, 0.000150197242028985, 
0.000136383977777778, 0.381103545036879, 0.221540732266667, 0.360871791444444, 
-0.000592961009090909, 0.000155833097101449, -0.000866745711111111, 
0.000216817234782609, 0.609538703077273, 0.645238149819048, -0.000398335866666667, 
-0.000344592138095238, -0.00137507775555556, -0.000256117023809524, 
-0.00192587620909091, -6.42416888888889e-05, 1.02013637508889, 
-9.28306444444445e-05, -0.00147465644444444, 0.409911313531783, 
0.0019891590620155, 0.0851806975111111, -0.00204437158439716, 
-0.000579642224637681, 0.000905066487804878, 0.643067721215504, 
0.00147966286666667, 0.00508458082380952, 0.000539691310077519, 
0.240211235089394, -0.000708393018181818, -0.00109935424444444, 
0.0026950974469697, 0.6396645562, -0.00126269592248062, 0.347808651568116, 
0.00457931780930233, 0.000302961476190476, -0.0012148691030303, 
0.0923351892, 0.00567091231666667, 0.620369322511111, -0.00404364773333333, 
-0.00241288513043478, 0.202742444965891, -0.000608278798484849, 
0.000356460755555555, -0.00161257603333333, -0.00190840115454545, 
0.120414817088889, 0.2228905758, 0.455057063555556, -0.000602834124242424, 
0.00261724412121212, 0.452285066562791, -0.00246991411111111, 
-0.00152651097424242, 0.000518139793939394, -0.00184454593939394, 
0.000185236244444444, -0.00128692128550725, -0.000827895915942029, 
-0.00243018582222222, -0.00120132326666667, 0.416215973261905, 
0.000253243971428571, -0.00170670997984496, 0.000686898960606061, 
-0.00033481963875969, -0.0020584786, 0.4539038956, -0.00121793155555556, 
-0.00123285105217391, -0.000559311711111111, 0.000337146574242424, 
-0.00228272816363636, -0.00138098148888889, -0.000646004586363636, 
-0.0014501903751938, -7.21782833333334e-05, -0.000256129177777778, 
0.000223322266666667, -0.00308267385909091, -0.00280884286060606, 
-0.00280925431666667, -0.00175704863687943, 0.380177369362791, 
-0.0012472598, -0.00215720449393939, 0.00190565352340426, 0.000277635766666667, 
-0.00224420778636364, -0.000902296071317829, 0.00110526400454545, 
0.00164177331884058, -0.000970322977777778, 0.488807391653901, 
-0.000405886401515152, -0.000386372792753623, -0.000187081244444444, 
0.00191570668888889, -0.00188197921594203, 0.219782496255814, 
-0.00182879988888889, 4.84401929078014e-05, 0.000748088722727273, 
-0.0012550096047619, 0.000173737503030303, -0.000151241941085271, 
0.581544109520833, -0.00156196413809524, 0.00255382897777778, 
0.208691561081818, 0.664795524311111, 0.42483048627234, 0.00175584192608696, 
0.201560880468116, -0.00209381856899225, 0.000321025016666667, 
-0.000421247062411348, -0.00215724371347518, 0.646012675801418, 
0.350737970755556, 0.178416441162121, 0.00103144908992248, -0.000187515334848485, 
0.000174517300775194, -0.000258069957723577, -0.00119511305426357, 
0.0003255877, 0.00023375347751938, -0.0005782456, -0.000402424114285714, 
0.1384140809, 0.418395168104762, 0.306135187705797, -0.00102090409612403, 
-0.000444740023255814, 0.138124852631206, -0.0004063458, 0.00029016809047619, 
-0.000799771212403101, -0.000183847566666667, 0.000114925325, 
-0.000269002668292683, -0.000733895628787879, -0.000981041028571429, 
-0.000873523457142857, 3.48687286821712e-06, -0.00107125035238095, 
0.36082286618, -0.000514447102727273, -0.00280299127727273, -0.00263217947069767, 
-0.00203968404782609, -0.000458293174166667, -0.00351976894695652, 
0.000204725635833333, -0.00339431988181818, 0.193479343088889, 
0.000112535625217391, -0.00213525120085106, -0.00288490815428571, 
0.343934077535319, 0.32598547811913, 0.155322520318298, -0.00360304194468085, 
0.116695380217872, -0.00115564579148936, 0.288642051333617, 0.00110193360444444, 
0.000231925527826087, -0.00271350401130435, 0.000528825044545455, 
-0.000843372957391304, 0.00152301136425532, 0.42680129887, 0.000348928813333333, 
-0.00106065298583333, 0.1891960110325, 0.00144156823234043, -0.0058980177693617, 
0.172238823228085, 0.180649895645, -0.00399227711833333, -0.00396987499111111, 
-0.000279930486666667, -0.00221300633826087, -0.00437551217957447, 
0.373557485947234, -0.000553946612765957, 0.180780737205957, 
0.186384874315, 0.0601822571175, -0.0013033474675, 0.000607181016170213, 
-0.00182825481191489, -0.00101525615565217, -0.00307393246086957, 
0.246870552998261, 0.244639116138261, -9.59920234782608e-05, 
0.00103178067829787, 0.158475002401702, 0.0957530407148936, -0.00108818790695652, 
-0.00106499063130435, 0.00182353239166667, -0.000770247991111111, 
0.251169645886809, 0.00135098203818182, -0.00106422307829787, 
0.268188202118298, 0.08014729743, 0.173129845155455, 0.266595692084348, 
-0.000510779745454545, 0.000609720036363636, -0.000709136471489362, 
-0.00074677718, 0.273296411113636, -0.00150910594978723, 0.375676878958261, 
-7.46597313043479e-05, 0.222301664431111, 0.599549396090213, 
0.13323872568, 0.216335941017778, -0.000372647409090909, 0.000250945590434783, 
-0.000609455297777778, 0.163203653554783, 0.365593381717273, 
0.387049939765714, 0.377756158386667, -0.000608418738095238, 
-0.000877734902222222, 0.29536194985619, -0.00151932240909091, 
8.84523111111115e-06, 0.887420447928889, 6.04058888888889e-05, 
-0.00168793287111111, 0.245879004745116, 0.00210788515534884, 
0.0518048467911111, -0.00204896865106383, -0.000766938131304348, 
0.000369568767804878, 0.385807118748837, 0.00125597745333333, 
0.00342261065047619, 0.000917297056744186, 0.145203749862727, 
-0.000608651458181818, -0.000619898377777778, 0.00255708987363636, 
0.46996938808, 0.0463716679041861, 0.209438538634783, 0.00629131560930233, 
0.000908092756190476, 0.118311483963636, 0.055266674, 0.00411797937, 
0.371606156751111, -0.00341520444, -0.00207519669043478, 0.388790875192558, 
0.382199056508182, 0.000288075208888889, -0.00233150982, 0.190417886325455, 
0.367062582115556, 0.133206785506667, 0.591484569088889, -0.000679957030909091, 
0.00235175421454545, 0.271733587002791, -0.00258801096444444, 
0.209600095759091, 0.268993620327273, -0.00175308111272727, 0.000141544911111111, 
-0.00146279359217391, -0.000720371102608696, -0.00227193868888889, 
-0.00099748604, 0.249673461088571, 0.00122685363809524, -0.00157589268651163, 
0.000390671067272727, -0.000246003732093023, -0.00189639072, 
0.272506430813333, -0.00121220570222222, -0.00129201353217391, 
-0.000471348084444445, 0.108562531000909, -0.00230415824363636, 
-0.00146539943555556, -0.000698085466363636, -0.00125351572186047, 
0.32477301345, -0.000655153204444444, 0.23415397968, -0.00311150981909091, 
-0.00240564984727273, -0.00271525109, 0.124826008829787, 0.228650982202791, 
-0.000831444746666667, -0.00208768206727273, 0.00210693784340426, 
-0.00032504578, -0.00201565726636364, -0.00121992036465116, 0.241373214804545, 
0.00180044913217391, -0.00144649063111111, 0.293257521547234, 
0.156942076691818, -0.000396712526086957, 0.333872094768889, 
0.00126618459555556, -0.0022017223626087, 0.132416393375814, 
-0.00154369867555556, -0.000127951540425532, 0.000643781042727273, 
-0.00149518583142857, 0.000159095756363636, 0.431103792525581, 
0.3497642155475, -0.0010894085247619, 0.00271588987111111, 0.347565654681818, 
0.554514602551111, 0.25466477267234, 0.00141746724608696, 0.120691384734783, 
-0.00244462910232558, 0.06381322055, -0.000572060355744681, 0.212942333313191, 
0.387555468868085, 0.210574743968889, 0.107224934255455, 0.00119969578325581, 
-0.000265811628181818, -0.00014974783255814, -6.32937443902438e-05, 
-0.00163017968093023, 0.00031326598, -0.000229250015813954, 0.281825955426667, 
-0.000219622714285714, 0.0828831356333333, 0.251266484118095, 
0.18387889279913, -0.000723895042790698, 0.362715330336744, 0.0829555198178723, 
-0.000342741853333333, 0.00098233181047619, 3.61713209302325e-05, 
0.00060474866, 0.000214934285, -0.000507613948292683, -0.00126401089545455, 
0.295409336744762, 0.49940544567619, 0.000444745259534884, 0.075755845887619, 
0.2663678565, -0.00112328892272727, -0.00252072917727273, 0.0919692581493023, 
-0.00293427190782609, -7.54536341666666e-05, -0.00354078930695652, 
2.57954158333333e-05, -0.00262876556181818, 0.0960986548688889, 
-0.000307806654782609, -0.00256542482085106, -0.00344343351428571, 
0.171732012655319, 0.16290640195913, 0.0771365308982979, -0.00323413486468085, 
0.239592012997872, 0.0929770842485106, 0.143919237313617, 0.0872022225044444, 
-0.000374014952173913, -0.00198149831130435, 0.150230880804545, 
0.0495306889826087, 0.00207576486425532, 0.21266222853, 6.23467733333334e-05, 
-0.000502785265833333, 0.1146813025125, 0.00236195601234043, 
-0.0055398928293617, 0.0850331580480851, 0.091095192845, -0.00308972659833333, 
-0.00268806053111111, -0.000621778406666667, -0.00211494293826087, 
0.0788185111804255, 0.187436011507234, -0.00105928309276596, 
0.273111877825957, 0.091654408075, 0.1881146334375, -0.0012845890675, 
0.000535671256170213, 0.261780701048085, -0.00130433873565217, 
0.0672782252391304, 0.123152524898261, 0.122219372418261, 0.108913352556522, 
0.00154812281829787, 0.0787473360617021, 0.208401497354894, 0.0494821097730435, 
-0.00100385527130435, 0.00179214179166667, -0.00113253543111111, 
0.125312734706809, 0.00112138269818182, -0.000824940378297872, 
0.133558478718298, 0.06196782193, 0.0853069711554545, 0.227115156824348, 
-0.000573424825454545, 0.147897520156364, 0.0948538439485106, 
-0.00102526364, 0.136662496333636, 0.0497748200102128, 0.327559354018261, 
0.158837548148696, 0.111405826131111, 0.472707361930213, 0.15095515508, 
0.108453752477778, -0.000742863369090909, 0.000265612850434783, 
0.187649188042222, 0.277873906394783, 0.417418378297273, 0.383188042745714, 
0.188476248766667, -0.000679375438095238, -0.000940556842222222, 
0.14747278479619, -0.00116911116909091, 0.193063097831111, 0.443910019148889, 
0.156860665528889, -0.00140947317111111, 0.194701012465116, 0.179753437755349, 
0.0271931040711111, -0.00148793193106383, -0.000247921611304348, 
0.0215570688078049, 0.193278326948837, 0.269718930653333, 0.00265989913047619, 
0.00111463559674419, 0.0727459556427273, 0.178373261021818, 0.0498605999422222, 
0.00285280777363636, 0.28503765544, 0.0225589752241861, 0.105547501674783, 
0.00522300738930233, 0.000134479296190476, 0.0587813904836364, 
0.02802175016, 0.17572127429, 0.184946720591111, -0.00234486358, 
-0.00230577149043478, 0.194051282172558, 0.250351470368182, 0.143461193228889, 
-0.00273105342, 0.0943393136254545, 0.184555788855556, 0.0659578766466666, 
0.294674815488889, -0.000197858810909091, 0.135317324614545)), row.names = c(NA, 
800L), class = "data.frame")

这是我得到的不正确的图,我很困惑,因为我确信它在过去的代码中有效,而且我确信语法是正确的!显然我一定是遗漏了什么。

鉴于您的数据和脚本,该图在我的计算机上看起来不错(见下文)。你用的是哪个版本?

library("plotly")
P1 <- ggplot(data=sd1, aes(x=Period, y=variable, fill=value)) + 
  geom_tile() +
  ggtitle("Stress Portfolio Returns") + 
  scale_fill_gradientn(colors=colorRampPalette(c("lightgray","royalblue","seagreen","orange","red","brown"))(500),name="Returns") +
  labs(x = "Period",y="Size") +
  theme_bw()
ggplotly(P1)

我建议听从@Dan Adams 的建议。它还可能有助于检查 R 和包的版本。这是我的一部分SessionInfo(仅显示 R 版本和附加包)。

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

# ... omitted 

other attached packages:
[1] plotly_4.10.0 ggplot2_3.3.5

# ... omitted