I am trying to combine points and line from two different data sets (of different size) on a 3d plot_ly plot.
I managed to combine both datasets but I would like to color the points according to the cluster variable of the points dataset, and I block on it.
Here is what I made :
plot_ly(x = rd$PHATE_1, y = rd$PHATE_2, z = rd$PHATE_3) %>% add_markers() %>%
add_trace(x = ~dim1, y = ~dim2, z = ~dim3, data = curve_i,
mode="lines",
line = list(color = "black", width = 4))
But I can't find a way to color the points by rd$cluster, I was thinking of using markers=list(color=rd$cluster)
in add_markers() but it just removes everything from the plot.
Thank you in advance
Here is the dataframes :
rd=structure(list(PHATE_1 = c(0.0351331790729935, 0.0360344902527886,
0.034949601333084, 0.0363786168806341, 0.0360436375026313, 0.0325126416346828,
0.0338385465488856, 0.0308176192475494, 0.0304490693984702, 0.030786811524225,
0.0308193713794059, 0.0289174973357227, 0.0287503799184529, 0.0290564834693387,
0.03391301090642, 0.0340849602768675, 0.0341301415353431, 0.0342918393605192,
0.0297287345598898, 0.0291143537411051, 0.0294210535428787, 0.0342030549659177,
0.0350014750517981, 0.0354325150713683, 0.0370283060771868, 0.0348558971956282,
0.0370647445385887, 0.0367694336348689, 0.037615136016424, 0.0368603105295895,
0.0373080621745224, 0.0376238858686782, 0.0380760445076172, 0.0375466900761308,
0.0376265121304119, 0.0376367805053484, 0.0376662393342405, 0.037265572276421,
0.0377259653287126, 0.0379095353571794, 0.0333178492098492, 0.0333247156874217,
0.0336397296335373, 0.0325420118568886, 0.0321306761885729, 0.0330562843074959,
0.0320383213279616, 0.0350115363897838, 0.0333822470809376, 0.0355430845142524
), PHATE_2 = c(0.0168001714446119, 0.0149264587554465, 0.0167312341233693,
0.0143807074553838, 0.0153739523287853, 0.0192896561656933, 0.0179541564037252,
0.0209133528800737, 0.0212785141078608, 0.0209311675781716, 0.02092876974327,
0.0226976574776206, 0.0228687895036187, 0.0225886618773771, 0.0179277520839278,
0.0177646558077513, 0.0177258488647976, 0.017466571732919, 0.0219855650644215,
0.0225526868430415, 0.0222666873007695, -0.0023041010725845,
-0.000831719118347632, -0.000166265708899464, 0.00155976970887437,
-0.000902761620771023, 0.00770767399160672, 0.00863245469472674,
0.00498207833142103, 0.00796787610222532, 0.00468105890547177,
0.00382918454187288, 0.00517017599552221, 0.00241630074351924,
0.00345158799021147, 0.00434444887773949, 0.00364018544600582,
0.000785181723659163, 0.00403845649917379, 0.00408336732547395,
-0.00400197122541039, -0.00375058962238342, -0.00315596049296407,
-0.00560327870796924, -0.00527835885405557, -0.00431335621089391,
-0.00581656672069219, -0.00145799499370337, -0.00378672183288891,
0.00462557922038298), PHATE_3 = c(0.00200028395913235, 0.00155621807217406,
0.00160269032759097, 0.00156352370303698, 0.00203998228478397,
0.0021501753794526, 0.00174323692486192, 0.00312940867455374,
0.00303012757557203, 0.00289445776422037, 0.00300702200426022,
0.00416161891885436, 0.0042160695405753, 0.00413276121148965,
0.00167804176277997, 0.0016644664092973, 0.0016723202468114,
0.00166475305415384, 0.00346880361363429, 0.00373113993881418,
0.00353104035760799, -0.00604302773619096, -0.00553251528344172,
-0.00539540873964862, -0.00486904335517079, -0.00554790556368677,
-0.00313351311405747, -0.0035073612479315, -0.00291636827954746,
-0.0046369351285745, -0.00455510818778417, -0.00185868438645987,
-0.00112756202018531, -0.00224377059887581, -0.00323980632227819,
-0.00383328960808446, -0.00420915823613813, -0.00518227450774445,
-0.000817586377990738, -0.000690880739690505, -0.00568398156863888,
-0.00610502427881552, -0.0058204149101023, -0.005523778815693,
-0.00614739791878966, -0.00576026601105177, -0.00597126429290568,
-0.00829971485040034, -0.00578466588785696, -0.0095250770564247
), cluster = c("cluster1", "cluster1", "cluster1", "cluster1",
"cluster1", "cluster1", "cluster1", "cluster1", "cluster1", "cluster1",
"cluster1", "cluster1", "cluster1", "cluster1", "cluster1", "cluster1",
"cluster1", "cluster1", "cluster1", "cluster1", "cluster1", "cluster2",
"cluster2", "cluster2", "cluster2", "cluster2", "cluster2", "cluster2",
"cluster2", "cluster2", "cluster2", "cluster2", "cluster2", "cluster1",
"cluster2", NA, "cluster2", "cluster2", "cluster2", "cluster2",
"cluster2", "cluster2", "cluster2", "cluster2", "cluster2", "cluster2",
"cluster2", "cluster2", "cluster2", "cluster2")), row.names = c(NA,
-50L), class = "data.frame")
curve_i=structure(list(dim1 = c(0.0283420517965077, 0.0286313442985047,
0.0289206364720171, 0.0292098982407142, 0.0294990098433898, 0.0297877389510291,
0.0300757087321304, 0.0303624728816417, 0.0306475024323809, 0.0309300265558328,
0.0312119307643188, 0.0314890556187912, 0.0317612268440264, 0.0320302096873613,
0.0322889021536278, 0.0325475946198942, 0.0327985110620455, 0.0330443279659669,
0.0332832739100077, 0.033509318675959, 0.0337277723530267, 0.0339262220016758,
0.0340891918692368, 0.034247892993435, 0.0344065941176333, 0.0345652952418315,
0.0346937558868009, 0.0347964225019468, 0.0348950812685146, 0.0349556250115386,
0.0349850238297045, 0.0349839468765661, 0.0349476087543302, 0.0348786490318081,
0.0347760212678184, 0.0346381282329869, 0.0344639196190438, 0.0342533809616572,
0.0340071117264335, 0.0337263659059492, 0.0334126069273457, 0.0330663319087086,
0.0326879796322522, 0.0322789875843813, 0.0318408702869556, 0.031374009362215,
0.0308814643312578, 0.0303628100736034, 0.0298196027061759, 0.0292529407958469,
0.0286637618859357, 0.0280531929522322, 0.0274220007862645, 0.0267714499744412,
0.0261029894857614, 0.0254176244402223, 0.0247162047681251, 0.0239997080017602,
0.0232691066430654, 0.0225253322388048), dim2 = c(0.0278549459098156,
0.027236842283014, 0.0266187381733077, 0.0260005898931273, 0.0253822290652525,
0.0247633418801286, 0.0241434301835334, 0.0235219204324892, 0.0228981522392605,
0.0222711615890567, 0.0216433906827958, 0.0210096328652393, 0.0203698285855333,
0.0197262612267781, 0.0190707755095166, 0.0184152897922552, 0.0177514045012667,
0.0170820107464793, 0.0164056434714637, 0.0157166530366124, 0.0150205807635804,
0.0143072240927548, 0.0135660343655686, 0.0128216189929644, 0.0120772036203603,
0.0113327882477561, 0.0105682344570318, 0.00978781073341425,
0.00900526210514385, 0.00820320127582587, 0.0073875578578558,
0.00656107520140721, 0.00572450741783241, 0.00488132210818565,
0.00403399866509612, 0.00318526858434962, 0.00233822557510423,
0.00149607594086057, 0.000661866823507216, -0.000161759654213832,
-0.000972539778786166, -0.00176808026604347, -0.00254611137581595,
-0.00330511305299538, -0.00404389955906721, -0.0047606856196878,
-0.00545629466705129, -0.00612834609180704, -0.00677683880782346,
-0.00740132262066165, -0.00800138692858268, -0.00857679385436596,
-0.00912707126272938, -0.00965249564267232, -0.0101536954526655,
-0.0106307545818614, -0.0110836779489386, -0.0115126478413892,
-0.0119178207623138, -0.0122993198420492), dim3 = c(0.0118827975383443,
0.0113424240909185, 0.0108020510200262, 0.0102617122036683, 0.0097215362893367,
0.00918175888589753, 0.00864275192827243, 0.00810494378941775,
0.00756883358222646, 0.00703515900714876, 0.00650208049681984,
0.00597359865711871, 0.0054498472619227, 0.00492910798222012,
0.00441806748458524, 0.00390702698695035, 0.00340311857667807,
0.00290388740912351, 0.00241084102970252, 0.00192928706078878,
0.00145443606340931, 0.000997042511009582, 0.000570880036083412,
0.000148499385403855, -0.000273881265275704, -0.000696261915955263,
-0.00109147847039071, -0.00146315586492672, -0.00183109469340746,
-0.00216326324581771, -0.00246548007515601, -0.00273768836273466,
-0.00297459470790318, -0.00317842840738921, -0.00334790204548602,
-0.00348133117305427, -0.00357786146140542, -0.00363799973743561,
-0.00366313058193279, -0.00365543318735918, -0.00361737195235494,
-0.00355058784914788, -0.00345679332610391, -0.00333860449086163,
-0.00319862431273337, -0.0030386936185834, -0.0028622451598389,
-0.00267052411701139, -0.00246600581105041, -0.00225069395396301,
-0.00202637586556695, -0.00179481829968922, -0.00155759546226233,
-0.00131628034499301, -0.00107231101737338, -0.000826907532852235,
-0.000581114773808583, -0.000335800089555646, -9.16150568783186e-05,
0.000151000053907709)), row.names = c(NA, 60L), class = "data.frame")
CodePudding user response:
You will get strange issues here if you try to change the color
parameter in the usual way, as you are using two different data frames. plotly
will try to apply the color to both data frames, be unable to do it as they have differing numbers of rows, and throw an error.
One way to do it is to group the first data frame and then apply a fake group to the second data frame (unless you want multiple lines per group, in which case remove transforms = ~line
).
transforms = list(
list(
type = 'groupby',
groups = rd$cluster,
styles = list(
list(target = "cluster1", value = list(marker =list(color = 'blue'))),
list(target = "cluster2", value = list(marker =list(color = 'red'))),
list(target = NA, value = list(marker =list(color = 'black')))
)
)
)
curve_i$line = 1
plot_ly(
x = ~PHATE_1,
y = ~PHATE_2,
z = ~PHATE_3,
data = rd,
transforms = transforms
) |>
add_markers() |>
add_trace(x = ~dim1, y = ~dim2, z = ~dim3, data = curve_i,
mode="lines",
line = list(color = "black", width = 4),
transforms = ~line
) |>
layout(showlegend = FALSE)