R Plotly: aspectmode='cube' not working in 3D slcatter plot

No one on stackoverflow seems to know how to answer this question, so I’m shamefully cross-posting here.

I am making a 3D scatterplot (image attached) of principal components. Although I’m using aspectmode='cube', the axes are not equal in size.

pca3d

I created an MRE to test, and in that test, the axes to come out equal. So, I’m very confused.

Actual Data

df = data.frame(
  PC1=c(0.402285144003261,1.4171695209575,-0.636749686422501,2.25024242477446,-0.841449299879213,-0.814419884751789,2.07378541014841,-0.778655366569915,-0.270510046040042,1.93710878668601,-0.204151727584088,1.18675386419666,-1.15183163106794,-0.93729638809887,-0.302710575107095,-1.09505445967183,-1.21552773114306,0.413320681421469,1.33687147572696,0.407904518638843,-0.779028649942894,-1.33696908904798,-0.233015236454913,0.35225066203097,1.6253605258001,1.38908779425315,-0.825671237656062,-0.560424988418219,0.651203145837364,2.058167645016,1.14526323821716,2.00625493277667,-0.0738636435202872,-0.284276158583548,2.18298399877386,-1.24082057644767,-1.22427038717449,-0.797599236553793,-0.644062745636431,0.626727844681026,1.98405367505224,-0.30069951949164,-1.23719135962738,1.43043331347688,0.116338609902778,-1.20787242688613,-0.675116638900054,-0.755292892588987,-1.25329048584962,-0.338781457022259,-0.107165383056406,-0.640496143088783,-0.853495558358877,-1.13400491664111,-0.425905080519206,0.314948307010327,-0.176343304604597,0.812600292304552,-0.689807236191771,-0.107039622042221,-0.505836714238656,-1.236739303571,-0.19463981358001,-0.916245478804557,3.33272908429981,0.0608810071077068,0.320806980146676,-1.43672309252606,-0.872334924217026,2.93800267031722,1.74331480788324,-0.127481839044974,1.10232163273596,-1.18053517163663,-0.583944280435473,2.91279366730135,-0.79411540469038,-1.34695953634993,0.84421649882345,-0.399359269417906,-0.524386948445777,-0.864372867873221,-1.4513340499215,-1.19440393237087,-0.727719334458475,-0.383382463796734,0.984901156287103,-0.762592307336777,0.452913567561011,0.821307331892765,0.438220819724908,0.0168627531475347,1.90134990281157,-0.457595439918553,0.674440080100837,1.80700544468329,1.44766526949108,1.87218759733718,-0.675225790428008,-1.01446164695057,-0.839626836802754,-0.87440205023598,-1.29255224846617,-0.403915576623382,-1.15071619104369,-0.451329811450679,0.161765738564826,-0.530973567290627,-1.52897735513236,0.795290275200284,-1.09618873783387,-1.23811553857377,-0.818092480976885,0.419682383341886,0.118566391474338,2.29655580176131,-0.727237776009522,-0.38818997901355,0.609446493322166,-0.661480237880466,0.368594401718748,-1.075975628103,0.963234802514334,-1.19133091075783,-0.971765389994952,2.95651190409003,-0.446829002191232,-0.743097737525159,-0.709478214011909,-0.853724528400848,-1.0953870682951,-1.15841828790577,-1.05708487979213,4.62345613963919),
  PC2=c(-0.691442417923578,-0.683079840989716,-0.508805612645602,0.310124183701145,-0.333359912443457,-0.800693619112235,0.085202920424396,-0.11132735865037,-0.666403768217729,-0.0834748188236059,-0.345047196968817,0.17842154902598,-0.639680797448222,0.461583332814334,-0.535599436330943,0.0237661360475331,0.398227929480799,-0.473097275114053,-0.407826836179771,-0.787460273256553,-0.632110095458283,0.293036356935143,-0.0261353332247592,1.74508155429367,0.173506020900256,-0.197903882583297,0.216078539613414,-0.120045617632566,-0.435771015868879,0.0587322085417543,-0.458026351200192,0.522310573056916,-0.751249128576159,-0.681622222714507,-0.0119462713603535,0.186349913516978,-0.073365944613263,-0.703357642739476,-0.762586386300058,-0.390827630338644,-0.159399980975471,-0.64942816302922,0.362640302315422,-0.343569418769015,0.00940186847067734,-0.307033450665064,-0.284026380841206,0.0848178676708685,0.145461952525289,0.0445692166880105,-0.479639339732628,0.497067063401757,-0.624634242815239,0.198201574580486,1.03586534103812,-0.695901741788024,-0.495076850381257,-0.159202416957294,0.508387562014729,-0.801904864319242,-0.352887593201084,-0.0979899399649464,0.661204492559326,-0.0353074276609244,0.793422871975105,-0.837102754131842,-0.387244804124702,0.379950022326929,1.09353454995192,0.11286469697211,-0.238402512309026,1.02658415334493,-0.405737978169531,-0.319728049872764,-0.0678533793665141,0.387249596539791,-0.57289942606773,0.621224280780749,-0.757348371955016,-0.0995226548027982,1.34260621422433,0.481352595393131,0.36123875438456,0.725696031349409,0.46264920965195,1.42821849906345,-0.440278745261427,-0.540546039701434,-0.553442266651548,-0.49093139262921,-0.293637136234839,-0.733558079546156,-0.126283772731982,-0.397415357333659,-0.736779913291033,0.223387767872012,0.219747287161707,-0.280300495103552,-0.371326555673896,0.5048026973908,0.889198753971627,0.451122232620754,-0.320969768454131,-0.796093450420807,-0.31519998293736,0.164019068998799,-0.618787824472785,-0.682261924082798,0.309010097143683,-0.770450326898851,-0.378760251921075,-0.0660735214480938,0.319233322557814,-0.15103193559757,0.411678752309201,-0.0361707225437949,1.0735553496112,1.09271229042861,-0.271562559613672,-0.230226950220361,-0.463833247578663,0.454005830614009,0.3994521713863,0.521021290137487,0.372722642029952,1.03412661393332,0.952395517233731,0.746585488938323,0.860055062087748,0.988406726965946,-0.339924842387608,0.593440971150134,0.417819332457096,1.4748062887763),
  PC3=c(0.78619943941128,0.43900154655854,-0.341118402369532,0.859428973982257,0.0027538969866319,0.261614636856235,-1.34075225507253,-0.141238485773147,0.45409680961907,-0.0921369165145691,0.304349936665113,0.670621568821989,0.150090275443703,-0.00630519340032405,-0.217939897320828,0.153913785536882,-0.151732015639995,-0.413303837888523,0.866185004773742,-0.332837341137064,0.0131732954744622,-0.241394043212782,0.41729623042356,-0.0649113326022627,1.20197863235631,0.0446977387781464,-0.354665773452229,-0.181159217591208,-0.273888528930021,-1.12798196562189,0.021890039031337,0.781429491231414,0.295045553919176,-0.0215301667556246,-1.37039669616791,-0.154896374866593,-0.0834548379840878,-0.181538678547918,-0.22585394574165,-0.408738601592734,0.612695296087596,0.152460767067255,-0.062960963201121,0.832870733541891,-0.000147381698993598,-0.0440568613398221,-0.428647800180143,0.392596253920975,-0.300991629277747,0.368250962783333,0.389759121271554,-0.367875465370281,0.15015817198429,-0.263928629365438,0.488991532708951,0.468558428266446,0.0456103925023596,0.768855044247282,0.193526741310598,-0.187671320529716,0.145204422795546,0.0690238768897314,0.302764765795145,-0.139858223316948,0.549653681151185,-0.321488686998528,0.685480426634766,-0.123599715575736,0.12834181518971,0.793496681163536,-0.634720554547527,0.760197797899689,-0.906572581798678,-0.189316129740401,-0.151125605918338,-1.2993501989525,0.0602141217939274,-0.182333698555019,-0.122486801497072,0.105385601478453,0.115374511101675,0.10183779380354,-0.0701463184861918,-0.211101622848838,-0.000185233188477408,0.148300158541414,-0.0653518230584272,-0.297161719872819,-0.341509915525046,-0.033356645558132,0.242133550019011,0.855452789566318,-1.35012232845054,-0.183482091763533,0.341444834709581,-0.066324577740098,1.05960459079952,0.86894681928822,0.476121141928887,-0.193649910316473,0.14535036399593,-0.277789505804451,-0.106503050428773,-0.165814382622154,-0.183839809056858,0.443999312925312,-0.567794411136856,0.152060669939955,-0.0134495792327868,-0.416022681039047,-0.0533562830668323,-0.11240720527039,0.0946074156935571,0.318887201210059,-0.195410482573163,-0.575749169988158,-0.128882469478499,0.195748794005393,-0.170194769581065,-0.0724930628838023,-0.240345090405827,-0.133713214239894,-0.759009487448492,0.182057572862671,0.0734334340698879,-1.04537341217951,0.00939322852534254,-0.0289834688700101,-0.0139702215267289,-0.146550672852971,-0.0790048982707758,-0.163629275776705,-0.307809524158755,0.212777395408197),
  CaseString=c("AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","AMD","AMD","AMD","AMD","AMD","AMD","AMD","AMD","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","Control","AMD")
)

axx <- list(
  gridcolor='rgb(255, 255, 255)',
  zerolinecolor='rgb(255, 255, 255)',
  showbackground=TRUE,
  backgroundcolor='rgb(230, 230,230)'
)

plot_ly(
  data=df, 
  x = ~PC1, 
  y = ~PC2, 
  z = ~PC3, 
  color=~CaseString
  ) %>%
  add_markers(size=3) %>%
  # layout(autosize = F, width = 1000, height = 1000)
  layout(
    # autosize=F,
    # width=700,
    # height=700,
    aspectmode='cube',
    title = 'MiSeq-239 Principal Components',
    scene = list(xaxis=axx, yaxis=axx, zaxis=axx),
    paper_bgcolor = 'rgb(243, 243, 243)',
    plot_bgcolor = 'rgb(243, 243, 243)'
    )

MRE

axx <- list(
  gridcolor='rgb(255, 255, 255)',
  zerolinecolor='rgb(255, 255, 255)',
  showbackground=TRUE,
  backgroundcolor='rgb(230, 230,230)'
)

df = data.frame(
  X=rnorm(100, mean=-5, sd=2),
  Y=rnorm(100, mean=5, sd=5),
  Z=rnorm(100, mean=20, sd=10),
  color=sample(c('R','G'), 100, replace = T)
)

plot_ly(
  data=df, 
  x = ~X, 
  y = ~Y, 
  z = ~Z, 
  color=~color
  ) %>%
  add_markers(size=3) %>%
  # layout(autosize = F, width = 1000, height = 1000)
  layout(
    # autosize=F,
    # width=700,
    # height=700,
    aspectmode='cube',
    title = 'MiSeq-239 Principal Components',
    scene = list(xaxis=axx, yaxis=axx, zaxis=axx),
    paper_bgcolor = 'rgb(243, 243, 243)',
    plot_bgcolor = 'rgb(243, 243, 243)'
    )

mre3d

@abalter I’m working with Python Ploty, but the layout attributes are the same. In your settings the aspectmode='cube' is not a scene attribute, as it should it be. It is defined as a direct layout atrribute.

Chek, please, the Python docs: https://plot.ly/python/reference/#layout-scene-aspectmode.

1 Like

Sweet! Thanks for catching that. Solved the problem.