Browse code

xlim-expand supports ggplot2

Guangchuang Yu authored on 21/05/2019 10:32:17
Showing 3 changed files

  • NAMESPACE index 938eddaf..4ab8eb57 100644
  • NEWS.md index 42d90365..f3e1df4a 100644
  • R/xlim.R index 47ed8d36..2a251c7b 100644
... ...
@@ -9,6 +9,7 @@ S3method(fortify,phylo4)
9 9
 S3method(fortify,phylo4d)
10 10
 S3method(fortify,phyloseq)
11 11
 S3method(fortify,treedata)
12
+S3method(ggplot_add,facet_xlim)
12 13
 S3method(groupClade,ggtree)
13 14
 S3method(groupOTU,ggtree)
14 15
 S3method(identify,gg)
... ...
@@ -164,6 +165,7 @@ importFrom(ggplot2,geom_text)
164 165
 importFrom(ggplot2,geom_tile)
165 166
 importFrom(ggplot2,ggplot)
166 167
 importFrom(ggplot2,ggplotGrob)
168
+importFrom(ggplot2,ggplot_add)
167 169
 importFrom(ggplot2,ggplot_build)
168 170
 importFrom(ggplot2,ggplot_gtable)
169 171
 importFrom(ggplot2,ggproto)
... ...
@@ -205,11 +207,13 @@ importFrom(grid,viewport)
205 207
 importFrom(magrittr,"%<>%")
206 208
 importFrom(magrittr,"%>%")
207 209
 importFrom(magrittr,equals)
210
+importFrom(magrittr,extract)
208 211
 importFrom(methods,is)
209 212
 importFrom(methods,missingArg)
210 213
 importFrom(methods,setGeneric)
211 214
 importFrom(methods,setOldClass)
212 215
 importFrom(rlang,.data)
216
+importFrom(rlang,quo_name)
213 217
 importFrom(rvcheck,get_aes_var)
214 218
 importFrom(rvcheck,get_fun_from_pkg)
215 219
 importFrom(tidyr,gather)
... ...
@@ -1,5 +1,7 @@
1 1
 # ggtree 1.17.1
2 2
 
3
++ extend `xlim_expand` to work with `ggplot2` (2019-05-20, Tue)
4
+  - <https://yulab-smu.github.io/treedata-book/chapter9.html#xlim_expand>
3 5
 + add `legend_title` variable in `gheatmap` (2019-05-16, Thu)
4 6
 
5 7
 # ggtree 1.16.0
... ...
@@ -23,7 +23,27 @@ xlim_tree <- function(xlim) {
23 23
 ##' @author guangchuang yu
24 24
 xlim_expand <- function(xlim, panel) {
25 25
     dummy <- data.frame(x=xlim, .panel=panel)
26
-    geom_blank(aes_(x=~x), dummy, inherit.aes = FALSE)
26
+    ly <- geom_blank(aes_(x=~x), dummy, inherit.aes = FALSE)
27
+    class(ly) <- c("facet_xlim", class(ly))
28
+    return(ly)
29
+}
30
+
31
+##' @importFrom rlang quo_name
32
+##' @importFrom magrittr extract
33
+##' @importFrom ggplot2 ggplot_add
34
+##' @method ggplot_add facet_xlim
35
+##' @export
36
+ggplot_add.facet_xlim <- function(object, plot, object_name) {
37
+    var <- quo_name(plot$facet$params$cols[[1]])
38
+    free_x <- plot$facet$params$free$x
39
+    if (!is.null(free_x) && !free_x) {
40
+        message('If you want to adjust xlim for specific panel, you need to set `scales = "free_x"`')
41
+    }
42
+    class(object) %<>% extract(., .!= "facet_xlim")
43
+    nm <- names(object$data)
44
+    nm[nm == '.panel']  <- var
45
+    names(object$data)  <- nm
46
+    ggplot_add(object, plot, object_name)
27 47
 }
28 48
 
29 49
 ##' reverse timescle x-axis