Search code examples
rtextyaxistruncatedplot3d

Truncated text3D when combined with persp3D


I'd like to use text3D with persp3D to rename and format rotated axes' titles, but that of y0 is truncated.
Does anyone know why?
Thanks for help

library(plot3D)
par(mfrow=c(1,1), mar=c(2.5,0,0,4))
persp3D(x = x0, y = y0, z = z0, border="black", resfac=0.5,
        lwd=0.3, zlim=c(11.3,12.5), theta=-65, phi=15, bty = "g", expand = 0.8,
        ticktype="detailed", xlab=NA, ylab=NA, zlab=NA, axes=TRUE,
        colkey = FALSE)
text3D(55, 320, 11.2, "x0 is ok", adj = 0.9, add = T, srt=72, cex=1.5)
text3D(5.2, 359, 10.98, "y0 is truncated", adj = 0.9, add = T, srt=345, cex=1.5)
text3D(0, 405, 11.95, "z0 is ok", adj = 0.8, add = T, srt=95, cex=1.5)

enter image description here

Data

x0 <-
        c(4, 8.52631578947368, 13.0526315789474, 17.5789473684211, 22.1052631578947, 
          26.6315789473684, 31.1578947368421, 35.6842105263158, 40.2105263157895, 
          44.7368421052632, 49.2631578947368, 53.7894736842105, 58.3157894736842, 
          62.8421052631579, 67.3684210526316, 71.8947368421053, 76.4210526315789, 
          80.9473684210526, 85.4736842105263, 90)

y0 <-
        c(330, 333.157894736842, 336.315789473684, 339.473684210526, 
          342.631578947368, 345.789473684211, 348.947368421053, 352.105263157895, 
          355.263157894737, 358.421052631579, 361.578947368421, 364.736842105263, 
          367.894736842105, 371.052631578947, 374.210526315789, 377.368421052632, 
          380.526315789474, 383.684210526316, 386.842105263158, 390)

z0 <-
        structure(c(11.7612569879335, 11.7689129508959, 11.7767657370987, 
                    11.7828760783414, 11.786957202306, 11.7885428437718, 11.7856439848119, 
                    11.7786587698204, 11.7693312461393, 11.759644100741, 11.7506139166178, 
                    11.7429933215606, 11.7373381920375, 11.7345349196192, 11.735044271562, 
                    11.7383854742358, 11.7436481094135, 11.7499811797297, 11.7568958929273, 
                    11.7640254669695, 11.7471630414405, 11.7499657370007, 11.7525442585571, 
                    11.7541174175986, 11.7534187831101, 11.750129563112, 11.7450020967205, 
                    11.7390810098916, 11.7333229861326, 11.7284922095674, 11.7250948010663, 
                    11.7234642252517, 11.7236934835224, 11.7255349924536, 11.7287573748879, 
                    11.733125376409, 11.7382954227425, 11.7438364094616, 11.7494632614898, 
                    11.7550995742913, 11.7344505504802, 11.7372741563128, 11.7400514571571, 
                    11.7415709766768, 11.7404438605387, 11.7368330733018, 11.7320733986797, 
                    11.7276858277047, 11.724653779922, 11.7232074404261, 11.7235962528367, 
                    11.7258683691046, 11.7297284553287, 11.7346786054199, 11.7403449892159, 
                    11.7463346604372, 11.7523547440745, 11.7582290333736, 11.7639490681328, 
                    11.7696226218867, 11.7230653656506, 11.7280616819708, 11.7331786216883, 
                    11.738121556961, 11.7416829062743, 11.7432534369618, 11.7423167216427, 
                    11.7403461639233, 11.7406263963289, 11.7436224085046, 11.748779068463, 
                    11.7555678844282, 11.763458902119, 11.7713827953303, 11.778658385735, 
                    11.7851541899967, 11.7910271188258, 11.7965274272086, 11.8017172012176, 
                    11.8067653631215, 11.70775899584, 11.7109359739594, 11.7151166925067, 
                    11.7214399770964, 11.7297962252327, 11.7389017649103, 11.7483512057444, 
                    11.75817216308, 11.7682022862248, 11.7786179687181, 11.7893489545389, 
                    11.7999382765001, 11.8100449744127, 11.8195094623152, 11.8281729617543, 
                    11.8359923684782, 11.8431476661595, 11.8498705890218, 11.8563741983411, 
                    11.8627949880037, 11.715611231826, 11.7145949694194, 11.7158352859633, 
                    11.7216149099281, 11.7323377813195, 11.7461078550849, 11.7619652174044, 
                    11.7787757492041, 11.7960049820947, 11.8132871098533, 11.8297227685622, 
                    11.8447394021905, 11.8584494939848, 11.8708666975895, 11.8821565990686, 
                    11.8926809025716, 11.9027909250378, 11.9126481619012, 11.9224061179491, 
                    11.9321114721411, 11.7462918233852, 11.7517037962131, 11.7568347338263, 
                    11.7621128693959, 11.7685607556935, 11.7769618574254, 11.788590713475, 
                    11.8041029042376, 11.8228563856752, 11.8434733494894, 11.8646820447105, 
                    11.8850011570516, 11.9037173098304, 11.9210088747096, 11.9374018285226, 
                    11.9534819731788, 11.9694394245713, 11.9853615601951, 12.0012931375205, 
                    12.0172276444374, 11.7264126765074, 11.7327841741938, 11.7393137212232, 
                    11.7473931839364, 11.7579448272071, 11.7715722060267, 11.7880746174329, 
                    11.8073748505848, 11.829784202577, 11.854992401854, 11.8819611195592, 
                    11.9097574560613, 11.9375154914779, 11.9649615746532, 11.9921380426964, 
                    12.0188928159987, 12.0451804622805, 12.0712649753623, 12.0972952751879, 
                    12.123309528212, 11.6330549142541, 11.6511172941558, 11.6690265184192, 
                    11.6872997173391, 11.7073950766717, 11.7307090870508, 11.7580772092129, 
                    11.7895020641777, 11.8235054368109, 11.858679843184, 11.8949188739592, 
                    11.9324960958356, 11.970999278773, 12.0095262590096, 12.0474977036539, 
                    12.0848064302939, 12.1215309032283, 12.1578043316024, 12.1938958169227, 
                    12.2299282001117, 11.5010814223399, 11.537767615259, 11.5752962624736, 
                    11.6144878701662, 11.6554441394064, 11.6977441841953, 11.7413744293598, 
                    11.7856315470454, 11.8299897021278, 11.8744051897958, 11.9184041894818, 
                    11.9618449323909, 12.0048395618599, 12.0476590077799, 12.0906052971185, 
                    12.1336804760253, 12.1768146747302, 12.2199817311149, 12.2631365738176, 
                    12.3062868563294, 11.3925702571354, 11.4454259089466, 11.4981138344073, 
                    11.5511534829332, 11.60517334196, 11.6596966138618, 11.7134980661257, 
                    11.7654655026989, 11.8150260277506, 11.8624676060587, 11.908842332183, 
                    11.9552269015451, 12.0020640173031, 12.0491280972899, 12.0962172830995, 
                    12.1432700991875, 12.1903348866552, 12.2374001493817, 12.284393181053, 
                    12.3313887440522, 11.3364593557396, 11.3886798766649, 11.4418688236601, 
                    11.4957737854073, 11.5487394984129, 11.600076008853, 11.6496946484256, 
                    11.6983478921735, 11.7472814062858, 11.7971366597429, 11.8472240557648, 
                    11.8971729080403, 11.9468256040542, 11.9959056693969, 12.0447771039725, 
                    12.0935443112376, 12.1418786829134, 12.1896484770348, 12.2370377523263, 
                    12.2843464796566, 11.3469866328133, 11.390072865679, 11.4332919898997, 
                    11.4762899953576, 11.5179009594692, 11.558015596624, 11.5981402949543, 
                    11.6403261526863, 11.684618328663, 11.7301631483064, 11.7763318937372, 
                    11.8222175560606, 11.8672071510016, 11.9109021706825, 11.9532005114428, 
                    11.9944932877019, 12.0353080522564, 12.0759852785732, 12.116685584216, 
                    12.157376522624, 11.3947622498456, 11.4276141789919, 11.4608940698777, 
                    11.4953580058827, 11.5289612614355, 11.5594045295263, 11.5885732476665, 
                    11.6192098605376, 11.6517359537, 11.6844788657364, 11.7165915880217, 
                    11.7482354086972, 11.7791531859362, 11.8092648994003, 11.8390496450558, 
                    11.8687208715357, 11.8982107628192, 11.9275324353938, 11.9567228201295, 
                    11.9859038125513, 11.4440084355464, 11.4729477170212, 11.5016933780494, 
                    11.5298994054391, 11.5571800978384, 11.5824516253128, 11.6051623397242, 
                    11.6236367431847, 11.6393016144108, 11.6544468527719, 11.6701896260319, 
                    11.6863825232751, 11.7020712809488, 11.717491841063, 11.7327403311616, 
                    11.7480434420064, 11.7638028652538, 11.7802418662236, 11.797242905984, 
                    11.8144389508925, 11.4772018836683, 11.501643594317, 11.525446502455, 
                    11.5476872374621, 11.5674373162863, 11.5846182256878, 11.5990257042172, 
                    11.6102282949502, 11.6189763959257, 11.6262470391114, 11.6326968633475, 
                    11.6383776755742, 11.6432119781044, 11.6476061565222, 11.6520385377311, 
                    11.657111869255, 11.6632706192101, 11.670472241835, 11.6783236629465, 
                    11.6864057433456, 11.4876450510757, 11.5051190629642, 11.5221478121585, 
                    11.539663237548, 11.5560247557832, 11.56913538997, 11.5804945915602, 
                    11.5903404132838, 11.5981953176283, 11.6048632338314, 11.6098591060004, 
                    11.6129254925327, 11.6148547266677, 11.6170491152446, 11.6205949321702, 
                    11.6261656925918, 11.6338715701594, 11.6433525487686, 11.6535786975427, 
                    11.6640540680138, 11.5079639930336, 11.5186998795505, 11.5302255413363, 
                    11.5427672997832, 11.55588763864, 11.5699603792767, 11.5843006561764, 
                    11.5978995752009, 11.6098816495061, 11.6206125061012, 11.6303525933029, 
                    11.6402312815795, 11.6513201141625, 11.6644669838375, 11.6800551850742, 
                    11.6981007902709, 11.718415724621, 11.7405057501901, 11.7635213796523, 
                    11.7867950893699, 11.5879696401724, 11.5909242934374, 11.5943990780467, 
                    11.5991517837665, 11.6069736100748, 11.6192427461246, 11.6352661130579, 
                    11.6546121091455, 11.6768906432117, 11.7018317466835, 11.7290504990265, 
                    11.7590797525742, 11.7921353430553, 11.8278060836386, 11.865422854044, 
                    11.9043557101997, 11.9441229184364, 11.984303685549, 12.0245962717943, 
                    12.0649144877822, 11.7209324877247, 11.7143541645974, 11.7067553903127, 
                    11.6974916576136, 11.6886110236861, 11.687138649268, 11.699998976851, 
                    11.7296724895185, 11.7735011764186, 11.82870403545, 11.8918206098373, 
                    11.9592107576092, 12.0280462510252, 12.0954166803865, 12.1597705597961, 
                    12.2206743495043, 12.2779214922974, 12.3315524328505, 12.3825089111389, 
                    12.4317484505825), .Dim = c(20L, 20L))

Solution

  • As Darren said here, "text3D() allows expressions to display mathematical annotation. Remember to set the graphical parameter xpd to TRUE, or the text will be clipped when out of the "plot" region."

    So, adding xpd=TRUE within par() works perfectly:

    par(mfrow=c(1,1), mar=c(2.5,0,0,4), xpd=TRUE)
    persp3D(x = x0, y = y0, z = z0, border="black", resfac=0.5,
            lwd=0.3, zlim=c(11.3,12.5), theta=-65, phi=15, bty = "g", expand = 0.8,
            ticktype="detailed", xlab=NA, ylab=NA, zlab=NA, axes=TRUE,
            colkey = FALSE)
    text3D(55, 320, 11.2, "x0 is ok", adj = 0.9, add = T, srt=72, cex=1.5)
    text3D(5.2, 359, 10.98, "y0 is truncated", adj = 0.9, add = T, srt=345, cex=1.5)
    text3D(0, 405, 11.95, "z0 is ok", adj = 0.8, add = T, srt=95, cex=1.5) 
    

    enter image description here