├── .Rbuildignore
├── .gitignore
├── DESCRIPTION
├── Makefile
├── NAMESPACE
├── NEWS.md
├── R
├── element_shadowtext.R
├── geom-shadowtext.R
├── geom_interactive.R
├── shadowtext-grob.R
└── shadowtext-package.R
├── README.md
├── ggshadow.Rproj
├── man
├── GeomInteractiveShadowtext.Rd
├── element_shadowtext.Rd
├── geom_shadowtext.Rd
├── geom_shadowtext_interactive.Rd
├── ggproto-shadowtext.Rd
├── reexports.Rd
├── shadowtext-package.Rd
└── shadowtextGrob.Rd
└── vignettes
└── shadowtext.Rmd
/.Rbuildignore:
--------------------------------------------------------------------------------
1 | ^.*\.Rproj$
2 | ^\.Rproj\.user$
3 | Makefile
4 | README.Rmd
5 | README.md
6 | Figs
7 | ^CRAN-SUBMISSION$
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .Rproj.user
2 | .Rhistory
3 | .RData
4 | .DS_Store
5 | CRAN-SUBMISSION
6 |
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: shadowtext
2 | Title: Shadow Text Grob and Layer
3 | Version: 0.1.6
4 | Authors@R: c(
5 | person("Guangchuang", "Yu", email = "guangchuangyu@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0002-6485-8781")),
6 | person("Shuangbin", "Xu", email = "xshuangbin@163.com", role = "ctb", comment = c(ORCID="0000-0003-3513-5362"))
7 | )
8 | Description: Implement shadowtextGrob() for 'grid' and geom_shadowtext() layer for 'ggplot2'.
9 | These functions create/draw text grob with background shadow.
10 | Depends: R (>= 3.4.0)
11 | Imports:
12 | ggiraph,
13 | ggplot2,
14 | grid,
15 | scales,
16 | S7,
17 | purrr,
18 | rlang
19 | Suggests:
20 | knitr,
21 | rmarkdown,
22 | prettydoc
23 | VignetteBuilder: knitr
24 | License: Artistic-2.0
25 | Encoding: UTF-8
26 | URL: https://github.com/GuangchuangYu/shadowtext/
27 | BugReports: https://github.com/GuangchuangYu/shadowtext/issues
28 | RoxygenNote: 7.3.2
29 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | PKGNAME := $(shell sed -n "s/Package: *\([^ ]*\)/\1/p" DESCRIPTION)
2 | PKGVERS := $(shell sed -n "s/Version: *\([^ ]*\)/\1/p" DESCRIPTION)
3 | PKGSRC := $(shell basename `pwd`)
4 |
5 | all: rd check clean
6 |
7 | alldocs: rd
8 |
9 | rd:
10 | Rscript -e 'roxygen2::roxygenise(".")'
11 |
12 | readme:
13 | Rscript -e 'rmarkdown::render("README.Rmd")'
14 |
15 | build:
16 | Rscript -e 'devtools::build()'
17 |
18 | build2:
19 | cd ..;\
20 | R CMD build --no-build-vignettes $(PKGSRC)
21 |
22 | build3:
23 | cd ..;\
24 | R CMD build $(PKGSRC)
25 |
26 |
27 | install:
28 | cd ..;\
29 | R CMD INSTALL $(PKGNAME)_$(PKGVERS).tar.gz
30 |
31 | check:
32 | Rscript -e 'devtools::check()'
33 |
34 | check2: build
35 | cd ..;\
36 | R CMD check $(PKGNAME)_$(PKGVERS).tar.gz
37 |
38 | check3: build
39 | cd ..;\
40 | Rscript -e 'rcmdcheck::rcmdcheck("$(PKGNAME)_$(PKGVERS).tar.gz", args="--as-cran")'
41 |
42 | clean:
43 | cd ..;\
44 | $(RM) -r $(PKGNAME).Rcheck/
45 |
46 |
47 |
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | S3method(element_grob,element_shadowtext)
4 | export(GeomInteractiveShadowtext)
5 | export(GeomShadowText)
6 | export(GeomShadowtext)
7 | export(element_shadowtext)
8 | export(geom_shadowtext)
9 | export(geom_shadowtext_interactive)
10 | export(gpar)
11 | export(grid.shadowtext)
12 | export(shadowtextGrob)
13 | import(ggiraph)
14 | importFrom(S7,S7_inherits)
15 | importFrom(S7,props)
16 | importFrom(ggplot2,.pt)
17 | importFrom(ggplot2,Geom)
18 | importFrom(ggplot2,aes)
19 | importFrom(ggplot2,draw_key_text)
20 | importFrom(ggplot2,element_grob)
21 | importFrom(ggplot2,ggproto)
22 | importFrom(ggplot2,layer)
23 | importFrom(ggplot2,position_nudge)
24 | importFrom(grid,gList)
25 | importFrom(grid,gTree)
26 | importFrom(grid,gpar)
27 | importFrom(grid,grid.draw)
28 | importFrom(grid,grid.layout)
29 | importFrom(grid,is.unit)
30 | importFrom(grid,pointsGrob)
31 | importFrom(grid,rectGrob)
32 | importFrom(grid,textGrob)
33 | importFrom(grid,unit)
34 | importFrom(grid,unit.c)
35 | importFrom(grid,viewport)
36 | importFrom(grid,vpList)
37 | importFrom(grid,vpTree)
38 | importFrom(purrr,detect_index)
39 | importFrom(rlang,check_installed)
40 | importFrom(scales,alpha)
41 |
--------------------------------------------------------------------------------
/NEWS.md:
--------------------------------------------------------------------------------
1 | # shadowtext 0.1.6
2 |
3 | + add `geom_shadowtext_interactive()` (2025-08-24, Sun, #18, #19)
4 |
5 | # shadowtext 0.1.5
6 |
7 | + consistency with upcoming ggplot2 version (4.0.0) (2025-06-30, Mon)
8 |
9 | # shadowtext 0.1.4
10 |
11 | + add `bg.colour` and `bg.r` arguments in `element_shadowtext()` (2024-07-18, Thu)
12 |
13 | # shadowtext 0.1.3
14 |
15 | + inline `add_margins()` to compatible with 'ggplot2' 3.5.0 (2024-01-19, Fri, #13)
16 |
17 | # shadowtext 0.1.2
18 |
19 | + support `aes(subset)` to compatible with `ggtree::geom_tiplab()` (2022-01-10, Mon)
20 |
21 | # shadowtext 0.1.0
22 |
23 | + export `Shadowtext` geom, so that users can use `annotate("shadowtext")` (2021-12-20, Mon, #11)
24 |
25 | # shadowtext 0.0.9
26 |
27 | + export `ShadowText` Geom, so that users can use e.g., `update_geom_defaults("ShadowText", list(size=28))` (2021-09-19, Sun, @bbcuffer, #9)
28 |
29 | # shadowtext 0.0.8
30 |
31 | + use `X` to determine the strheight to fix varying shifts (2019-11-05, @rcannood, #6)
32 |
33 | # shadowtext 0.0.7
34 |
35 | + `element_shadowtext`, so that we can use shadowtext in axis and other labels by using `theme()` (2019-09-09)
36 |
37 | # shadowtext 0.0.6
38 |
39 | + import `ggplot2::.pt` (2019-05-30)
40 |
41 | # shadowtext 0.0.5
42 |
43 | + import `scale::alpha` (2019-05-29)
44 |
45 | # shadowtext 0.0.4
46 |
47 | + update docs (2018-12-07)
48 |
49 | # shadowtext 0.0.3
50 |
51 | + compatible with `ggplot2` 3.0.1 (2018-09-29)
52 |
53 | # shadowtext 0.0.2
54 |
55 | + add vignette (2017-10-30)
56 |
57 | # shadowtext 0.0.1
58 |
59 | + `shadowtextGrob` and `grid.shadowtext` (2017-10-30)
60 | + `geom_shadowtext` (2017-10-27)
61 |
--------------------------------------------------------------------------------
/R/element_shadowtext.R:
--------------------------------------------------------------------------------
1 |
2 | ##' shadown text element for ggplot theme system
3 | ##'
4 | ##'
5 | ##' @title element_shadowtext
6 | ##' @param family Font family
7 | ##' @param face Font face ("plain", "italic", "bold", "bold.italic")
8 | ##' @param colour text colour
9 | ##' @param size text size in pts
10 | ##' @param hjust horizontal justification (in [0, 1])
11 | ##' @param vjust vertical justification (in [0, 1])
12 | ##' @param angle text angle
13 | ##' @param lineheight line height
14 | ##' @param color aliase to colour
15 | ##' @param margin margins around the text, see also 'margin()' for more details
16 | ##' @param debug if 'TRUE', aids visual debugging by drawing a solic rectangle behind the complete text area, and a point where each label is anchored.
17 | ##' @param bg.colour the background colour of text, default is \code{black}.
18 | ##' @param bg.color the alias of bg.colour,
19 | ##' @param bg.r background ratio of shadow text.
20 | ##' @param inherit.blank whether inherit 'element_blank'
21 | ##' @return element_shadowtext object
22 | ##' @export
23 | ##' @author Guangchuang Yu and xmarti6@github
24 | element_shadowtext <- function (family = NULL, face = NULL, colour = NULL, size = NULL,
25 | hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL,
26 | color = NULL, margin = NULL, debug = NULL, bg.colour = 'black',
27 | bg.color = NULL, bg.r = .1, inherit.blank = FALSE) {
28 | if (!is.null(color))
29 | colour <- color
30 | if (!is.null(bg.color))
31 | bg.colour <- bg.color
32 | ## Create a new "subclass" from "element_text"
33 | structure(list(family = family, face = face, colour = colour,
34 | size = size, hjust = hjust, vjust = vjust, angle = angle,
35 | lineheight = lineheight, margin = margin, debug = debug,
36 | bg.colour = bg.colour, bg.r = bg.r, inherit.blank = inherit.blank),
37 | class = c("element_shadowtext", "element_text", "element"))
38 | }
39 |
40 | ##' @importFrom ggplot2 element_grob
41 | ##' @method element_grob element_shadowtext
42 | ##' @export
43 | element_grob.element_shadowtext <- function (element, label = "", x = NULL, y = NULL, family = NULL,
44 | face = NULL, colour = NULL, size = NULL, hjust = NULL, vjust = NULL,
45 | angle = NULL, lineheight = NULL, margin = NULL, margin_x = FALSE,
46 | margin_y = FALSE, ...) {
47 | if (is.null(label))
48 | return(zeroGrob())
49 | vj <- vjust %||% element$vjust
50 | hj <- hjust %||% element$hjust
51 | margin <- margin %||% element$margin
52 | angle <- angle %||% element$angle %||% 0
53 | gp <- gpar(fontsize = size, col = colour, fontfamily = family,
54 | fontface = face, lineheight = lineheight)
55 | element_gp <- gpar(fontsize = element$size, col = element$colour,
56 | fontfamily = element$family, fontface = element$face,
57 | lineheight = element$lineheight)
58 | shadow.titleGrob(label, x, y, hjust = hj, vjust = vj, angle = angle,
59 | gp = modify_list(element_gp, gp),
60 | bg.colour = element$bg.colour, bg.r = element$bg.r,
61 | margin = margin, margin_x = margin_x,
62 | margin_y = margin_y, debug = element$debug)
63 | }
64 |
65 | ##' @importFrom grid rectGrob viewport grid.layout
66 | ##' @importFrom grid pointsGrob unit.c gTree vpTree vpList
67 | shadow.titleGrob <- function (label, x, y, hjust, vjust, angle = 0, gp = gpar(), bg.colour, bg.r,
68 | margin = NULL, margin_x = FALSE, margin_y = FALSE, debug = FALSE) {
69 | if (is.null(label))
70 | return(zeroGrob())
71 | grob_details <- shadow.title_spec(label, x = x, y = y, hjust = hjust,
72 | vjust = vjust, angle = angle, gp = gp,
73 | bg.colour = bg.colour, bg.r = bg.r,
74 | debug = debug)
75 | add_margins(grob = grob_details$text_grob, height = grob_details$text_height,
76 | width = grob_details$text_width, gp = gp, margin = margin,
77 | margin_x = margin_x, margin_y = margin_y)
78 | }
79 |
80 |
81 | #' @importFrom S7 S7_inherits props
82 | shadow.title_spec <- function (label, x, y, hjust, vjust, angle, gp = gpar(),
83 | bg.colour, bg.r, debug = FALSE) {
84 | if (is.null(label))
85 | return(zeroGrob())
86 | just <- rotate_just(angle, hjust, vjust)
87 | n <- max(length(x), length(y), 1)
88 | x <- x %||% unit(rep(just$hjust, n), "npc")
89 | y <- y %||% unit(rep(just$vjust, n), "npc")
90 | #text_grob <- textGrob(label, x, y, hjust = hjust, vjust = vjust, #<<<",
16 | #' "gear: ",
17 | #' mtcars$gear,
18 | #' "
",
19 | #' "carb: ",
20 | #' mtcars$carb
21 | #' )
22 | #' p <- ggplot(
23 | #' mtcars,
24 | #' aes(
25 | #' x = mpg,
26 | #' y = wt,
27 | #' label = label,
28 | #' color = qsec,
29 | #' tooltip = tooltip,
30 | #' data_id = label
31 | #' )
32 | #' ) +
33 | #' geom_shadowtext_interactive(check_overlap = TRUE) +
34 | #' coord_cartesian(xlim = c(0, 50))
35 | #' girafe(ggobj = p, options = list(opts_hover(css = "fill:#FF4C3B;font-style:italic;")))
36 | geom_shadowtext_interactive <- function(...){
37 | rlang::check_installed('ggiraph', "for `geom_shadowtext_interactive()`.")
38 | layer_interactive(geom_shadowtext, interactive_geom = GeomInteractiveShadowtext,...)
39 | }
40 |
41 |
42 | #' @importFrom purrr detect_index
43 | # the internal functions of ggiraph
44 | #' @import ggiraph
45 | layer_interactive <- getFromNamespace("layer_interactive", "ggiraph")
46 | add_default_interactive_aes <- getFromNamespace("add_default_interactive_aes", "ggiraph")
47 | interactive_geom_parameters <- getFromNamespace("interactive_geom_parameters", "ggiraph")
48 | interactive_geom_draw_key <- getFromNamespace("interactive_geom_draw_key", "ggiraph")
49 | IPAR_NAMES <- getFromNamespace("IPAR_NAMES", "ggiraph")
50 | add_interactive_attrs <- getFromNamespace("add_interactive_attrs", "ggiraph")
51 |
52 |
53 | #' @title ggproto classes for ggiraph
54 | #' @description
55 | #' ggproto classes for ggiraph
56 | #' @format NULL
57 | #' @usage NULL
58 | #' @importFrom ggplot2 ggproto
59 | #' @export
60 | GeomInteractiveShadowtext <- ggproto(
61 | "GeomInteractiveShadowtext",
62 | GeomShadowtext,
63 | default_aes = add_default_interactive_aes(GeomShadowtext),
64 | parameters = interactive_geom_parameters,
65 | draw_key = interactive_geom_draw_key,
66 | draw_panel = function(data, panel_params, coord, ..., .ipar = IPAR_NAMES){
67 | gr <- GeomShadowtext$draw_panel(data, panel_params, coord, ...)
68 | if (!.check_ipar_params(data)){
69 | return(gr)
70 | }
71 | coords <- coord$transform(data, panel_params)
72 | ind <- length(gr$children)
73 | gr$children[[ind]] <- add_interactive_attrs(gr$children[[ind]], coords, ipar=.ipar)
74 | gr
75 | }
76 | )
77 |
78 | .check_ipar_params <- function(x){
79 | any(colnames(x) %in% IPAR_NAMES)
80 | }
81 |
--------------------------------------------------------------------------------
/R/shadowtext-grob.R:
--------------------------------------------------------------------------------
1 | ##' create and draw text
2 | ##'
3 | ##'
4 | ##' @title shadowtextGrob
5 | ##' @param label text labels
6 | ##' @param x x position
7 | ##' @param y y position
8 | ##' @param just The justification of the text, can be 'left', 'right', 'center', 'bottom' and 'top'
9 | ##' @param hjust horizontal adjustment
10 | ##' @param vjust vertical adjustment
11 | ##' @param rot angle to rotate the text
12 | ##' @param check.overlap whether check for and omit overlapping text
13 | ##' @param default.units default unit of x and y
14 | ##' @param name identifier
15 | ##' @param gp gpar object
16 | ##' @param vp viewport object
17 | ##' @param bg.colour background color of shadow text
18 | ##' @param bg.r background ratio of shadow text
19 | ##' @return grob object
20 | ##' @importFrom grid textGrob
21 | ##' @importFrom grid unit
22 | ##' @importFrom grid gpar
23 | ##' @importFrom grid gList
24 | ##' @importFrom grid gTree
25 | ##' @importFrom grid is.unit
26 | ##' @export
27 | ##' @author guangchuang yu
28 | shadowtextGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"),
29 | just = "centre", hjust = NULL, vjust = NULL, rot = 0, check.overlap = FALSE,
30 | default.units = "npc", name = NULL, gp = gpar(col="white"), vp = NULL,
31 | bg.colour = "black", bg.r = 0.1) {
32 |
33 | upperGrob <- textGrob(label = label, x = x, y = y, just = just, hjust = hjust,
34 | vjust = vjust, rot = rot, default.units = default.units,
35 | check.overlap = check.overlap, name = name, gp = gp, vp = vp)
36 |
37 | if (is.null(bg.colour))
38 | return(upperGrob)
39 |
40 |
41 | gp$col <- bg.colour
42 |
43 | theta <- seq(pi/8, 2*pi, length.out=16)
44 | char <- "X"
45 | r <- bg.r[1]
46 |
47 | bgList <- lapply(theta, function(i) {
48 | if (!is.unit(x))
49 | x <- unit(x, default.units)
50 | if (!is.unit(y))
51 | y <- unit(y, default.units)
52 |
53 | x <- x + unit(cos(i) * r, "strheight", data = char)
54 | y <- y + unit(sin(i) * r, "strheight", data = char)
55 | textGrob(label = label, x = x, y = y, just = just, hjust = hjust,
56 | vjust = vjust, rot = rot, default.units = default.units,
57 | check.overlap = check.overlap, name = name, gp = gp, vp = vp)
58 | })
59 |
60 | bgGrob <- do.call(gList, bgList)
61 | grobs <- gList(bgGrob, upperGrob)
62 | gTree(children = grobs)
63 | }
64 |
65 | ##' @rdname shadowtextGrob
66 | ##' @param draw whether draw the grob
67 | ##' @importFrom grid grid.draw
68 | ##' @export
69 | grid.shadowtext <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"),
70 | just = "centre", hjust = NULL, vjust = NULL, rot = 0, check.overlap = FALSE,
71 | default.units = "npc", name = NULL, gp = gpar(col="white"), vp = NULL,
72 | bg.colour = "black", bg.r = 0.1, draw = TRUE) {
73 | stg <- shadowtextGrob(label = label, x = x, y = y, just = just, hjust = hjust,
74 | vjust = vjust, rot = rot, default.units = default.units,
75 | check.overlap = check.overlap, name = name, gp = gp, vp = vp,
76 | bg.colour = bg.colour, bg.r = bg.r)
77 | if (draw) grid.draw(stg)
78 | invisible(stg)
79 | }
80 |
81 | ##' @importFrom grid gpar
82 | ##' @export
83 | grid::gpar
84 |
--------------------------------------------------------------------------------
/R/shadowtext-package.R:
--------------------------------------------------------------------------------
1 | #' @keywords internal
2 | "_PACKAGE"
3 |
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # shadowtext: shadow text grob/layer for grid/ggplot2
2 |
3 |
4 | [](https://cran.r-project.org/package=shadowtext)
5 | 
6 | 
7 | 
8 | [](https://guangchuangyu.github.io/blog_images/biobabble.jpg)
9 | [](https://saythanks.io/to/GuangchuangYu)
10 |
11 |
12 | ## :writing_hand: Authors
13 |
14 | Guangchuang YU
15 |
16 | School of Basic Medical Sciences, Southern Medical University
17 |
18 |
19 | ## :arrow_double_down: Installation
20 |
21 | Get the released version from CRAN:
22 |
23 | ```r
24 | install.packages("shadowtext")
25 | ```
26 |
27 | Or the development version from github:
28 |
29 | ```r
30 | ## install.packages("remotes")
31 | remotes::install_github("GuangchuangYu/shadowtext")
32 | ```
33 |
34 |
35 | ## :book: Vignette
36 |
37 | ```r
38 | ## after installing the package
39 | vignette("shadowtext", package="shadowtext")
40 | ```
41 |
42 | online version:
43 |
44 |
--------------------------------------------------------------------------------
/ggshadow.Rproj:
--------------------------------------------------------------------------------
1 | Version: 1.0
2 |
3 | RestoreWorkspace: No
4 | SaveWorkspace: No
5 | AlwaysSaveHistory: Default
6 |
7 | EnableCodeIndexing: Yes
8 | Encoding: UTF-8
9 |
10 | AutoAppendNewline: Yes
11 | StripTrailingWhitespace: Yes
12 |
13 | BuildType: Package
14 | PackageUseDevtools: Yes
15 | PackageInstallArgs: --no-multiarch --with-keep.source
16 | PackageRoxygenize: rd,collate,namespace
17 |
--------------------------------------------------------------------------------
/man/GeomInteractiveShadowtext.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/geom_interactive.R
3 | \docType{data}
4 | \name{GeomInteractiveShadowtext}
5 | \alias{GeomInteractiveShadowtext}
6 | \title{ggproto classes for ggiraph}
7 | \description{
8 | ggproto classes for ggiraph
9 | }
10 | \keyword{datasets}
11 |
--------------------------------------------------------------------------------
/man/element_shadowtext.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/element_shadowtext.R
3 | \name{element_shadowtext}
4 | \alias{element_shadowtext}
5 | \title{element_shadowtext}
6 | \usage{
7 | element_shadowtext(
8 | family = NULL,
9 | face = NULL,
10 | colour = NULL,
11 | size = NULL,
12 | hjust = NULL,
13 | vjust = NULL,
14 | angle = NULL,
15 | lineheight = NULL,
16 | color = NULL,
17 | margin = NULL,
18 | debug = NULL,
19 | bg.colour = "black",
20 | bg.color = NULL,
21 | bg.r = 0.1,
22 | inherit.blank = FALSE
23 | )
24 | }
25 | \arguments{
26 | \item{family}{Font family}
27 |
28 | \item{face}{Font face ("plain", "italic", "bold", "bold.italic")}
29 |
30 | \item{colour}{text colour}
31 |
32 | \item{size}{text size in pts}
33 |
34 | \item{hjust}{horizontal justification (in [0, 1])}
35 |
36 | \item{vjust}{vertical justification (in [0, 1])}
37 |
38 | \item{angle}{text angle}
39 |
40 | \item{lineheight}{line height}
41 |
42 | \item{color}{aliase to colour}
43 |
44 | \item{margin}{margins around the text, see also 'margin()' for more details}
45 |
46 | \item{debug}{if 'TRUE', aids visual debugging by drawing a solic rectangle behind the complete text area, and a point where each label is anchored.}
47 |
48 | \item{bg.colour}{the background colour of text, default is \code{black}.}
49 |
50 | \item{bg.color}{the alias of bg.colour,}
51 |
52 | \item{bg.r}{background ratio of shadow text.}
53 |
54 | \item{inherit.blank}{whether inherit 'element_blank'}
55 | }
56 | \value{
57 | element_shadowtext object
58 | }
59 | \description{
60 | shadown text element for ggplot theme system
61 | }
62 | \author{
63 | Guangchuang Yu and xmarti6@github
64 | }
65 |
--------------------------------------------------------------------------------
/man/geom_shadowtext.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/geom-shadowtext.R
3 | \name{geom_shadowtext}
4 | \alias{geom_shadowtext}
5 | \title{geom_shadowtext}
6 | \usage{
7 | geom_shadowtext(
8 | mapping = NULL,
9 | data = NULL,
10 | stat = "identity",
11 | position = "identity",
12 | ...,
13 | parse = FALSE,
14 | nudge_x = 0,
15 | nudge_y = 0,
16 | check_overlap = FALSE,
17 | na.rm = FALSE,
18 | show.legend = NA,
19 | inherit.aes = TRUE
20 | )
21 | }
22 | \arguments{
23 | \item{mapping}{aesthetic mapping}
24 |
25 | \item{data}{the data to be displayed}
26 |
27 | \item{stat}{statistical transformation}
28 |
29 | \item{position}{position adjustment}
30 |
31 | \item{...}{additional parameter}
32 |
33 | \item{parse}{whether parse text as expression}
34 |
35 | \item{nudge_x}{horizontal adjustment of text}
36 |
37 | \item{nudge_y}{vertical adjustment of text}
38 |
39 | \item{check_overlap}{whether check overlap}
40 |
41 | \item{na.rm}{whether remove NA values}
42 |
43 | \item{show.legend}{whether show legend}
44 |
45 | \item{inherit.aes}{whether inherit aes from ggplot}
46 | }
47 | \value{
48 | layer
49 | }
50 | \description{
51 | a shadow version of geom_text
52 | }
53 | \examples{
54 | library(ggplot2)
55 | d <- data.frame(x = rnorm(3), y=rnorm(3), label = c('hello', 'world', '!!!'))
56 | ggplot(d, aes(x,y)) + geom_shadowtext(aes(label=label, color=label), bg.colour='firebrick')
57 | }
58 | \author{
59 | guangchuang yu
60 | }
61 |
--------------------------------------------------------------------------------
/man/geom_shadowtext_interactive.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/geom_interactive.R
3 | \name{geom_shadowtext_interactive}
4 | \alias{geom_shadowtext_interactive}
5 | \title{Create interactive shadow text}
6 | \usage{
7 | geom_shadowtext_interactive(...)
8 | }
9 | \arguments{
10 | \item{...}{see also the parameters of `geom_shadowtext()` of `shadowtext`}
11 | }
12 | \description{
13 | The geometry is based on `geom_shadowtext()`.
14 | See the documentation for those functions for more details.
15 | }
16 | \examples{
17 | library(ggplot2)
18 | library(ggiraph)
19 | mtcars$label <- rownames(mtcars)
20 | mtcars$tooltip = paste0(
21 | "cyl: ",
22 | mtcars$cyl,
23 | "
",
24 | "gear: ",
25 | mtcars$gear,
26 | "
",
27 | "carb: ",
28 | mtcars$carb
29 | )
30 | p <- ggplot(
31 | mtcars,
32 | aes(
33 | x = mpg,
34 | y = wt,
35 | label = label,
36 | color = qsec,
37 | tooltip = tooltip,
38 | data_id = label
39 | )
40 | ) +
41 | geom_shadowtext_interactive(check_overlap = TRUE) +
42 | coord_cartesian(xlim = c(0, 50))
43 | girafe(ggobj = p, options = list(opts_hover(css = "fill:#FF4C3B;font-style:italic;")))
44 | }
45 |
--------------------------------------------------------------------------------
/man/ggproto-shadowtext.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/geom-shadowtext.R
3 | \docType{data}
4 | \name{GeomShadowText}
5 | \alias{GeomShadowText}
6 | \alias{GeomShadowtext}
7 | \title{GeomShadowText}
8 | \format{
9 | An object of class \code{GeomShadowText} (inherits from \code{Geom}, \code{ggproto}, \code{gg}) of length 7.
10 | }
11 | \usage{
12 | GeomShadowText
13 | }
14 | \description{
15 | ShadowText Geom
16 | }
17 | \author{
18 | Guangchuang Yu
19 | }
20 | \keyword{datasets}
21 |
--------------------------------------------------------------------------------
/man/reexports.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/shadowtext-grob.R
3 | \docType{import}
4 | \name{reexports}
5 | \alias{reexports}
6 | \alias{gpar}
7 | \title{Objects exported from other packages}
8 | \keyword{internal}
9 | \description{
10 | These objects are imported from other packages. Follow the links
11 | below to see their documentation.
12 |
13 | \describe{
14 | \item{grid}{\code{\link[grid]{gpar}}}
15 | }}
16 |
17 |
--------------------------------------------------------------------------------
/man/shadowtext-package.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/shadowtext-package.R
3 | \docType{package}
4 | \name{shadowtext-package}
5 | \alias{shadowtext}
6 | \alias{shadowtext-package}
7 | \title{shadowtext: Shadow Text Grob and Layer}
8 | \description{
9 | Implement shadowtextGrob() for 'grid' and geom_shadowtext() layer for 'ggplot2'. These functions create/draw text grob with background shadow.
10 | }
11 | \seealso{
12 | Useful links:
13 | \itemize{
14 | \item \url{https://github.com/GuangchuangYu/shadowtext/}
15 | \item Report bugs at \url{https://github.com/GuangchuangYu/shadowtext/issues}
16 | }
17 |
18 | }
19 | \author{
20 | \strong{Maintainer}: Guangchuang Yu \email{guangchuangyu@gmail.com} (\href{https://orcid.org/0000-0002-6485-8781}{ORCID}) [copyright holder]
21 |
22 | Other contributors:
23 | \itemize{
24 | \item Shuangbin Xu \email{xshuangbin@163.com} (\href{https://orcid.org/0000-0003-3513-5362}{ORCID}) [contributor]
25 | }
26 |
27 | }
28 | \keyword{internal}
29 |
--------------------------------------------------------------------------------
/man/shadowtextGrob.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/shadowtext-grob.R
3 | \name{shadowtextGrob}
4 | \alias{shadowtextGrob}
5 | \alias{grid.shadowtext}
6 | \title{shadowtextGrob}
7 | \usage{
8 | shadowtextGrob(
9 | label,
10 | x = unit(0.5, "npc"),
11 | y = unit(0.5, "npc"),
12 | just = "centre",
13 | hjust = NULL,
14 | vjust = NULL,
15 | rot = 0,
16 | check.overlap = FALSE,
17 | default.units = "npc",
18 | name = NULL,
19 | gp = gpar(col = "white"),
20 | vp = NULL,
21 | bg.colour = "black",
22 | bg.r = 0.1
23 | )
24 |
25 | grid.shadowtext(
26 | label,
27 | x = unit(0.5, "npc"),
28 | y = unit(0.5, "npc"),
29 | just = "centre",
30 | hjust = NULL,
31 | vjust = NULL,
32 | rot = 0,
33 | check.overlap = FALSE,
34 | default.units = "npc",
35 | name = NULL,
36 | gp = gpar(col = "white"),
37 | vp = NULL,
38 | bg.colour = "black",
39 | bg.r = 0.1,
40 | draw = TRUE
41 | )
42 | }
43 | \arguments{
44 | \item{label}{text labels}
45 |
46 | \item{x}{x position}
47 |
48 | \item{y}{y position}
49 |
50 | \item{just}{The justification of the text, can be 'left', 'right', 'center', 'bottom' and 'top'}
51 |
52 | \item{hjust}{horizontal adjustment}
53 |
54 | \item{vjust}{vertical adjustment}
55 |
56 | \item{rot}{angle to rotate the text}
57 |
58 | \item{check.overlap}{whether check for and omit overlapping text}
59 |
60 | \item{default.units}{default unit of x and y}
61 |
62 | \item{name}{identifier}
63 |
64 | \item{gp}{gpar object}
65 |
66 | \item{vp}{viewport object}
67 |
68 | \item{bg.colour}{background color of shadow text}
69 |
70 | \item{bg.r}{background ratio of shadow text}
71 |
72 | \item{draw}{whether draw the grob}
73 | }
74 | \value{
75 | grob object
76 | }
77 | \description{
78 | create and draw text
79 | }
80 | \author{
81 | guangchuang yu
82 | }
83 |
--------------------------------------------------------------------------------
/vignettes/shadowtext.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Shadow Text Grob and Layer"
3 | author: "Guangchuang Yu\\
4 |
5 | School of Basic Medical Sciences, Southern Medical University"
6 | date: "`r Sys.Date()`"
7 | output:
8 | prettydoc::html_pretty:
9 | toc: true
10 | theme: cayman
11 | highlight: github
12 | pdf_document:
13 | toc: true
14 | vignette: >
15 | %\VignetteEngine{knitr::rmarkdown}
16 | %\VignetteIndexEntry{shadowtext introduction}
17 | %\VignetteDepends{ggplot2}
18 | %\VignetteDepends{grid}
19 | %\usepackage[utf8]{inputenc}
20 | ---
21 |
22 |
23 |
24 | ```{r include=FALSE}
25 | knitr::opts_chunk$set(warning = FALSE,
26 | message = TRUE)
27 |
28 | library(grid)
29 | library(ggplot2)
30 | library(shadowtext)
31 | ```
32 |
33 |
34 |
35 | ## shadow text grob: `shadowtextGrob()` and `grid.shadowtext()`
36 |
37 |
38 | ```{r}
39 | library(shadowtext)
40 | grid.shadowtext("grid is awesome", gp=gpar(cex=3, col="white"), rot=45)
41 | grid.shadowtext("hello world", y=0.85, gp=gpar(cex=5, col="steelblue"), bg.colour="firebrick")
42 | grid.shadowtext("R you ready!!!", y=.1, gp=gpar(cex=4, col="firebrick"))
43 | ```
44 |
45 |
46 | ## ggplot2 layer: `geom_shadowtext()`
47 |
48 |
49 | ```{r}
50 | library(ggplot2)
51 |
52 | random_text <- function(n=1, length=10) {
53 | d <- data.frame(n=1:n, length=length)
54 | sapply(1:nrow(d), function(i) {
55 | paste(sample(c(0:9, letters, LETTERS),
56 | d$length[i], replace=TRUE),
57 | collapse="")
58 | })
59 | }
60 |
61 | n <- 10
62 | set.seed(2017-10-27)
63 | d <- data.frame(x = rnorm(n), y=rnorm(n),
64 | label = random_text(n),
65 | angle = sample(0:360, 10))
66 | p <- ggplot(d, aes(x, y)) + xlim(-2, 2.2) + ylim(-2, 2.4)
67 |
68 | p + geom_shadowtext(aes(label=label, angle=angle), size=5)
69 |
70 | p + geom_shadowtext(aes(label=label, angle=angle, color=label),
71 | bg.colour='firebrick', size=5) +
72 | theme(legend.position="none")
73 | ```
74 |
75 | axis text is also supported:
76 |
77 | ```{r}
78 | x <- c("a","b","c","d","e","f")
79 | y <- c("10", "9","-10","11","-3","-15")
80 | data <- data.frame(x, y)
81 | data$y <- as.numeric(as.character(data$y))
82 |
83 | data$category <- ifelse(as.numeric(data$y)<0, 0, 1)
84 | data$category <- as.factor(data$category)
85 |
86 | g <- ggplot(data, aes(x = x, y = y)) +
87 | geom_bar(stat = "identity", aes(fill = category))
88 |
89 | g + theme(axis.text.x = element_shadowtext(
90 | colour = "white",
91 | size = 12,face = "bold"))
92 | ```
93 |
--------------------------------------------------------------------------------