Skip to content

Commit

Permalink
Merge pull request #3 from mikeasilva/gcplot
Browse files Browse the repository at this point in the history
Initial PR of gcplot
  • Loading branch information
mikeasilva authored Mar 24, 2021
2 parents 8dfee79 + 2142a04 commit e64339c
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 9 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Imports:
ggplot2
grid,
ggplot2,
gridExtra
3 changes: 3 additions & 0 deletions NAMESPACE
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)
111 changes: 111 additions & 0 deletions R/gcplot.R
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)
}
15 changes: 9 additions & 6 deletions R/theme_gc.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
#' @import ggplot2
theme_gc <- function(...) {
ggplot2::theme_classic(...) %+replace% theme(
plot.title = ggplot2::element_blank(),
plot.subtitle = ggplot2::element_blank(),
plot.caption = element_blank(),
axis.line.y = ggplot2::element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(margin = margin(20, 0, 0, 0)),
axis.text.y = element_text(margin = margin(0, 15, 0, 0)),
axis.ticks.length = unit(0.3, "cm"),
axis.title.x = element_text(size = 15),
axis.title.y = ggplot2::element_blank(),
axis.text.x = ggplot2::element_text(margin = margin(10, 0, 0, 0)),
axis.text.y = ggplot2::element_text(margin = margin(0, 15, 0, 0)),
axis.ticks.length = ggplot2::unit(0.3, "cm"),
# This is a bit of a hack. This removes the lowest y-axis tick. I
# doubt that there will be this many tick marks in a "good chart".
axis.ticks.y = element_line(
Expand All @@ -29,7 +32,7 @@ theme_gc <- function(...) {
"black", "black", "black", "black", "black", "black"
)
),
axis.text = element_text(size = 15, color = "#000000"),
axis.text = ggplot2::element_text(size = 15, color = "#000000"),
legend.position = "none"
)
}
38 changes: 38 additions & 0 deletions man/gcplot.Rd

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

4 changes: 2 additions & 2 deletions man/theme_gc.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-gcplot.R
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)
})

0 comments on commit e64339c

Please sign in to comment.