Skip to content

Commit

Permalink
add direction argument in annotation_axis_grob()
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Apr 17, 2019
1 parent c79aaf9 commit 278d633
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 64 deletions.
104 changes: 72 additions & 32 deletions R/annotation_axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# -gp Graphic parameters.
# -side side of the axis of the annotation viewport.
# -facing Facing of the axis.
# -direction direction of the axis. Value should be "normal" or "reverse".
#
# == value
# A `grid::grob` object.
Expand All @@ -16,109 +17,147 @@
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
# side = "left", facing = "outside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "left", facing = "outside"')
# grid.draw(gb)
# grid.rect(x = unit(0, "npc"), width = grobWidth(gb), just = "right")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
# side = "left", facing = "inside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "left", facing = "inside"')
# grid.draw(gb)
# grid.rect(x = unit(0, "npc"), width = grobWidth(gb), just = "left")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
# side = "right", facing = "outside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "right", facing = "outside"')
# grid.draw(gb)
# grid.rect(x = unit(1, "npc"), width = grobWidth(gb), just = "left")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
# side = "right", facing = "inside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "right", facing = "inside"')
# grid.draw(gb)
# grid.rect(x = unit(1, "npc"), width = grobWidth(gb), just = "right")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
# gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 0,
# side = "top", facing = "outside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "top", facing = "outside"')
# grid.draw(gb)
# grid.rect(y = unit(1, "npc"), height = grobHeight(gb), just = "bottom")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 90,
# gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 90,
# side = "top", facing = "outside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "top", facing = "outside"')
# grid.draw(gb)
# grid.rect(y = unit(1, "npc"), height = grobHeight(gb), just = "bottom")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 45,
# gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 45,
# side = "top", facing = "outside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "top", facing = "outside"')
# grid.draw(gb)
# grid.rect(y = unit(1, "npc"), height = grobHeight(gb), just = "bottom")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
# gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 0,
# side = "top", facing = "inside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "top", facing = "inside"')
# grid.draw(gb)
# grid.rect(y = unit(1, "npc"), height = grobHeight(gb), just = "top")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
# gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 0,
# side = "bottom", facing = "outside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "bottom", facing = "outside"')
# grid.draw(gb)
# grid.rect(y = unit(0, "npc"), height = grobHeight(gb), just = "top")
# popViewport()
#
# gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
# gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 0,
# side = "bottom", facing = "inside")
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# grid.rect()
# grid.text('side = "bottom", facing = "inside"')
# grid.draw(gb)
# grid.rect(y = unit(0, "npc"), height = grobHeight(gb), just = "bottom")
# popViewport()
#
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# gb = annotation_axis_grob(labels_rot = 0, side = "left", facing = "outside")
# grid.rect()
# grid.text('side = "left", facing = "outside"')
# grid.draw(gb)
# popViewport()
#
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# gb = annotation_axis_grob(side = "left", direction = "reverse")
# grid.rect()
# grid.text('side = "left", direction = "reverse')
# grid.draw(gb)
# popViewport()
#
# grid.newpage()
# pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
# gb = annotation_axis_grob(side = "bottom", direction = "reverse")
# grid.rect()
# grid.text('side = "bottom", directio = "reverse"')
# grid.draw(gb)
# grid.rect(x = unit(0, "npc"), width = grobWidth(gb), just = "right")
# popViewport()
#
annotation_axis_grob = function(at = NULL, labels = at, labels_rot = 0, gp = gpar(),
side = "left", facing = "outside") {
side = "left", facing = "outside", direction = "normal") {

if(!side %in% c("left", "right", "top", "bottom")) {
stop_wrap("`side` can only be in 'left', 'right', 'top' and 'bottom'.")
}
if(!facing %in% c("inside", "outside")) {
stop_wrap("`facing` can only be in 'inside' and 'outside'.")
}
if(!direction %in% c("normal", "reverse")) {
stop_wrap("`direction` can only be in `normal` and `reverse`.")
}

if(is.null(at)) {
if(side %in% c("top", "bottom")) {
scale = current.viewport()$xscale
} else {
scale = current.viewport()$yscale
}
if(is.null(at)) {
at = pretty_breaks(scale)
labels = at
}
if(is.null(labels)) {
labels = at
}

if(direction == "reverse") {
at = scale[2] - at + scale[1]
}

if(side == "left" && facing == "inside") {
gl = gList(
linesGrob(unit(c(0, 0), "npc"), unit(c(0, 1), "npc"), gp = gp),
Expand Down Expand Up @@ -271,6 +310,7 @@ pretty_breaks = function(x) {
# -gp Graphic parameters.
# -side side of the axis of the annotation viewport.
# -facing Facing of the axis.
# -direction direction of the axis. Value should be "normal" or "reverse".
#
# == details
# It uses `annotation_axis_grob` to construct the grob object, then use `grid::grid.draw`
Expand All @@ -281,8 +321,8 @@ pretty_breaks = function(x) {
# NULL
#
grid.annotation_axis = function(at = NULL, labels = at, labels_rot = 0, gp = gpar(),
side = "left", facing = "outside") {
side = "left", facing = "outside", direction = "normal") {
grob = annotation_axis_grob(at = at, labels = labels, labels_rot = labels_rot, gp = gp,
side = side, facing = facing)
side = side, facing = facing, direction = direction)
grid.draw(grob)
}
103 changes: 71 additions & 32 deletions tests_not_run/test-annotation_axis.R
Original file line number Diff line number Diff line change
@@ -1,77 +1,116 @@

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0, side = "left", facing = "outside")
gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
side = "left", facing = "outside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "left", facing = "outside"')
grid.draw(gb)
grid.rect(x = unit(0, "npc"), width = grobWidth(gb), just = "right")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0, side = "left", facing = "inside")
gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
side = "left", facing = "inside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "left", facing = "inside"')
grid.draw(gb)
grid.rect(x = unit(0, "npc"), width = grobWidth(gb), just = "left")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0, side = "right", facing = "outside")
gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
side = "right", facing = "outside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "right", facing = "outside"')
grid.draw(gb)
grid.rect(x = unit(1, "npc"), width = grobWidth(gb), just = "left")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0, side = "right", facing = "inside")
gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0,
side = "right", facing = "inside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "right", facing = "inside"')
grid.draw(gb)
grid.rect(x = unit(1, "npc"), width = grobWidth(gb), just = "right")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0, side = "top", facing = "outside")
gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 0,
side = "top", facing = "outside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "top", facing = "outside"')
grid.draw(gb)
grid.rect(y = unit(1, "npc"), height = grobHeight(gb), just = "bottom")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 90, side = "top", facing = "outside")
gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 90,
side = "top", facing = "outside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "top", facing = "outside"')
grid.draw(gb)
grid.rect(y = unit(1, "npc"), height = grobHeight(gb), just = "bottom")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 45, side = "top", facing = "outside")
gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 45,
side = "top", facing = "outside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "top", facing = "outside"')
grid.draw(gb)
grid.rect(y = unit(1, "npc"), height = grobHeight(gb), just = "bottom")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0, side = "top", facing = "inside")
gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 0,
side = "top", facing = "inside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "top", facing = "inside"')
grid.draw(gb)
grid.rect(y = unit(1, "npc"), height = grobHeight(gb), just = "top")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0, side = "bottom", facing = "outside")
gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 0,
side = "bottom", facing = "outside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "bottom", facing = "outside"')
grid.draw(gb)
grid.rect(y = unit(0, "npc"), height = grobHeight(gb), just = "top")
popViewport()

gb = annotation_axis_grob(at = 1:5, labels = month.name[1:5], labels_rot = 0, side = "bottom", facing = "inside")
gb = annotation_axis_grob(at = 1:3, labels = month.name[1:3], labels_rot = 0,
side = "bottom", facing = "inside")
grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
grid.rect()
grid.text('side = "bottom", facing = "inside"')
grid.draw(gb)
grid.rect(y = unit(0, "npc"), height = grobHeight(gb), just = "bottom")
popViewport()

grid.newpage()
pushViewport(viewport(xscale = c(0, 6), yscale = c(0, 6), width = 0.6, height = 0.6))
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
gb = annotation_axis_grob(labels_rot = 0, side = "left", facing = "outside")
grid.rect()
grid.text('side = "left", facing = "outside"')
grid.draw(gb)
grid.rect(x = unit(0, "npc"), width = grobWidth(gb), just = "right")
popViewport()

grid.newpage()
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
gb = annotation_axis_grob(side = "left", direction = "reverse")
grid.rect()
grid.text('side = "left", direction = "reverse')
grid.draw(gb)
popViewport()

grid.newpage()
pushViewport(viewport(xscale = c(0, 4), yscale = c(0, 6), width = 0.6, height = 0.6))
gb = annotation_axis_grob(side = "bottom", direction = "reverse")
grid.rect()
grid.text('side = "bottom", directio = "reverse"')
grid.draw(gb)
popViewport()


0 comments on commit 278d633

Please sign in to comment.