martin-borkovec / ggparty

147 stars 14 forks source link

theme(legend.position = "none") does not remove legend #26

Closed HeidiSeibold closed 5 years ago

HeidiSeibold commented 5 years ago

I would like to remove the legend from the plot below. Is there a way? Shouldn't theme(legend.position = "none") work?

library("ggparty")
data("PimaIndiansDiabetes", package = "mlbench")

tr <- ctree(diabetes ~ glucose + age,
            data = PimaIndiansDiabetes)

ggparty(tr) + 
  geom_edge() + geom_node_plot(
  gglist = list(geom_bar(aes(x = !!tr$terms[[2]], 
                             fill = !!tr$terms[[2]])))
) + theme(legend.position = "none")

Created on 2019-04-10 by the reprex package (v0.2.1)

Session info ``` r devtools::session_info() #> ─ Session info ────────────────────────────────────────────────────────── #> setting value #> version R version 3.5.3 (2019-03-11) #> os Ubuntu 18.04.2 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Berlin #> date 2019-04-10 #> #> ─ Packages ────────────────────────────────────────────────────────────── #> package * version date lib #> assertthat 0.2.1 2019-03-21 [1] #> backports 1.1.3 2018-12-14 [1] #> callr 3.2.0 2019-03-15 [1] #> checkmate 1.9.1 2019-01-15 [1] #> cli 1.1.0 2019-03-19 [1] #> colorspace 1.4-1 2019-03-18 [1] #> crayon 1.3.4 2017-09-16 [1] #> curl 3.3 2019-01-10 [1] #> desc 1.2.0 2018-05-01 [1] #> devtools 2.0.2 2019-04-08 [1] #> digest 0.6.18 2018-10-10 [1] #> evaluate 0.13 2019-02-12 [1] #> Formula 1.2-3 2018-05-03 [1] #> fs 1.2.7 2019-03-19 [1] #> ggparty * 0.0.0.9000 2019-04-10 [1] #> ggplot2 * 3.1.1 2019-04-07 [1] #> glue 1.3.1 2019-03-12 [1] #> gtable 0.3.0 2019-03-25 [1] #> highr 0.8 2019-03-20 [1] #> htmltools 0.3.6 2017-04-28 [1] #> httr 1.4.0 2018-12-11 [1] #> inum 1.0-0 2017-12-12 [1] #> knitr 1.22 2019-03-08 [1] #> labeling 0.3 2014-08-23 [1] #> lattice 0.20-38 2018-11-04 [4] #> lazyeval 0.2.2 2019-03-15 [1] #> libcoin * 1.0-4 2019-02-28 [1] #> magrittr 1.5 2014-11-22 [1] #> Matrix 1.2-17 2019-03-22 [4] #> memoise 1.1.0 2017-04-21 [1] #> mime 0.6 2018-10-05 [1] #> munsell 0.5.0 2018-06-12 [1] #> mvtnorm * 1.0-10 2019-03-05 [1] #> partykit * 1.2-3 2019-01-31 [1] #> pillar 1.3.1 2018-12-15 [1] #> pkgbuild 1.0.3 2019-03-20 [1] #> pkgconfig 2.0.2 2018-08-16 [1] #> pkgload 1.0.2 2018-10-29 [1] #> plyr 1.8.4 2016-06-08 [1] #> prettyunits 1.0.2 2015-07-13 [1] #> processx 3.3.0 2019-03-10 [1] #> ps 1.3.0 2018-12-21 [1] #> R6 2.4.0 2019-02-14 [1] #> Rcpp 1.0.1 2019-03-17 [1] #> remotes 2.0.3 2019-04-09 [1] #> rlang 0.3.4 2019-04-07 [1] #> rmarkdown 1.12 2019-03-14 [1] #> rpart 4.1-13 2018-02-23 [4] #> rprojroot 1.3-2 2018-01-03 [1] #> scales 1.0.0 2018-08-09 [1] #> sessioninfo 1.1.1 2018-11-05 [1] #> stringi 1.4.3 2019-03-12 [1] #> stringr 1.4.0 2019-02-10 [1] #> survival 2.43-3 2018-11-26 [4] #> tibble 2.1.1 2019-03-16 [1] #> usethis 1.5.0 2019-04-07 [1] #> withr 2.1.2 2018-03-15 [1] #> xfun 0.6 2019-04-02 [1] #> xml2 1.2.0 2018-01-24 [1] #> yaml 2.2.0 2018-07-25 [1] #> source #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> Github (mmostly-harmless/ggparty@c5429b1) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.1) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.0) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.1) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> CRAN (R 3.5.3) #> #> [1] /home/kennwortistkennwort/R/x86_64-pc-linux-gnu-library/3.5 #> [2] /usr/local/lib/R/site-library #> [3] /usr/lib/R/site-library #> [4] /usr/lib/R/library ```
martin-borkovec commented 5 years ago

well... no.

there are two different ggplot-levels to this ggparty-plot. first you have your base-ggplot, the tree. in this case consisting only of geom_edge(). then you have the ggplot of the geom_nodeplot. in this case the geom_bar() as set in the gglist_argument.

you have to keep this in mind when deciding where to put each additional ggplot component. since you want to remove the legend of the geom_bar, you have to add the theme() call to gglist of geom_node.

at the place where you put it, it would remove legends created by geom_edge,

i'm aware that this can be tricky and i hope i will be able to make this sufficiently clear in the vignette which will be coming soon.

looking into this, i realized, that passing the legend.position didn't really work as intended (and described above). so i reworked it a bit and now there is an additional argument called shared_legend which if TRUE will plot one legend at the bottom of the plot (basically what the behaviour of geom_node_plot was untill now) and also overwrite any other legend.position settings. so if you want to remove the legend you must not set it to TRUE. for now it defaults to FALSE.

so you should install the latest build for this to work properly. nevermind that it is failing, because i changed some other things so some tests don't pass at the moment...

library("ggparty")
#> Loading required package: ggplot2
#> Loading required package: partykit
#> Loading required package: grid
#> Loading required package: libcoin
#> Loading required package: mvtnorm
data("PimaIndiansDiabetes", package = "mlbench")

tr <- ctree(diabetes ~ glucose + age,
            data = PimaIndiansDiabetes)

ggparty(tr) + 
  geom_edge() +
  geom_node_plot(
    gglist = list(geom_bar(aes(x = !!tr$terms[[2]], 
                               fill = !!tr$terms[[2]])),
                  theme(legend.position = "none"))
  )

Created on 2019-04-10 by the reprex package (v0.2.1)