Browse code

extend `xlim2` and `ylim2` to support discrete scale.

Guangchuang Yu authored on 01/11/2019 09:06:55
Showing 4 changed files

... ...
@@ -198,6 +198,7 @@ importFrom(ggplot2,guides)
198 198
 importFrom(ggplot2,labeller)
199 199
 importFrom(ggplot2,last_plot)
200 200
 importFrom(ggplot2,layer)
201
+importFrom(ggplot2,layer_scales)
201 202
 importFrom(ggplot2,position_nudge)
202 203
 importFrom(ggplot2,scale_color_manual)
203 204
 importFrom(ggplot2,scale_fill_discrete)
... ...
@@ -205,8 +206,10 @@ importFrom(ggplot2,scale_fill_gradient)
205 206
 importFrom(ggplot2,scale_fill_manual)
206 207
 importFrom(ggplot2,scale_x_continuous)
207 208
 importFrom(ggplot2,scale_x_date)
209
+importFrom(ggplot2,scale_x_discrete)
208 210
 importFrom(ggplot2,scale_x_reverse)
209 211
 importFrom(ggplot2,scale_y_continuous)
212
+importFrom(ggplot2,scale_y_discrete)
210 213
 importFrom(ggplot2,theme)
211 214
 importFrom(ggplot2,theme_bw)
212 215
 importFrom(ggplot2,theme_void)
... ...
@@ -1,6 +1,7 @@
1 1
 # ggtree 2.1.1
2 2
 
3 3
 + convert roxygen documents using markdown (2019-11-01, Fri)
4
++ extend `xlim2` and `ylim2` to support discrete scale.
4 5
 + `xlim2` and `ylim2` to uniformize axis limits of ggplot objects (2019-10-31, Thu)
5 6
   - <https://yulab-smu.github.io/treedata-book/chapter7.html#composite_plot>
6 7
   - <https://yulab-smu.github.io/treedata-book/docs/chapter10.html#axis_align>
... ...
@@ -95,14 +95,19 @@ axis_align <- function(gg, limits = NULL, axis) {
95 95
 
96 96
 
97 97
 yrange <- function(gg) {
98
-    ggrange(gg, "y.range")
98
+    ggrange(gg, "y")
99 99
 }
100 100
 
101 101
 xrange <- function(gg) {
102
-    ggrange(gg, "x.range")
102
+    ggrange(gg, "x")
103 103
 }
104 104
 
105
+##' @importFrom ggplot2 layer_scales
105 106
 ggrange <- function(gg, var) {
107
+    res <- layer_scales(gg)[[var]]$range$range
108
+    if (is.character(res)) return(res)
109
+
110
+    var <- paste0(var, ".range")
106 111
     ggplot_build(gg)$layout$panel_params[[1]][[var]]
107 112
 }
108 113
 
... ...
@@ -22,15 +22,23 @@ ggplot_add.facet_xlim <- function(object, plot, object_name) {
22 22
 
23 23
 
24 24
 ##' @method ggplot_add axisAlign
25
+##' @importFrom ggplot2 scale_x_discrete
26
+##' @importFrom ggplot2 scale_y_discrete
25 27
 ##' @export
26 28
 ggplot_add.axisAlign <- function(object, plot, object_name) {
27 29
     limits <- object$limits
30
+
28 31
     ## expand_limits <- object$expand_limits
29 32
     ## limits[1] <- limits[1] + (limits[1] * expand_limits[1]) - expand_limits[2]
30 33
     ## limits[2] <- limits[2] + (limits[2] * expand_limits[3]) + expand_limits[4]
31 34
 
32
-    lim_x <- scale_x_continuous(limits=limits, expand=c(0,0))
33
-    lim_y <- scale_y_continuous(limits = limits, expand = c(0, 0))
35
+    if (is.numeric(limits)) {
36
+        lim_x <- scale_x_continuous(limits=limits, expand=c(0,0))
37
+        lim_y <- scale_y_continuous(limits = limits, expand = c(0, 0))
38
+    } else {
39
+        lim_x <- scale_x_discrete(limits=limits, expand = c(0, 0.6))
40
+        lim_y <- scale_y_discrete(limits = limits, expand = c(0, 0.6))
41
+    }
34 42
 
35 43
     if (object$axis == 'x') {
36 44
         ## if (object$by == "x") {