-
Notifications
You must be signed in to change notification settings - Fork 0
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 #3 from mikeasilva/gcplot
Initial PR of gcplot
- Loading branch information
Showing
7 changed files
with
181 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,6 @@ Suggests: | |
testthat (>= 3.0.0) | ||
Config/testthat/edition: 3 | ||
Imports: | ||
ggplot2 | ||
grid, | ||
ggplot2, | ||
gridExtra |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(gcplot) | ||
export(theme_gc) | ||
export(unit_chart) | ||
import(ggplot2) | ||
import(grid) | ||
import(gridExtra) |
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,111 @@ | ||
#' Convert a ggplot into a Good Charts themed plot | ||
#' | ||
#' @param plot A ggplot object | ||
#' @param title (optional) The plot's title | ||
#' @param subtitle (optional) The plot's subtitle | ||
#' @param ylab (optional) The y-axis label | ||
#' @param caption (optional) The caption or source line | ||
#' @examples | ||
#'\dontrun{ | ||
#' library(ggplot2) | ||
#' | ||
#' p1 <- ggplot(iris, aes(Petal.Length, Petal.Width, color = Species)) + | ||
#' geom_point() + | ||
#' labs( | ||
#' title = "A ROSE BY ANY OTHER NAME", | ||
#' subtitle = "IRISES PETAL ATTRIBUTE", | ||
#' caption = "SOURCE: R.A. FISHER", | ||
#' x = "PETAL LENGTH", | ||
#' y = "PETAL WIDTH" | ||
#' ) | ||
#' gcplot(p1) | ||
#'} | ||
#' @export | ||
#' @import grid | ||
#' @import gridExtra | ||
gcplot <- function(plot, | ||
title = NA, | ||
subtitle = NA, | ||
ylab = NA, | ||
caption = NA) { | ||
text_grob_x <- unit(0.008, "npc") | ||
#heights | ||
heights <- c(0, 0, 0, 0.6, 0) | ||
names(heights) <- c("title", "subtitle", "ylab", "plot", "caption") | ||
# Chart Title | ||
if (nchar(plot$labels$title) > 0 & is.na((title))) { | ||
title <- plot$labels$title | ||
} | ||
if (!is.na(title)) { | ||
title <- grid::textGrob( | ||
title, | ||
gp = grid::gpar( | ||
fontsize = 26, | ||
#fontfamily = "PT Sans Narrow", | ||
fontface = "bold" | ||
), | ||
x = text_grob_x, | ||
just = c("left", "center") | ||
) | ||
heights["title"] <- 0.1 | ||
} | ||
# Chart Subtitle | ||
if (nchar(plot$labels$subtitle) > 0 & is.na((subtitle))) { | ||
subtitle <- plot$labels$subtitle | ||
} | ||
if (!is.na(subtitle)) { | ||
subtitle <- grid::textGrob( | ||
subtitle, | ||
gp = grid::gpar( | ||
fontsize = 22, | ||
#fontfamily = "PT Sans Narrow", | ||
fontface = "bold" | ||
), | ||
x = text_grob_x, | ||
just = c("left", "bottom") | ||
) | ||
heights["subtitle"] <- 0.08 | ||
} | ||
# Chart Y-Axis Label | ||
if (nchar(plot$labels$y) > 0 & is.na(ylab)) { | ||
ylab <- plot$labels$y | ||
} | ||
if (!is.na(ylab)) { | ||
ylab <- grid::textGrob( | ||
ylab, | ||
gp = grid::gpar( | ||
fontsize = 15, | ||
#fontfamily = "Proxima Nova", | ||
col = "#000000" | ||
), | ||
x = text_grob_x, | ||
just = c("left", "bottom") | ||
) | ||
heights["ylab"] <- 0.03 | ||
#heights["plot"] <- heights["plot"] - heights["ylab"] | ||
} | ||
# Chart Caption | ||
if (nchar(plot$labels$caption) > 0 & is.na((caption))) { | ||
caption <- plot$labels$caption | ||
} | ||
if (!is.na(caption)) { | ||
caption <- grid::textGrob( | ||
caption, | ||
gp = grid::gpar( | ||
fontsize = 10, | ||
#fontfamily = "Proxima Nova", | ||
col = "#444444", | ||
fontface = "bold" | ||
), | ||
x = text_grob_x, | ||
just = c("left", "bottom") | ||
) | ||
heights["caption"] <- 0.05 | ||
} | ||
# Apply the good charts base theme | ||
plot <- plot + gcplot2::theme_gc() | ||
# 12% title; 8% subtitle; 75% field; 5% source | ||
gridExtra::grid.arrange(title, subtitle, ylab, plot, caption, ncol = 1, heights = heights) | ||
#return(heights) | ||
#return(plot) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
test_that("multiplication works", { | ||
library(ggplot2) | ||
|
||
# p1 <- ggplot(iris, aes(Petal.Length, Petal.Width, color = Species)) + | ||
# geom_point() + | ||
# labs( | ||
# title = "A ROSE BY ANY OTHER NAME", | ||
# subtitle = "IRISES PETAL ATTRIBUTE", | ||
# caption = "SOURCE: R.A. FISHER", | ||
# x = "PETAL LENGTH", | ||
# y = "PETAL WIDTH" | ||
# ) | ||
# gcplot(p1) | ||
expect_equal(2 * 2, 4) | ||
}) |