Я пытаюсь добавить настраиваемую шкалу цветов.

Я создаю свои цвета, используя следующее:

N     = nrow(data)
phi   = runif(N)*2*pi
i = order(phi)
t = 2*runif(N) - 1
j = order(t)

col = c(rainbow(length(phi))[order(i)],
        rainbow(length(t), start=0, end=2/6)[order(j)])

И я пытаюсь вложить их в следующий сюжетный сюжет.

Код, который у меня есть:

data %>% 
plot_ly(
  x = colnames(.),
  y = as.Date(index(.)),
  z = data.matrix(.),
  type = "surface",
  colorscale = list(c(0, 1, 2), c("blue", "red", "yellow"))
)

Но вместо того, чтобы colorscale быть списком определенных цветов, я хотел бы установить его на цветовую шкалу цветов в col

Я попытался заменить colorscale на colorscale = list(seq(1:length(col)), col), но это не помогло.

Данных:

data <- structure(list(col1 = c(0.235656331824658, 0.416750972441865, 
0.502715210710106, 0.560093004360497, 0.445501580890665, 0.433515663967857, 
0.414680750189819, 0.285437333389163, 0.196963905678594, 0.206316122912184, 
0.173240840144559, 0.192197832000845, 0.178556337576989, 0.200011899756623, 
0.195251400917186, 0.191952530614014, 0.192698666783412, 0.0935238866937333, 
0.116314809422852, 0.145383204955348, 0.204147981767266, 0.216173982573999, 
0.247936102134843, 0.307373778299483, 0.306491014100766, 0.344075368621888, 
0.377173441446655, 0.34252585288411, 0.347973257562028, 0.340937352238925, 
0.363698945174488, 0.374287091473872, 0.354009843983882, 0.417672353843713, 
0.408570566357946, 0.39002935028702, 0.314402377002984, 0.305265770404778, 
0.37377781355228, 0.368644215665075, 0.482721143361634, 0.433715211414997, 
0.4625567127525, 0.496503289427253, 0.491953216519579, 0.540740872640734, 
0.630976837983005, 0.749363695711662, 0.825763296856934, 0.68360543177435, 
0.813073864181017, 0.724902238088593, 0.843960238465473, 0.8532814766617, 
0.827691240274222, 0.887303286902871, 0.935108817593125, 0.9102900144695, 
0.803028733299559, 0.809889037748211, 0.698780019600734, 0.414607543439043, 
0.452992087693615, 0.580398323385506, 0.609138367946255, 0.687834967665635, 
0.702826962009956, 0.612000553526612, 0.652128703773085, 0.690196185426843, 
0.619702395041897, 0.713977716337787, 0.758191791185991, 0.685888162207606, 
0.564353743343398, 0.631059973441547, 0.577673134581722, 0.719069882452169, 
0.567584730383172, 0.59921019769292, 0.796651860548267, 0.645172839389433, 
0.487968120396763, 0.484816859456377, 0.352664007566271, 0.510448957474189, 
0.3556082440527, 0.36772069173982, 0.196438787107622, 0.197618707016012, 
0.236664981492182, 0.0427462973557569, -0.0781979507923356, -0.0157571296469586, 
0.0215217275257378, -0.026630477533061, -0.0207245949923834, 
0.0965356769743508, -0.0623735408605962, -0.103014239323061), 
    col2 = c(0.12719361125789, 0.168795189900192, 0.236251162933698, 
    0.315955130282715, 0.297007271192553, 0.292403873601807, 
    0.257035205070242, 0.231085846540227, 0.176869191257498, 
    0.164401858229985, 0.137403933873663, 0.121393688174807, 
    0.111213596190166, 0.104833001340247, 0.116459391106515, 
    0.0854067246806089, 0.09064707096754, 0.122519553524729, 
    0.012975203797587, -0.00769368404727278, -0.00298463751559075, 
    0.0518785863811539, 0.0360576911683621, 0.0932451049461138, 
    0.126781584452029, 0.146884533437943, 0.125421832633012, 
    0.0567636068466464, 0.015993090589461, 0.00479867581857851, 
    -0.0434551990962814, 0.00300864221549735, 0.00958052451334535, 
    0.0626683513919197, 0.0766604472046852, 0.0272743146978776, 
    -0.020985530963431, -0.00288086887943817, -0.0462127535603084, 
    -0.0881580452462788, -0.112487398406493, -0.0938713232568118, 
    -0.107299484281944, -0.147471007489256, -0.145924079881878, 
    -0.191043444146704, -0.134348616976711, -0.121554453161668, 
    -0.130853958136341, -0.0703607705168348, -0.0718988597529627, 
    -0.0619771920960762, -0.115498012967471, -0.12872072753232, 
    -0.124987435403811, -0.12155359660131, -0.123583972169411, 
    -0.121916947989936, -0.131034780760477, -0.188802751954098, 
    -0.246620609461188, -0.290243167968015, -0.283403352686716, 
    -0.277350132493539, -0.270808999226358, -0.284445516499356, 
    -0.279676620628594, -0.323578948490959, -0.356135756177754, 
    -0.361046533135117, -0.358815680181268, -0.323253636220458, 
    -0.283221939732876, -0.311140179292285, -0.277037513216707, 
    -0.308438389272701, -0.247744739467366, -0.24570353903177, 
    -0.212201399236759, -0.0563562186936151, -0.181979153154105, 
    -0.155284647540919, -0.209881413796043, -0.159936337208494, 
    -0.148787344152928, -0.210437264675086, -0.22398406145976, 
    -0.245012397056348, -0.204457861271901, -0.150330528956728, 
    -0.107691691218474, -0.0844381921422784, -0.0646108564563991, 
    -0.0656389620298383, -0.00508475424039256, 0.0831429934083177, 
    0.104381091318285, 0.179299386318344, 0.120292946613076, 
    0.221316260930201), col3 = c(0.161546203443221, 0.152453640019518, 
    0.153373120334683, 0.135474135913219, 0.147597868297894, 
    0.172888642184766, 0.194923524240401, 0.195646624708927, 
    0.250480392793265, 0.255915652823187, 0.27084611862598, 0.291192799721722, 
    0.331600536630724, 0.320491446794146, 0.315437188637601, 
    0.298557393134282, 0.312498960238484, 0.38232894820008, 0.351441890896562, 
    0.297172810768038, 0.2243748354899, 0.200701694819175, 0.204270255433855, 
    0.137411887339277, 0.11828128151939, 0.146221482111235, 0.147147160675428, 
    0.177923990907149, 0.211250029744542, 0.180283602301764, 
    0.122246069261056, 0.0265709543179899, 0.013306725072691, 
    -0.0455355182360554, -0.0318064493521603, -0.0316164025270259, 
    -0.00233727298283182, -0.00811620418896331, -0.0375712486485237, 
    0.0438375711391912, 0.0749247953123675, 0.0698212948004988, 
    0.0335134803815706, 0.0944554606278381, 0.11716021947087, 
    0.0971618758155439, 0.0438689897555926, 0.0329368196563609, 
    0.0246124855212335, 0.0539411900929401, 0.0667291519545328, 
    0.111577925074181, 0.134742826699399, 0.176825100517058, 
    0.136248922290186, 0.121118508339388, 0.153935067989936, 
    0.141876992555499, 0.160938352861888, 0.166913946607316, 
    0.174325200929141, 0.170876375082066, 0.139697096988293, 
    0.129961969539393, 0.0838364584266291, 0.0367699777229393, 
    -0.00917271509803308, 0.0294217605655285, -0.00273490886903516, 
    0.0626527816422324, 0.0931126962785895, 0.0355127043345604, 
    -0.00713124401207743, -0.024385517722365, -0.0399159937163092, 
    -0.116069707523365, -0.208478638930302, -0.282108532983297, 
    -0.270810616708794, -0.403169664294586, -0.369999648526716, 
    -0.323877439520509, -0.261646380980994, -0.345560160234489, 
    -0.282741812525804, -0.298194412615014, -0.231168824049575, 
    -0.198575357375914, -0.0788898022838032, -0.0110550704137208, 
    -0.0763389566949494, 0.0910158843671702, 0.192177523097374, 
    0.0886131875583314, 0.0784599896563007, 0.106073160223084, 
    0.161354300458175, 0.185071698052216, 0.248193651710127, 
    0.175574235930518), col4 = c(0.0304390482130936, 0.0469779927089966, 
    0.033770819843445, 0.00331619536480243, 0.0124536827325159, 
    -0.000455927336921125, -0.030846803386302, -0.00117080011257586, 
    -0.00696748748434617, -0.00209236544113756, -0.0132756409134801, 
    -0.0234861521684404, -0.0457818627558735, -0.0486116911581397, 
    -0.047945586120943, -0.0224544740251954, -0.0332841450482498, 
    -0.0598284071391612, -0.0305858683034936, -0.0155752051975765, 
    -0.0139877433232731, -0.0225403920102994, -0.00513371332929846, 
    0.00893226123481994, -0.000410624446992025, -0.0507962731179592, 
    -0.0573470234760132, -0.0606662240975765, -0.0781897093423642, 
    -0.0753131118981941, -0.0522054100652527, -0.027429641696892, 
    -0.00471526492904928, 0.0309918575250425, 0.0307864192750624, 
    0.0683509152732753, 0.101119753015762, 0.0909914769432321, 
    0.098722826864678, 0.0917702600346066, 0.0959931143578587, 
    0.0963223073237252, 0.129249927020376, 0.109186262341304, 
    0.124145094784313, 0.157880655455308, 0.172761331759985, 
    0.175797758231381, 0.178273866627175, 0.160399991791368, 
    0.140103140360734, 0.136499495507757, 0.159934797883648, 
    0.152208818019029, 0.154601893400139, 0.143785411640161, 
    0.14113829305818, 0.125988916141387, 0.138259741162324, 0.132932295650013, 
    0.144310058587335, 0.214962314879126, 0.189029931507835, 
    0.178876260125701, 0.19888971295085, 0.212244129221552, 0.188382009571607, 
    0.165825308091227, 0.137020932052855, 0.108618040537601, 
    0.112263720192583, 0.091246951738569, 0.0886661548483718, 
    0.10349662214449, 0.0837014894776758, 0.0530774871879585, 
    0.0229063281991708, -0.0352470893183078, -0.103333832248695, 
    -0.23732464638257, -0.159693838293407, -0.13472297471351, 
    -0.108960905222514, -0.161442647694013, -0.113863140162991, 
    -0.14678214764108, -0.0999115840323502, -0.0207123655048754, 
    0.0839225444460361, 0.0921669513093166, 0.0129384959981884, 
    0.119735230787535, 0.174620891116606, 0.150675913211874, 
    0.132467215148983, 0.142551215363916, 0.205426400379512, 
    0.199527086837687, 0.263287706994205, 0.235147945181147), 
    col5 = c(0.0440128290737087, 0.0104393828074874, -0.030401149082101, 
    -0.0335757459746789, 0.00387471213061752, 0.0277961309077646, 
    0.0913122279704728, 0.0815771584817113, 0.116952257012042, 
    0.132129148505063, 0.158457733354018, 0.169586821215297, 
    0.190034974625445, 0.184263942216673, 0.189233378281525, 
    0.189353188212845, 0.191021452378257, 0.2507033517076, 0.227075146257368, 
    0.200554919569283, 0.174302345823063, 0.151263457623384, 
    0.146701740109274, 0.102296433544678, 0.0781126107690535, 
    0.0981855782968859, 0.118573646746753, 0.144058109683463, 
    0.191187177023668, 0.211623636183806, 0.205826393905415, 
    0.129189274109536, 0.0984404624529147, 0.0484722747166004, 
    0.0566584808769306, 0.0596804824494583, 0.0765288717081505, 
    0.0550887950998713, 0.0269038317890493, 0.0546054979661184, 
    0.0602411191994521, 0.079280360806264, 0.0530448895850839, 
    0.120244659541414, 0.130712176277564, 0.101399480135523, 
    0.056715509998446, 0.0425140164856161, 0.0176228226854103, 
    0.0214453652680864, 0.00345752031211677, 0.0288295196672432, 
    0.0619744026420159, 0.081229168659235, 0.0696865645440461, 
    0.0501572859297159, 0.0534209326328559, 0.032236205675334, 
    0.0555397977875467, 0.0542347825831937, 0.0940803868824052, 
    0.150848708843285, 0.113228097261483, 0.111671290412851, 
    0.0904494744831099, 0.0115937835210702, -0.0674121673897199, 
    -0.0076345512976107, -0.0273199800957551, -0.00169952656337924, 
    0.0305425955790051, -0.0171892917764586, -0.0330456716695445, 
    -0.0114217568421809, -0.00339362349416816, -0.0400044422104336, 
    -0.0820782621637022, -0.161594691409598, -0.115233611959491, 
    -0.195728091791677, -0.218299745422938, -0.148134988712772, 
    -0.0905179370402545, -0.142697627835499, -0.0808413007066509, 
    -0.0753562836885152, 0.0200672474965569, 0.0845104819375362, 
    0.204451013089104, 0.275044570509885, 0.148131602469016, 
    0.334276847377688, 0.445088766243454, 0.359168498552319, 
    0.344759884825204, 0.359603015388057, 0.421195769440871, 
    0.436464595439161, 0.557383581353241, 0.45674641084667)), row.names = c("1993-07-31", 
"1993-08-31", "1993-09-30", "1993-10-31", "1993-11-30", "1993-12-31", 
"1994-01-31", "1994-02-28", "1994-03-31", "1994-04-30", "1994-05-31", 
"1994-06-30", "1994-07-31", "1994-08-31", "1994-09-30", "1994-10-31", 
"1994-11-30", "1994-12-31", "1995-01-31", "1995-02-28", "1995-03-31", 
"1995-04-30", "1995-05-31", "1995-06-30", "1995-07-31", "1995-08-31", 
"1995-09-30", "1995-10-31", "1995-11-30", "1995-12-31", "1996-01-31", 
"1996-02-29", "1996-03-31", "1996-04-30", "1996-05-31", "1996-06-30", 
"1996-07-31", "1996-08-31", "1996-09-30", "1996-10-31", "1996-11-30", 
"1996-12-31", "1997-01-31", "1997-02-28", "1997-03-31", "1997-04-30", 
"1997-05-31", "1997-06-30", "1997-07-31", "1997-08-31", "1997-09-30", 
"1997-10-31", "1997-11-30", "1997-12-31", "1998-01-31", "1998-02-28", 
"1998-03-31", "1998-04-30", "1998-05-31", "1998-06-30", "1998-07-31", 
"1998-08-31", "1998-09-30", "1998-10-31", "1998-11-30", "1998-12-31", 
"1999-01-31", "1999-02-28", "1999-03-31", "1999-04-30", "1999-05-31", 
"1999-06-30", "1999-07-31", "1999-08-31", "1999-09-30", "1999-10-31", 
"1999-11-30", "1999-12-31", "2000-01-31", "2000-02-29", "2000-03-31", 
"2000-04-30", "2000-05-31", "2000-06-30", "2000-07-31", "2000-08-31", 
"2000-09-30", "2000-10-31", "2000-11-30", "2000-12-31", "2001-01-31", 
"2001-02-28", "2001-03-31", "2001-04-30", "2001-05-31", "2001-06-30", 
"2001-07-31", "2001-08-31", "2001-09-30", "2001-10-31"), class = "data.frame")
r
0
user113156 17 Ноя 2019 в 21:07

1 ответ

Лучший ответ

Вот что я сделал (использовал rownames вместо индекса и my_col вместо col).

Изменить : использование colors = my_col вместо шкалы цветов.

library(plotly)

N     = nrow(data)
phi   = runif(N)*2*pi
i = order(phi)
t = 2*runif(N) - 1
j = order(t)

my_col = c(rainbow(length(phi))[order(i)],
        rainbow(length(t), start=0, end=2/6)[order(j)])

data %>% 
  plot_ly(
    x = colnames(.),
    y = as.Date(rownames(.)),
    z = data.matrix(.),
    type = "surface",
    colors = my_col
  )

plotly surface using color vector

1
Ben 17 Ноя 2019 в 19:11