Skip to content

Commit 0e9223d

Browse files
Merge pull request #45 from xiangpin/master
supporting the main plot with coord_fixed
2 parents 80d088e + e2e467a commit 0e9223d

5 files changed

Lines changed: 84 additions & 21 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ build2: rd
2323
cd ..;\
2424
R CMD build --no-build-vignettes $(PKGSRC)
2525

26-
install: build
26+
install: build2
2727
cd ..;\
2828
R CMD INSTALL $(PKGNAME)_$(PKGVERS).tar.gz
2929

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ importFrom(ggfun,yrange)
3939
importFrom(ggplot2,aes)
4040
importFrom(ggplot2,annotation_custom)
4141
importFrom(ggplot2,coord_cartesian)
42+
importFrom(ggplot2,coord_fixed)
4243
importFrom(ggplot2,element_blank)
4344
importFrom(ggplot2,element_text)
4445
importFrom(ggplot2,ggplot)

R/aplot.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,35 @@ as.patchwork <- function(x,
5050
return(res)
5151
}
5252

53-
mp <- x$plotlist[[1]]
53+
mp <- x[[1]]
5454
if ( length(x$plotlist) == 1) {
5555
return(ggplotGrob(mp))
5656
}
57-
57+
width <- x$width
58+
height <- x$height
5859
if(align == "x" || align == "xy"){
59-
for (i in x$layout[, x$main_col]) {
60+
for (ind in seq(length(x$layout[, x$main_col]))) {
61+
i <- x$layout[,x$main_col][ind]
6062
if (is.na(i)) next
6163
if (i == 1) next
62-
x$plotlist[[i]] <- suppressMessages(x$plotlist[[i]] + xlim2(mp))
64+
x[[i]] <- suppressMessages(x[[i]] + xlim2(mp))
65+
x <- adjust_coord(x, i, ind, type = "height")
6366
}
6467
}
6568

6669
if(align == "y" || align == "xy"){
67-
for (i in x$layout[x$main_row,]) {
70+
for (ind in seq(length(x$layout[x$main_row,]))) {
71+
i <- x$layout[x$main_row,][ind]
6872
if(is.na(i)) next
6973
if (i == 1) next
70-
x$plotlist[[i]] <- suppressMessages(x$plotlist[[i]] + ylim2(mp))
74+
x[[i]] <- suppressMessages(x[[i]] + ylim2(mp))
75+
x <- adjust_coord(x, i, ind, type = "width")
7176
}
7277
}
78+
79+
if (is.coord_fixed(mp)){
80+
width <- height <- NULL
81+
}
7382

7483
idx <- as.vector(x$layout)
7584
idx[is.na(idx)] <- x$n + 1
@@ -104,8 +113,8 @@ as.patchwork <- function(x,
104113

105114
pp + plot_layout(byrow=F,
106115
ncol=ncol(x$layout),
107-
widths = x$width,
108-
heights= x$height,
116+
widths = width,
117+
heights= height,
109118
guides = guides)
110119
}
111120

R/method-accessor.R

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,52 @@
11
##' @method [[ aplot
22
##' @export
33
`[[.aplot` <- function(x, i){
4-
x$plotlist[[i]]
4+
if(inherits(i, "numeric")){
5+
return(x$plotlist[[i]])
6+
}
7+
NextMethod()
58
}
69

710
##' @method [ aplot
811
##' @export
912
`[.aplot` <- function(x, i, j, ...){
10-
x[[x$layout[i, j]]]
13+
if (inherits(i, "numeric") && inherits(i, "numeric")){
14+
return(x[[x$layout[i, j]]])
15+
}
16+
NextMethod()
1117
}
1218

1319
##' @method [[<- aplot
1420
##' @export
1521
`[[<-.aplot` <- function(x, i, value){
16-
if(!inherits(value, 'ggplot')){
17-
stop('The value should be a ggplot object.')
22+
if (inherits(i, "numeric")){
23+
if(!inherits(value, 'ggplot')){
24+
stop('The value should be a ggplot object.')
25+
}
26+
27+
x$plotlist[[i]] <- value
28+
return(x)
1829
}
19-
x$plotlist[[i]] <- value
30+
x <- NextMethod(value)
2031
return(x)
2132
}
2233

2334

2435
##' @method [<- aplot
2536
##' @export
2637
`[<-.aplot` <- function(x, i, j, value){
27-
if (!inherits(value, 'ggplot')){
28-
stop('The value should be a ggplot object.')
29-
}else if (is.na(x$layout[i, j])){
30-
stop(paste0('The subplot which local in row ', i,
31-
' and col ', j,
32-
' is NULL, it can not be replaced.'))
38+
if (inherits(i, "numeric") && inherits(i, "numeric")){
39+
if (!inherits(value, 'ggplot')){
40+
stop('The value should be a ggplot object.')
41+
}else if (is.na(x$layout[i, j])){
42+
stop(paste0('The subplot which local in row ', i,
43+
' and col ', j,
44+
' is NULL, it can not be replaced.'))
45+
}
46+
x[[x$layout[i, j]]] <- value
47+
return(x)
3348
}
34-
x[[x$layout[i, j]]] <- value
49+
x <- NextMethod(value)
3550
return(x)
3651
}
3752

R/utilities.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,41 @@ is.coord_flip <- function(p) {
1919
inherits(p, "gg") && inherits(p$coordinates, "CoordFlip")
2020
}
2121

22+
is.coord_fixed <- function(p){
23+
inherits(p, "gg") && inherits(p$coordinates, "CoordFixed")
24+
}
25+
26+
#' @importFrom ggplot2 coord_fixed
27+
adjust_coord <- function(x, i, ind, type='width'){
28+
coordfixed <- is.coord_fixed(x[[1]])
29+
coordfixed2 <- !is.coord_fixed(x[[i]])
30+
ratio <- 1
31+
if (coordfixed && coordfixed2){
32+
ajustcoord <- getOption("ajust_coord", default = TRUE)
33+
if (ajustcoord) ratio <- .cal_ratio(x[[i]], x[[type]][ind], type)
34+
x[[i]] <- suppressMessages(x[[i]] + coord_fixed(ratio = ratio))
35+
36+
}
37+
return(x)
38+
}
39+
40+
.cal_ratio <- function(x, size, type='width'){
41+
xr <- .cal_limit_range(xlim2(x)$limits)
42+
yr <- .cal_limit_range(ylim2(x)$limits)
43+
val <- xr / yr
44+
if (type == 'width'){
45+
val <- val / size
46+
}else{
47+
val <- val * size
48+
}
49+
return(val)
50+
}
51+
52+
.cal_limit_range <- function(x){
53+
if (inherits(x, "character")){
54+
res <- length(x)
55+
}else if(inherits(x, "numeric")){
56+
res <- diff(x)
57+
}
58+
return(res)
59+
}

0 commit comments

Comments
 (0)