Skip to content

Commit

Permalink
Merge pull request #41 from jhollway/develop
Browse files Browse the repository at this point in the history
Added iheid_palette(s) functions
  • Loading branch information
jhollway authored Dec 19, 2020
2 parents d56cc35 + 312f9f3 commit cb0a207
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 4 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: iheiddown
Title: A package for writing IHEID documents in RMarkdown
Date: 2020-11-23
Version: 0.7.2
Date: 2020-12-19
Version: 0.7.3
Authors@R:
c(person(given = "James",
family = "Hollway",
Expand All @@ -26,4 +26,4 @@ LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Suggests:
testthat, kableExtra
testthat, kableExtra, ggantrify
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# Generated by roxygen2: do not edit by hand

S3method(print,palette)
export(advdate)
export(bookdown_profile)
export(chapter_pdf)
export(iheid_palette)
export(iheid_palettes)
export(syllabus_pdf)
export(thesis_pdf)
importFrom(bookdown,pdf_book)
importFrom(bookdown,preview_chapter)
importFrom(bookdown,render_book)
importFrom(grDevices,rgb)
importFrom(graphics,image)
importFrom(graphics,par)
importFrom(graphics,rect)
importFrom(graphics,text)
importFrom(rmarkdown,pdf_document)
importFrom(utils,file.edit)
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# iheiddown 0.7.3

## Package

* Fixed #38 by adding `iheid_palettes` and `iheid_palette()` for accessing IHEID and SDG color palettes

## Thesis

* Closed #22 by adding example of how to create a Gantt plot including call to `iheid_palette()`

# iheiddown 0.7.2

## Thesis
Expand Down
95 changes: 95 additions & 0 deletions R/palettes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#' Complete list of palettes
#'
#' Use \code{\link{iheid_palette}} to construct palettes of desired length.
#'
#' @export
iheid_palettes <- list("IHEID" = c("IHEIDRed" = "#E20020",
"IHEIDBlack" = "#5c666f",
"IHEIDGrey" = "#6f7072"),
"Centres" = c("AHCD" = "#622550",
"CFFD" = "#0094D8",
"CIES" = "#268D2B",
"CTEI" = "#008F92",
"CGEN" = "#820C2B",
"CCDP" = "#3E2682",
"GLGC" = "#006564",
"GLHC" = "#A8086E",
"GLMC" = "#006EAA"),
"SDGs" = c("NoPoverty" = "#e5243b",
"ZeroHunger" = "#DDA63A",
"GoodHealth" = "#4C9F38",
"QualityEducation" = "#C5192D",
"GenderEquality" = "#FF3A21",
"CleanWater" = "#26BDE2",
"CleanEnergy" = "#FCC30B",
"EconomicGrowth" = "#A21942",
"Innovation" = "#FD6925",
"ReducedInequalities" = "#DD1367",
"SustainableCities" = "#FD9D24",
"ResponsibleConsumption" = "#BF8B2E",
"ClimateAction" = "#3F7E44",
"BelowWater" = "#0A97D9",
"OnLand" = "#56C02B",
"StrongInstitutions" = "#00689D",
"GoalPartnerships" = "#19486A"))

#' An IHEID palette generator
#'
#' These are a couple of color palettes for the Graduate Institute.
#'
#' @param n Number of colors desired. If omitted, uses all colours.
#' @param name Name of desired palette. Current choices are:
#' \code{IHEID}, \code{Centres}, and \code{SDGs}.
#' @param type Either "continuous" or "discrete". Use continuous if you want
#' to automatically interpolate between colours.
#' @importFrom graphics rect par image text
#' @return A vector of colours.
#' @source Adapted from https://github.com/karthik/wesanderson/blob/master/R/colors.R
#' @export
#' @keywords colors
#' @examples
#' iheid_palette("IHEID")
#' iheid_palette("Centres")
#' iheid_palette("SDGs")
#'
#' # If you need more colours than normally found in a palette, you
#' # can use a continuous palette to interpolate between existing
#' # colours
#' pal <- iheid_palette(21, name = "Centres", type = "continuous")
#' image(volcano, col = pal)
iheid_palette <- function(name, n, type = c("discrete", "continuous")) {
type <- match.arg(type)

pal <- iheid_palettes[[name]]
if (is.null(pal))
stop("Palette not found.")

if (missing(n)) {
n <- length(pal)
}

if (type == "discrete" && n > length(pal)) {
stop("Number of requested colors greater than what palette can offer")
}

out <- switch(type,
continuous = grDevices::colorRampPalette(pal)(n),
discrete = pal[1:n]
)
structure(out, class = "palette", name = name)
}

#' @export
#' @importFrom graphics rect par image text
#' @importFrom grDevices rgb
print.palette <- function(x, ...) {
n <- length(x)
old <- par(mar = c(0.5, 0.5, 0.5, 0.5))
on.exit(par(old))

image(1:n, 1, as.matrix(1:n), col = x,
ylab = "", xaxt = "n", yaxt = "n", bty = "n")

rect(0, 0.9, n + 1, 1.1, col = rgb(1, 1, 1, 0.8), border = NA)
text((n + 1) / 2, 1, labels = attr(x, "name"), cex = 1, family = "serif")
}
4 changes: 3 additions & 1 deletion inst/rmarkdown/templates/thesis/skeleton/04-results.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ mean_delay_by_carrier <- flights %>%
group_by(carrier) %>%
summarize(mean_dep_delay = mean(dep_delay))
ggplot(mean_delay_by_carrier, aes(x = carrier, y = mean_dep_delay)) +
geom_bar(position = "identity", stat = "identity", fill = "red")
geom_bar(position = "identity", stat = "identity",
fill = iheiddown::iheid_palette("IHEID", 1)) +
theme_minimal()
```

You don't have to use `{ggplot2}` though.
Expand Down
29 changes: 29 additions & 0 deletions inst/rmarkdown/templates/thesis/skeleton/05-conclusion.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@ output:
\setcounter{chapter}{5}
# Conclusion {#conclusion}

In sum, `{iheiddown}` offers an easy way to write IHEID-consistent theses,
but is enormously extensible and adaptable,
allowing students to craft their own dissertations and other documents.

## Using iheiddown for proposals

As an example of this, let us consider how one might begin using iheiddown
from the start of a masters or doctoral dissertation,
and not just at the end while 'writing up' (a good idea in any case).

One feature often requested by supervisors from DDPs and MPTs
is the preparation of a plan about how and when the various tasks
associated with the project will be completed.
Your supervisor may even ask for this to be presented in a table or a Gantt chart.

We have covered tables in the previous chapter.
Here I want to offer a quick vignette about how you can create a Gantt chart
that outlines the temporal progression you expect to make.
We will use the `{ggantrify}` package to do this.

```{r }
library("ganttrify")
ganttrify(project = ganttrify::test_project,
project_start_date = "2020-03",
colour_palette = iheiddown::iheid_palette("Centres",
type = "continuous")
)
```

## Additional resources

- [_Markdown_ Cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-palettes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("palettes work", {
expect_equal(unname(iheid_palette("IHEID")[1]), c("#E20020"))
expect_equal(unname(iheid_palette("IHEID")[2]), c("#5c666f"))
expect_equal(unname(iheid_palette("IHEID")[3]), c("#6f7072"))
expect_equal(unname(iheid_palette("Centres")[3]), c("#268D2B"))
expect_equal(unname(iheid_palette("SDGs")[3]), c("#4C9F38"))
})

0 comments on commit cb0a207

Please sign in to comment.