Skip to content

Commit

Permalink
fixed #72
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangchuangYu committed Dec 5, 2020
1 parent 006beaf commit 1dc4fd7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 80 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: hexSticker
Title: Create Hexagon Sticker in R
Version: 0.4.8
Version: 0.4.9
Authors@R: c(person(given = "Guangchuang", family = "Yu", email = "[email protected]", role = c("aut", "cre")),
person(given = "Laurent", family = "Gatto", email = "[email protected]", role = "ctb"),
person(given = "Johannes", family = "Rainer", email = "[email protected]", role = "ctb"),
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ install:
check:
Rscript -e 'devtools::check()'

# check: build
# cd ..;\
# Rscript -e 'rcmdcheck::rcmdcheck("$(PKGNAME)_$(PKGVERS).tar.gz", args="--as-cran")'
check3: build
cd ..;\
Rscript -e 'rcmdcheck::rcmdcheck("$(PKGNAME)_$(PKGVERS).tar.gz", args="--as-cran")'

check2: build
cd ..;\
Expand Down
45 changes: 34 additions & 11 deletions R/sticker.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
##' @param p_y y position for package name
##' @param p_color color for package name
##' @param p_family font family for package name
##' @param p_fontface fontface for package name
##' @param p_size font size for package name
##' @param h_size size for hexagon border
##' @param h_fill color to fill hexagon
Expand Down Expand Up @@ -55,10 +56,12 @@
##' @author Guangchuang Yu
##' @md
sticker <- function(subplot, s_x=.8, s_y=.75, s_width=.4, s_height=.5,
package, p_x=1, p_y=1.4, p_color="#FFFFFF", p_family="Aller_Rg", p_size=8,
package, p_x=1, p_y=1.4, p_color="#FFFFFF",
p_family="Aller_Rg", p_fontface = "plain", p_size=8,
h_size=1.2, h_fill="#1881C2", h_color="#87B13F",
spotlight=FALSE, l_x=1, l_y=.5, l_width=3, l_height=3, l_alpha=0.4,
url = "", u_x=1, u_y=0.08, u_color="black", u_family="Aller_Rg", u_size=1.5, u_angle=30,
url = "", u_x=1, u_y=0.08, u_color="black",
u_family="Aller_Rg", u_size=1.5, u_angle=30,
white_around_sticker = FALSE, ...,
filename = paste0(package, ".png"), asp=1, dpi = 300) {

Expand All @@ -67,20 +70,35 @@ sticker <- function(subplot, s_x=.8, s_y=.75, s_width=.4, s_height=.5,

if (inherits(subplot, "character")) {
d <- data.frame(x=s_x, y=s_y, image=subplot)
sticker <- hex + geom_image(aes_(x=~x, y=~y, image=~image), d, size=s_width, asp=asp)
sticker <- hex + geom_image(aes_(x=~x, y=~y, image=~image),
d, size=s_width, asp=asp
)
} else {
sticker <- hex + geom_subview(subview=subplot, x=s_x, y=s_y, width=s_width, height=s_height)
sticker <- hex + geom_subview(subview=subplot,
x=s_x, y=s_y,
width=s_width, height=s_height
)
}

sticker <- sticker +
geom_hexagon(size = h_size, fill = NA, color = h_color)

if(spotlight)
sticker <- sticker + geom_subview(subview=spotlight(l_alpha), x=l_x, y=l_y, width=l_width, height=l_height)

sticker <- sticker + geom_pkgname(package, p_x, p_y, p_color, p_family, p_size, ...)

sticker <- sticker + geom_url(url, x=u_x, y = u_y, color = u_color, family = u_family, size=u_size, angle=u_angle)
sticker <- sticker + geom_subview(subview=spotlight(l_alpha),
x=l_x, y=l_y,
width=l_width, height=l_height
)

sticker <- sticker + geom_pkgname(package, p_x, p_y,
color = p_color,
family = p_family,
fontface = p_fontface,
size = p_size,
...)

sticker <- sticker + geom_url(url, x=u_x, y = u_y, color = u_color,
family = u_family, size=u_size, angle=u_angle
)

if (white_around_sticker)
sticker <- sticker + white_around_hex(size = h_size)
Expand Down Expand Up @@ -138,13 +156,16 @@ spotlight <- function(alpha) {
##' @param y y position
##' @param color color
##' @param family font family
##' @param fontface fontface, e.g. 'plain', 'bold', 'italic'
##' @param size font size
##' @param ... addition parameters passed to geom_text()
##' @return package name layer
##' @importFrom ggplot2 geom_text
##' @export
##' @author Guangchuang Yu
geom_pkgname <- function(package, x=1, y=1.4, color="#FFFFFF", family="Aller_Rg", size=8, ...) {
geom_pkgname <- function(package, x=1, y=1.4, color="#FFFFFF",
family="Aller_Rg", fontface = "plain",
size=8, ...) {
family <- load_font(family)
## d <- data.frame(x = x, y = y,
## label = package)
Expand All @@ -154,7 +175,9 @@ geom_pkgname <- function(package, x=1, y=1.4, color="#FFFFFF", family="Aller_Rg"

## https://github.com/GuangchuangYu/hexSticker/issues/105
ggplot2::annotate("text", x = x, y = y, size = size,
label = package, color = color, family = family, ...)
label = package, color = color,
family = family, fontface = fontface,
...)
}

##' @importFrom sysfonts font_add
Expand Down
124 changes: 59 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
<!-- README.md is generated from README.Rmd. Please edit that file -->

# hexSticker: create hexagon sticker in R
hexSticker: create hexagon sticker in R
=======================================

[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/hexSticker?color=green)](https://cran.r-project.org/package=hexSticker)
[![](http://cranlogs.r-pkg.org/badges/grand-total/hexSticker?color=green)](https://cran.r-project.org/package=hexSticker)
[![](http://cranlogs.r-pkg.org/badges/hexSticker?color=green)](https://cran.r-project.org/package=hexSticker)
[![](http://cranlogs.r-pkg.org/badges/last-week/hexSticker?color=green)](https://cran.r-project.org/package=hexSticker)

## :writing\_hand: Author
:writing\_hand: Author
----------------------

Guangchuang YU <https://guangchuangyu.github.io>
Guangchuang YU
<a href="https://guangchuangyu.github.io" class="uri">https://guangchuangyu.github.io</a>

School of Basic Medical Sciences, Southern Medical University

[![saythanks](https://img.shields.io/badge/say-thanks-ff69b4.svg)](https://saythanks.io/to/GuangchuangYu)
[![](https://img.shields.io/badge/follow%20me%20on-WeChat-green.svg)](https://guangchuangyu.github.io/blog_images/biobabble.jpg)

-----
------------------------------------------------------------------------

## :arrow\_double\_down: Installation
:arrow\_double\_down: Installation
----------------------------------

Install the hexSticker package via CRAN:

``` r
install.packages("hexSticker")
```
install.packages("hexSticker")

You can also install the package via the Github repository.

``` r
# install.package("remotes") #In case you have not installed it.
remotes::install_github("GuangchuangYu/hexSticker")
```
# install.package("remotes") #In case you have not installed it.
remotes::install_github("GuangchuangYu/hexSticker")

## Fail to install
Fail to install
---------------

### imageMagick

Expand All @@ -47,25 +48,23 @@ In Mac OS, you may need to re-install `sysfont` to properly load it.

Be sure to install `xquartz` first.

``` r
brew update && brew install homebrew/cask/xquartz
```
brew update && brew install homebrew/cask/xquartz

-----
------------------------------------------------------------------------

## Examples
Examples
--------

> `sticker` function will produce a file with dimension exactly for
> printing according to <http://hexb.in/sticker.html>
> printing according to
> <a href="http://hexb.in/sticker.html" class="uri">http://hexb.in/sticker.html</a>
### base plot

``` r
library(hexSticker)
s <- sticker(~plot(cars, cex=.5, cex.axis=.5, mgp=c(0,.3,0), xlab="", ylab=""),
package="hexSticker", p_size=20, s_x=.8, s_y=.6, s_width=1.4, s_height=1.2,
filename="inst/figures/baseplot.png")
```
library(hexSticker)
s <- sticker(~plot(cars, cex=.5, cex.axis=.5, mgp=c(0,.3,0), xlab="", ylab=""),
package="hexSticker", p_size=20, s_x=.8, s_y=.6, s_width=1.4, s_height=1.2,
filename="inst/figures/baseplot.png")

The `sticker()` will generate a figure specified by the `filename`
parameter. The output of the `sticker()` function is a `ggplot` object,
Expand All @@ -79,63 +78,56 @@ function to preview sticker. Try `plot(s)` :).

### lattice

``` r
library(lattice)
library(lattice)

counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
bwplot <- bwplot(counts ~ outcome | treatment, xlab=NULL, ylab=NULL, cex=.5,
scales=list(cex=.5), par.strip.text=list(cex=.5))
sticker(bwplot, package="hexSticker", p_size=20, s_x=1.05, s_y=.8, s_width=2, s_height=1.5,
h_fill="#f9690e", h_color="#f39c12", filename="inst/figures/lattice.png")
```
counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
bwplot <- bwplot(counts ~ outcome | treatment, xlab=NULL, ylab=NULL, cex=.5,
scales=list(cex=.5), par.strip.text=list(cex=.5))
sticker(bwplot, package="hexSticker", p_size=20, s_x=1.05, s_y=.8, s_width=2, s_height=1.5,
h_fill="#f9690e", h_color="#f39c12", filename="inst/figures/lattice.png")

<img src="inst/figures/lattice.png" height="300"/>

### ggplot2

``` r
library(ggplot2)
library(ggplot2)

p <- ggplot(aes(x = mpg, y = wt), data = mtcars) + geom_point()
p <- p + theme_void() + theme_transparent()
p <- ggplot(aes(x = mpg, y = wt), data = mtcars) + geom_point()
p <- p + theme_void() + theme_transparent()

sticker(p, package="hexSticker", p_size=20, s_x=1, s_y=.75, s_width=1.3, s_height=1,
filename="inst/figures/ggplot2.png")
```
sticker(p, package="hexSticker", p_size=20, s_x=1, s_y=.75, s_width=1.3, s_height=1,
filename="inst/figures/ggplot2.png")

<img src="inst/figures/ggplot2.png" height="300"/>

### image file

``` r
imgurl <- system.file("figures/cat.png", package="hexSticker")
sticker(imgurl, package="hexSticker", p_size=20, s_x=1, s_y=.75, s_width=.6,
filename="inst/figures/imgfile.png")
```
imgurl <- system.file("figures/cat.png", package="hexSticker")
sticker(imgurl, package="hexSticker", p_size=20, s_x=1, s_y=.75, s_width=.6,
filename="inst/figures/imgfile.png")

<img src="inst/figures/imgfile.png" height="300"/>

### Google fonts

``` r
library(showtext)
## Loading Google fonts (http://www.google.com/fonts)
font_add_google("Gochi Hand", "gochi")
## Automatically use showtext to render text for future devices
showtext_auto()
library(showtext)
## Loading Google fonts (http://www.google.com/fonts)
font_add_google("Gochi Hand", "gochi")
## Automatically use showtext to render text for future devices
showtext_auto()

## use the ggplot2 example
sticker(p, package="hexSticker", p_size=22, s_x=1, s_y=.75, s_width=1.3, s_height=1,
p_family = "gochi", filename="inst/figures/ggplot2-google-font.png")
```
## use the ggplot2 example
sticker(p, package="hexSticker", p_size=22, s_x=1, s_y=.75, s_width=1.3, s_height=1,
p_family = "gochi", filename="inst/figures/ggplot2-google-font.png")

<img src="inst/figures/ggplot2-google-font.png" height="300"/>

-----
------------------------------------------------------------------------

## :sparkling\_heart: Stickers produced by `hexSticker`
:sparkling\_heart: Stickers produced by `hexSticker`
----------------------------------------------------

> If you use `hexSticker` and want your sticker to be listed here,
> please feel free to edit
Expand Down Expand Up @@ -253,18 +245,20 @@ sticker(p, package="hexSticker", p_size=22, s_x=1, s_y=.75, s_width=1.3, s_heigh
[<img src="https://user-images.githubusercontent.com/9893806/36942615-8f9b0640-1f2b-11e8-85eb-6d2cabcfd62f.png" height="120"/>](https://github.com/shinycrypto)
[<img src="https://github.com/slc-rug/slcrug-hexsticker/raw/master/R/slcrug_hex.png" height="120"/>](https://github.com/slc-rug/slcrug-hexsticker)

## Print/order stickers
Print/order stickers
--------------------

Sticker designers can make their stickers available via [Sticker
Mule](https://www.stickermule.com/uses/hexagon-stickers).

## Related Tools
Related Tools
-------------

- [badger](https://github.com/GuangchuangYu/badger): Query information
- [badger](https://github.com/GuangchuangYu/badger): Query information
and generate badge for using in README and GitHub Pages.
- [ggimage](https://github.com/GuangchuangYu/ggimage): Supports image
- [ggimage](https://github.com/GuangchuangYu/ggimage): Supports image
files and graphic objects to be visualized in ‘ggplot2’ graphic
system.
- [meme](https://github.com/GuangchuangYu/meme/): Create Meme.
- [shadowtext](https://github.com/GuangchuangYu/shadowtext/): Create
- [meme](https://github.com/GuangchuangYu/meme/): Create Meme.
- [shadowtext](https://github.com/GuangchuangYu/shadowtext/): Create
text grob with background shadow.
3 changes: 3 additions & 0 deletions man/geom_pkgname.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/sticker.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1dc4fd7

Please sign in to comment.