-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from xiangpin/master
add background point layer in grid level to be compatable with the framework of ggsc
- Loading branch information
Showing
15 changed files
with
730 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#' Key drawing functions | ||
#' | ||
#' Each Geom has an associated function that draws the key when the geom needs | ||
#' to be displayed in a legend. These are the options built into ggplot2. | ||
#' | ||
#' @param data A single row data frame containing the scaled aesthetics to | ||
#' display in this key | ||
#' @param params A list of additional parameters supplied to the geom. | ||
#' @param size Width and height of key in mm. | ||
#' @return A grid grob. | ||
#' @name draw_key_scattermore2 | ||
#' @export | ||
#' @importFrom scales alpha | ||
#' @importFrom ggplot2 draw_key_point | ||
#' @importFrom grid grobTree pointsGrob | ||
draw_key_scattermore2 <- function(data, params, size){ | ||
pointkey <- draw_key_point(data, params, size) | ||
if (is.null(data$bg_colour)){ | ||
return (pointkey) | ||
} | ||
|
||
stroke_size <- data$stroke %||% 0.5 | ||
stroke_size[is.na(stroke_size)] <- 0 | ||
|
||
gp <- gpar(col = data$bg_colour, | ||
fontsize = (data$size %||% 1.5) * .pt + stroke_size * .stroke / 2, | ||
lwd = (data$stroke %||% 0.5) * 4) | ||
grobTree(pointkey, pointsGrob(0.5, 0.5, pch = 21, gp = gp)) | ||
} | ||
|
||
.pt <- 2.845276 | ||
.stroke <- 3.779528 | ||
|
||
`%||%` <- function (a, b){ | ||
if (!is.null(a)) | ||
a | ||
else b | ||
} | ||
|
||
rd_aesthetics <- getFromNamespace("rd_aesthetics", "ggplot2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
##' @importFrom grid unit | ||
##' @importFrom grid gpar | ||
##' @importFrom grid gList | ||
##' @importFrom grid gTree | ||
##' @importFrom grid is.unit | ||
##' @importFrom grid rasterGrob | ||
crasterGrob <- function(image, bg.image = NULL, gap.image = NULL, | ||
x = unit(0.5, "npc"), y = unit(0.5, "npc"), | ||
width = NULL, height = NULL, just = "centre", | ||
hjust = NULL, vjust = NULL, interpolate = TRUE, | ||
default.units = "npc", name = NULL, gp = gpar(), | ||
vp = NULL){ | ||
|
||
upperGrob <- rasterGrob(image, x = x, y = y, width = width, height = height, | ||
just = just, hjust = hjust, vjust =vjust, interpolate = interpolate, | ||
default.units = default.units, name = name, gp = gp, vp = vp) | ||
|
||
if (is.null(bg.image)){ | ||
return(upperGrob) | ||
} | ||
|
||
bgGrob <- rasterGrob(bg.image, x = x, y = y, width = width, height = height, | ||
just = just, hjust = hjust, vjust =vjust, | ||
interpolate = interpolate, default.units = default.units, | ||
name = name, gp = gp, vp = vp) | ||
|
||
gapGrob <- rasterGrob(gap.image, x = x, y = y, width = width, height = height, | ||
just = just, hjust = hjust, vjust =vjust, | ||
interpolate = interpolate, default.units = default.units, | ||
name = name, gp = gp, vp = vp) | ||
|
||
grobs <- gList(bgGrob, gapGrob, upperGrob) | ||
gTree(children = grobs) | ||
} | ||
|
||
grid.craster <- function (image, bg.image, gap.image, x = unit(0.5, "npc"), | ||
y = unit(0.5, "npc"), width = NULL, | ||
height = NULL, just = "centre", hjust = NULL, | ||
vjust = NULL, interpolate = TRUE, default.units = "npc", | ||
name = NULL, gp = gpar(), vp = NULL){ | ||
|
||
rg <- crasterGrob(image, bg.image, gap.image, x = x, y = y, | ||
width = width, height = height, just = just, | ||
hjust = hjust, vjust = vjust, interpolate = interpolate, | ||
default.units = default.units, name = name, gp = gp, | ||
vp = vp) | ||
|
||
grid.draw(rg) | ||
} | ||
|
||
|
||
##' @importFrom grid pointsGrob | ||
cpointsGrob <- function(x = stats::runif(10), y = stats::runif(10), pch = 1, | ||
size = unit(1, "char"), bg_line_width = .3, gap_line_width = .1, | ||
bg_colour = "black", gap.colour = 'white', default.units = "native", | ||
name = NULL, gp = gpar(), vp = NULL){ | ||
|
||
upperPointGrob <- pointsGrob(x = x, y = y, pch = pch, size = size, | ||
default.units = default.units, name = name, | ||
gp = gp, vp = vp) | ||
|
||
if (is.null(bg_colour)){ | ||
return(upperPointGrob) | ||
} | ||
|
||
gp.bg <- gp | ||
gp.gap <- gp | ||
|
||
gp.bg$col <- bg_colour | ||
gp.gap$col <- gap.colour | ||
|
||
gp$fontsize | ||
|
||
tmpsize <- sqrt(gp$fontsize) | ||
gp.gap$fontsize <- (tmpsize + tmpsize * gap_line_width * 2)^2 | ||
gp.bg$fontsize <- gp.gap$fontsize + (sqrt(bg_line_width) + tmpsize * bg_line_width * 2) ^2 | ||
|
||
gapPointGrob <- pointsGrob(x = x, y = y, pch = pch, size = size, | ||
default.units = default.units, name = name, | ||
gp = gp.gap, vp = vp) | ||
|
||
bgPointGrob <- pointsGrob(x = x, y = y, pch = pch, size = size, | ||
default.units = default.units, name = name, | ||
gp = gp.bg, vp = vp) | ||
|
||
grobs <- gList(bgPointGrob, gapPointGrob, upperPointGrob) | ||
gTree(children = grobs) | ||
} | ||
|
||
##' @importFrom grid grid.draw | ||
grid.cpoints <- function(x = stats::runif(10), y = stats::runif(10), pch = 1, | ||
size = unit(1, "char"), bg_line_width = .3, gap_line_width = .1, | ||
bg_colour = "black", gap.colour = 'white', default.units = "native", | ||
name = NULL, gp = gpar(), draw = TRUE, vp = NULL){ | ||
pg <- cpointsGrob(x = x, y = y, pch = pch, size = size, bg_line_width = bg_line_width, | ||
gap_line_width = gap_line_width, bg_colour = bg_colour, | ||
gap.colour = gap.colour, default.units = default.units, name = name, | ||
gp = gp, vp = vp) | ||
if (draw) grid.draw(pg) | ||
invisible(pg) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.