├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── NAMESPACE ├── R ├── hexdf.R └── theme.R ├── README.Rmd ├── README.md ├── hexagon.Rproj ├── man └── figures │ └── logo.png └── tools └── readme ├── dots-sticker.png ├── plot1.png └── plot2.png /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rhistory 2 | .RData 3 | .Rproj.user 4 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: hexagon 2 | Version: 0.1.1 3 | Title: Hexagon Shaped Data Frames 4 | Authors@R: person("Michael Wayne", "Kearney", , 5 | "kearneymw@missouri.edu", role = c("aut", "cre"), 6 | comment = c(ORCID = "0000-0002-0730-4694")) 7 | Description: Returns hexagon shaped data frames with xy coordinates. 8 | License: MIT + file LICENSE 9 | URL: https://github.com/mkearney/hexagon 10 | BugReports: https://github.com/mkearney/hexagon/issues 11 | Depends: 12 | R (>= 3.1.0) 13 | Imports: 14 | shape 15 | Suggests: 16 | knitr, 17 | roxygen2, 18 | testthat 19 | Encoding: UTF-8 20 | LazyData: yes 21 | Roxygen: list(markdown = TRUE) 22 | RoxygenNote: 6.0.1.9000 23 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: fake comment so roxygen2 overwrites silently. 2 | exportPattern("^[^\\.]") 3 | -------------------------------------------------------------------------------- /R/hexdf.R: -------------------------------------------------------------------------------- 1 | #' Generate a hexagon shaped data frame 2 | #' 3 | #' Returns xy coordinates for hexagon shape using hex sticker dimensions 4 | #' 5 | #' @param size The size of hexagon (one half of the width). 6 | #' @param mid The xy midpoint, defaulting to c(0, 0) 7 | #' @param angle The angle at which to rotate the hexagon. The default, 8 | #' \code{angle = 90}, produces a hexagon with parallel sides and points at the 9 | #' top and bottom. To produce a hexagon with a parallel top and bottom and 10 | #' points at the sides, set \code{angle = 0} or \code{angle = 180} 11 | #' @return A tibble data frame 12 | #' @examples 13 | #' ## create hexagon xy data frame using argument defaults 14 | #' hex1 <- hexdf() 15 | #' 16 | #' ## plot outline of hexagon 17 | #' with(hex1, plot(x, y, type = "l")) 18 | #' 19 | #' ## create larger hexagon to the lower right with a different midpoint 20 | #' hex2 <- hexdf(2, mid = c(2.5, -2)) 21 | #' 22 | #' ## load ggplot2 23 | #' library(ggplot2) 24 | #' 25 | #' ## plot both hex1 and hex2 objects using ggplot2 26 | #' ggplot(hex1, aes(x, y)) + 27 | #' geom_polygon(fill = "blue") + 28 | #' geom_polygon(data = hex2, fill = "red") 29 | #' 30 | #' ## create the hexagon pkg sticker 31 | #' 32 | #' ## generate some data to plot 33 | #' n <- 43 34 | #' den <- ceiling(n / 5) 35 | #' x <- seq(-.525, .465, length.out = n) + rep(c(-.005, .005), ceiling(n/2))[1:n] 36 | #' y <- seq(-.1, .1, length.out = ceiling(n / den)) 37 | #' y <- rep(c(y, rev(y)), ceiling(n / den))[1:n] + seq(.0, .45, length.out = n) 38 | #' z <- c(rep("a", ceiling(n / den)), rep("b", ceiling(n / den))) 39 | #' z <- rep(z, ceiling(n / den))[1:n] 40 | #' pts <- data.frame(x, y, z) 41 | #' 42 | #' ## create base plot 43 | #' p_ <- ggplot(hex1, aes(x, y)) + 44 | #' geom_polygon(fill = "#114466", colour = "#001030", size = 1) + 45 | #' geom_point(data = pts, aes(fill = z, colour = z), shape = 21, size = 1.75) + 46 | #' scale_fill_manual(values = c("#8CFF00", "#cc00ff")) + 47 | #' scale_colour_manual(values = c("#0A2200", "#110033")) + 48 | #' annotate("text", 0, -.36, label = "hexagon", 49 | #' colour = "white", size = 8, fontface = "bold") + 50 | #' coord_fixed(ratio = 1, expand = TRUE) + 51 | #' coord_cartesian(xlim = range(hex1$x), ylim = range(hex1$y)) + 52 | #' theme_void() 53 | #' 54 | #' ## adjust margin to maximize sticker 55 | #' t <- 6 56 | #' r <- 5 57 | #' b <- 9 58 | #' l <- 8 59 | #' p <- p_ + 60 | #' theme(legend.position = "none", 61 | #' plot.margin = margin(-t, -r, -b, -l, unit = "pt")) 62 | #' 63 | #' ##' view plot in device 64 | #' p 65 | #' 66 | #' ## save plot 67 | #' ggsave("hexagon-logo.png", p, width = 1.73, height = 2, 68 | #' units = "in", bg = "transparent") 69 | #' @export 70 | hexdf <- function(size = 1, mid = c(0, 0), angle = 90) { 71 | width <- size * 1 72 | height <- size * 0.865 73 | xy <- shape:::getellipse(width, height, mid = mid, dr = 2 * pi/6) 74 | tibble::as_tibble(structure(as.data.frame(shape::rotatexy(xy, angle = angle, mid = mid)), 75 | class = "data.frame", names = c("x", "y")), validate = FALSE) 76 | } 77 | -------------------------------------------------------------------------------- /R/theme.R: -------------------------------------------------------------------------------- 1 | 2 | #' theme_hexagon 3 | #' 4 | #' A hex sticker friendly ggplot2 theme 5 | #' 6 | #' @param base_size Base font size, defaults to 11 7 | #' @param base_family Font family. 8 | #' @param dark Primary forefront color. 9 | #' @param light Primary background color. 10 | #' @param gray Gray color. 11 | #' @return My ggplot2 theme (similar to theme_minimal/theme_bw) 12 | #' @export 13 | theme_hexagon <- function(hexdata) { 14 | if (!requireNamespace("ggplot2", quietly = FALSE)) { 15 | stop("must install ggplot2 pkg", call. = FALSE) 16 | } 17 | t <- 6 18 | r <- 5 19 | b <- 9 20 | l <- 8 21 | ggplot2::theme_void() + 22 | # ggplot2::coord_fixed(ratio = 1, expand = TRUE) + 23 | # ggplot2::coord_cartesian(xlim = range(hexdata$x), ylim = range(hexdata)) + 24 | ggplot2::theme(legend.position = "none", 25 | plot.margin = ggplot2::margin(-t, -r, -b, -l, unit = "pt")) 26 | } 27 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, echo = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "README-" 12 | ) 13 | ``` 14 | 15 | # hexagon 16 | 17 | 23 | [![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental) 24 | 25 | R package for creating hexagon shaped xy data frames. 26 | 27 | ## Installation 28 | 29 | To get the current development version from Github: 30 | 31 | ```{r, eval=FALSE} 32 | ## install devtools package if it's not already 33 | if (!requireNamespace("devtools", quietly = TRUE)) { 34 | install.packages("devtools") 35 | } 36 | 37 | ## install dev version of hexagon from github 38 | devtools::install_github("mkearney/hexagon") 39 | 40 | ## load rtweet package 41 | library(hexagon) 42 | ``` 43 | 44 | ## Usage 45 | 46 | ```{r, eval=FALSE} 47 | ## create hexagon xy data frame using argument defaults 48 | hex1 <- hexdf() 49 | 50 | ## plot outline of hexagon 51 | with(hex1, plot(x, y, type = "l")) 52 | ``` 53 | 54 |

120 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # hexagon 5 | 6 | 12 | 13 | [![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental) 14 | 15 | R package for creating hexagon shaped xy data frames. 16 | 17 | ## Installation 18 | 19 | To get the current development version from Github: 20 | 21 | ``` r 22 | ## install devtools package if it's not already 23 | if (!requireNamespace("devtools", quietly = TRUE)) { 24 | install.packages("devtools") 25 | } 26 | 27 | ## install dev version of hexagon from github 28 | devtools::install_github("mkearney/hexagon") 29 | 30 | ## load rtweet package 31 | library(hexagon) 32 | ``` 33 | 34 | ## Usage 35 | 36 | ``` r 37 | ## create hexagon xy data frame using argument defaults 38 | hex1 <- hexdf() 39 | 40 | ## plot outline of hexagon 41 | with(hex1, plot(x, y, type = "l")) 42 | ``` 43 | 44 |

45 | 46 | 47 | 48 |

67 | 68 | 69 | 70 |

120 | 121 | 122 | 123 |

124 | -------------------------------------------------------------------------------- /hexagon.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: No 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: XeLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkearney/hexagon/38e3be30899aa0d989a5953137c8201ac52a99bd/man/figures/logo.png -------------------------------------------------------------------------------- /tools/readme/dots-sticker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkearney/hexagon/38e3be30899aa0d989a5953137c8201ac52a99bd/tools/readme/dots-sticker.png -------------------------------------------------------------------------------- /tools/readme/plot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkearney/hexagon/38e3be30899aa0d989a5953137c8201ac52a99bd/tools/readme/plot1.png -------------------------------------------------------------------------------- /tools/readme/plot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkearney/hexagon/38e3be30899aa0d989a5953137c8201ac52a99bd/tools/readme/plot2.png --------------------------------------------------------------------------------