rggplot2ggplotly

How to stop ggplotly enclosing geom_density plot?


I'm trying to plot a density curve in ggplotly. My full data is very different from this example, but the same behaviour is demonstrated here:

library(ggplot2)
library(plotly)

a = rnorm(100,10,10)
a <- data.frame(a)
p <- ggplot(a, aes(x=a)) + 
     geom_density()

If I plot it, I get the curve that I want.

plot(p)

plain plot

But if I use ggplotly, I get this odd result rendered, which is plain wrong with it drawing lines to enclose the curve:

ggplotly(p)

ggplotly

What can one do to remove that enclosure? I've had a dig around in the ggplotly object (towards the end of this post). Firstly though, I've seen people use geom_density(position = "identity") but that doesn't help.

p <- ggplot(a, aes(x=a)) + 
     geom_density(position = "identity")
ggplotly(p)

ggplotly plot with position = "identity" in ggplot2 object

So I've had a dig into the ggplotly object to see what is going on. There are 1026 values in the object in x & y.

length(d$x[4]$data[[4]]$x)
> [4] 1026
length(d$x[4]$data[[4]]$y)
> [4] 1026

When I look at the y values it has:

d <- ggplotly(p)

d$x[4]$data[[4]]$y[1:600]
  [4] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
 [17] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
 [33] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
 [49] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
 [65] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
 [81] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
 [97] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[113] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[129] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[145] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[161] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[177] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[193] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[209] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[225] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[241] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[257] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[273] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[289] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[305] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[321] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[337] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[353] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[369] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[385] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[401] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[417] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[433] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[449] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[465] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[481] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[497] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[513] 0.0000000000 0.0012671853 0.0012711407 0.0012735703 0.0012738452 0.0012727952 0.0012704564 0.0012665862 0.0012612032 0.0012547938 0.0012474102 0.0012387904 0.0012293494 0.0012192866 0.0012086507 0.0011974075
[529] 0.0011859353 0.0011743015 0.0011625991 0.0011510454 0.0011397231 0.0011286950 0.0011182647 0.0011084907 0.0010993500 0.0010909116 0.0010837321 0.0010774443 0.0010720787 0.0010678911 0.0010651175 0.0010633938
[545] 0.0010627292 0.0010635706 0.0010656675 0.0010688022 0.0010729679 0.0010786761 0.0010852796 0.0010927405 0.0011011626 0.0011105469 0.0011205186 0.0011310238 0.0011421166 0.0011535347 0.0011651267 0.0011768285
[561] 0.0011884455 0.0011998402 0.0012109460 0.0012215932 0.0012314636 0.0012406558 0.0012491081 0.0012563798 0.0012624416 0.0012674327 0.0012713034 0.0012732445 0.0012738352 0.0012730754 0.0012706862 0.0012662312
[577] 0.0012602995 0.0012528815 0.0012434209 0.0012321192 0.0012193400 0.0012050938 0.0011886265 0.0011707403 0.0011515229 0.0011308178 0.0011083864 0.0010848467 0.0010602435 0.0010343235 0.0010073406 0.0009795924
[593] 0.0009511323 0.0009217746 0.0008919515 0.0008617518 0.0008312109 0.0008004394 0.0007696296 0.0007388368

Just eyeballing the data, the first 513 y values are 0. So if I look at X over that range from 1:513 :

d$x[4]$data[[4]]$x[1:513]
  [4] -18.21790571 -18.08147246 -17.94503921 -17.80860596 -17.67217272 -17.53573947 -17.39930622 -17.26287298 -17.12643973 -16.99000648 -16.85357323 -16.71713999 -16.58070674 -16.44427349 -16.30784025 -16.17140700
 [17] -16.03497375 -15.89854051 -15.76210726 -15.62567401 -15.48924076 -15.35280752 -15.21637427 -15.07994102 -14.94350778 -14.80707453 -14.67064128 -14.53420803 -14.39777479 -14.26134154 -14.12490829 -13.98847505
 [33] -13.85204180 -13.71560855 -13.57917530 -13.44274206 -13.30630881 -13.16987556 -13.03344232 -12.89700907 -12.76057582 -12.62414257 -12.48770933 -12.35127608 -12.21484283 -12.07840959 -11.94197634 -11.80554309
 [49] -11.66910985 -11.53267660 -11.39624335 -11.25981010 -11.12337686 -10.98694361 -10.85051036 -10.71407712 -10.57764387 -10.44121062 -10.30477737 -10.16834413 -10.03191088  -9.89547763  -9.75904439  -9.62261114
 [65]  -9.48617789  -9.34974464  -9.21331140  -9.07687815  -8.94044490  -8.80401166  -8.66757841  -8.53114516  -8.39471192  -8.25827867  -8.12184542  -7.98541217  -7.84897893  -7.71254568  -7.57611243  -7.43967919
 [81]  -7.30324594  -7.16681269  -7.03037944  -6.89394620  -6.75751295  -6.62107970  -6.48464646  -6.34821321  -6.21177996  -6.07534671  -5.93891347  -5.80248022  -5.66604697  -5.52961373  -5.39318048  -5.25674723
 [97]  -5.12031399  -4.98388074  -4.84744749  -4.71101424  -4.57458100  -4.43814775  -4.30171450  -4.16528126  -4.02884801  -3.89241476  -3.75598151  -3.61954827  -3.48311502  -3.34668177  -3.21024853  -3.07381528
[113]  -2.93738203  -2.80094878  -2.66451554  -2.52808229  -2.39164904  -2.25521580  -2.11878255  -1.98234930  -1.84591606  -1.70948281  -1.57304956  -1.43661631  -1.30018307  -1.16374982  -1.02731657  -0.89088333
[129]  -0.75445008  -0.61801683  -0.48158358  -0.34515034  -0.20871709  -0.07228384   0.06414940   0.20058265   0.33701590   0.47344915   0.60988239   0.74631564   0.88274889   1.01918213   1.15561538   1.29204863
[145]   1.42848187   1.56491512   1.70134837   1.83778162   1.97421486   2.11064811   2.24708136   2.38351460   2.51994785   2.65638110   2.79281435   2.92924759   3.06568084   3.20211409   3.33854733   3.47498058
[161]   3.61141383   3.74784708   3.88428032   4.02071357   4.15714682   4.29358006   4.43001331   4.56644656   4.70287980   4.83931305   4.97574630   5.11217955   5.24861279   5.38504604   5.52147929   5.65791253
[177]   5.79434578   5.93077903   6.06721228   6.20364552   6.34007877   6.47651202   6.61294526   6.74937851   6.88581176   7.02224501   7.15867825   7.29511150   7.43154475   7.56797799   7.70441124   7.84084449
[193]   7.97727773   8.11371098   8.25014423   8.38657748   8.52301072   8.65944397   8.79587722   8.93231046   9.06874371   9.20517696   9.34161021   9.47804345   9.61447670   9.75090995   9.88734319  10.02377644
[209]  10.16020969  10.29664294  10.43307618  10.56950943  10.70594268  10.84237592  10.97880917  11.11524242  11.25167566  11.38810891  11.52454216  11.66097541  11.79740865  11.93384190  12.07027515  12.20670839
[225]  12.34314164  12.47957489  12.61600814  12.75244138  12.88887463  13.02530788  13.16174112  13.29817437  13.43460762  13.57104087  13.70747411  13.84390736  13.98034061  14.11677385  14.25320710  14.38964035
[241]  14.52607359  14.66250684  14.79894009  14.93537334  15.07180658  15.20823983  15.34467308  15.48110632  15.61753957  15.75397282  15.89040607  16.02683931  16.16327256  16.29970581  16.43613905  16.57257230
[257]  16.70900555  16.84543880  16.98187204  17.11830529  17.25473854  17.39117178  17.52760503  17.66403828  17.80047152  17.93690477  18.07333802  18.20977127  18.34620451  18.48263776  18.61907101  18.75550425
[273]  18.89193750  19.02837075  19.16480400  19.30123724  19.43767049  19.57410374  19.71053698  19.84697023  19.98340348  20.11983673  20.25626997  20.39270322  20.52913647  20.66556971  20.80200296  20.93843621
[289]  21.07486945  21.21130270  21.34773595  21.48416920  21.62060244  21.75703569  21.89346894  22.02990218  22.16633543  22.30276868  22.43920193  22.57563517  22.71206842  22.84850167  22.98493491  23.12136816
[305]  23.25780141  23.39423466  23.53066790  23.66710115  23.80353440  23.93996764  24.07640089  24.21283414  24.34926738  24.48570063  24.62213388  24.75856713  24.89500037  25.03143362  25.16786687  25.30430011
[321]  25.44073336  25.57716661  25.71359986  25.85003310  25.98646635  26.12289960  26.25933284  26.39576609  26.53219934  26.66863259  26.80506583  26.94149908  27.07793233  27.21436557  27.35079882  27.48723207
[337]  27.62366531  27.76009856  27.89653181  28.03296506  28.16939830  28.30583155  28.44226480  28.57869804  28.71513129  28.85156454  28.98799779  29.12443103  29.26086428  29.39729753  29.53373077  29.67016402
[353]  29.80659727  29.94303052  30.07946376  30.21589701  30.35233026  30.48876350  30.62519675  30.76163000  30.89806324  31.03449649  31.17092974  31.30736299  31.44379623  31.58022948  31.71666273  31.85309597
[369]  31.98952922  32.12596247  32.26239572  32.39882896  32.53526221  32.67169546  32.80812870  32.94456195  33.08099520  33.21742845  33.35386169  33.49029494  33.62672819  33.76316143  33.89959468  34.03602793
[385]  34.17246117  34.30889442  34.44532767  34.58176092  34.71819416  34.85462741  34.99106066  35.12749390  35.26392715  35.40036040  35.53679365  35.67322689  35.80966014  35.94609339  36.08252663  36.21895988
[401]  36.35539313  36.49182638  36.62825962  36.76469287  36.90112612  37.03755936  37.17399261  37.31042586  37.44685910  37.58329235  37.71972560  37.85615885  37.99259209  38.12902534  38.26545859  38.40189183
[417]  38.53832508  38.67475833  38.81119158  38.94762482  39.08405807  39.22049132  39.35692456  39.49335781  39.62979106  39.76622431  39.90265755  40.03909080  40.17552405  40.31195729  40.44839054  40.58482379
[433]  40.72125704  40.85769028  40.99412353  41.13055678  41.26699002  41.40342327  41.53985652  41.67628976  41.81272301  41.94915626  42.08558951  42.22202275  42.35845600  42.49488925  42.63132249  42.76775574
[449]  42.90418899  43.04062224  43.17705548  43.31348873  43.44992198  43.58635522  43.72278847  43.85922172  43.99565497  44.13208821  44.26852146  44.40495471  44.54138795  44.67782120  44.81425445  44.95068769
[465]  45.08712094  45.22355419  45.35998744  45.49642068  45.63285393  45.76928718  45.90572042  46.04215367  46.17858692  46.31502017  46.45145341  46.58788666  46.72431991  46.86075315  46.99718640  47.13361965
[481]  47.27005290  47.40648614  47.54291939  47.67935264  47.81578588  47.95221913  48.08865238  48.22508562  48.36151887  48.49795212  48.63438537  48.77081861  48.90725186  49.04368511  49.18011835  49.31655160
[497]  49.45298485  49.58941810  49.72585134  49.86228459  49.99871784  50.13515108  50.27158433  50.40801758  50.54445083  50.68088407  50.81731732  50.95375057  51.09018381  51.22661706  51.36305031  51.49948355
[513]  51.49948355

It spans the entirety of the X range:

range(d$x[4]$data[[4]]$x)
> [4] -18.21791  51.49948

The following 513 values of X & Y are the real deal. The X data then sweeps back from 51.49948 to -12.2179 over 514:1026. In this range, the Y values represent the actual curve.

So, the first first 513 values are y=0, which is the line rendered at the bottom of the curve, and the remaining 513 for y are the values that should be plotted.

When the curve is plotted, it inevitably starts at max(x) and finishes at min(x) so there are two points plotted for X at these extremes, which probably represent the vertical lines that enclose the ggplotly rendered geom_density curve. I can hack the ggplot object to make ggplotly "behave".

p <- ggplot(a, aes(x=a)) + 
    geom_density() + scale_y_continuous(limits = c(0.00000001,NA))

This really upsets plot(p), rendering nothing. However ggplotly renders a plot without "borders"

ggplotly(p)

enter image description here

This isn't ideal, but is a hack that works.

So, why does ggplotly generate the y=0 values like it does? How can I stop ggplotly from adding this nonsense data, so I get the correct render from ggplotly without having to hack the y-scale?

It might be something as simple as an option for ggplotly, but I can't see anything.


Solution

  • If you just want the line then use stat_density() instead:

    library(ggplot2)
    library(plotly)
    
    set.seed(1)
    a = rnorm(100,10,10)
    a <- data.frame(a)
    p <- ggplot(a, aes(x=a)) + 
      stat_density(geom="line")
    
    ggplotly(p)
    

    result