Skip to content
This repository
Browse code

initial commit

  • Loading branch information...
commit 1ff019d8e343f3995cc2ec1bc787f76b398c6ea9 0 parents
Jeffrey Arnold authored
1  .Rinstignore
... ... @@ -0,0 +1 @@
  1 +roxygen
20 DESCRIPTION
... ... @@ -0,0 +1,20 @@
  1 +Package: ggplotJrnold
  2 +Version: 0.0.1
  3 +Date: 2012-9-6
  4 +Title: ggplot related stuff
  5 +Authors@R: c(person("Jeffrey B.", "Arnold", role = c("aut", "cre"),
  6 + email = "jeffrey.arnold@gmail.com"))
  7 +Author: Jeffrey B. Arnold <jeffrey.arnold@gmail.com>
  8 +Maintainer: Jeffrey B. Arnold <jeffrey.arnold@gmail.com>
  9 +Depends:
  10 + R (>= 2.15.0),
  11 + ggplot2 (>= 0.9.2)
  12 +Imports:
  13 + scales (>= 0.2.2),
  14 + grid
  15 +Description: Some extra themes and scales for ggplot.
  16 +License: GPL-3
  17 +URL: http://github.com/jrnold/ggplot-jrnold
  18 +BugReports: http://github.com/jrnold/ggplot-jrnold
  19 +Collate:
  20 + 'economist.R'
5 NAMESPACE
... ... @@ -0,0 +1,5 @@
  1 +export(economist_colors)
  2 +export(economist_pal)
  3 +export(scale_colour_economist)
  4 +export(scale_fill_economist)
  5 +export(theme_economist)
186 R/economist.R
... ... @@ -0,0 +1,186 @@
  1 +##' Economist colors
  2 +##'
  3 +##' @format \code{character} vector of the hex RGB values of the
  4 +##' colors used by The Economist.
  5 +##' @usage economist_colors
  6 +##' @seealso economist_pal
  7 +##' @export
  8 +economist_colors <-
  9 + c(## Background
  10 + ebg = rgb(198,211,223, max=255),
  11 + edkbg = rgb(178,191,203, max=255),
  12 + ## Symbols
  13 + edkblue = rgb(62,100,125, max=255),
  14 + emidblue = rgb(123,146,168, max=255),
  15 + eltblue = rgb(130,192,233, max=255),
  16 + gs6 = rgb(96, 96, 96, max=255),
  17 + emerald = rgb(45,109,102, max=255),
  18 + erose = rgb(191,161,156, max=255),
  19 + ebblue = rgb(0,139,188, max=255),
  20 + eltgreen = rgb(151,182,176, max=255),
  21 + stone = rgb(215,210,158, max=255),
  22 + navy = rgb(26,71,111, max=255),
  23 + maroon = rgb(144,53,59, max=255),
  24 + brown = rgb(156,136,71, max=255),
  25 + lavender = rgb(147,141,210, max=255),
  26 + teal = rgb(110,142,132, max=255),
  27 + cranberry=rgb(193, 5, 52, max=255),
  28 + khaki = rgb(202,194, 126, max=255))
  29 +
  30 +##' Economist color palette (discrete)
  31 +##'
  32 +##' @param n \code{integer}. Number of values.
  33 +##' @export
  34 +##' @examples
  35 +##' show_col(economist_pal(16))
  36 +economist_pal <- manual_pal(unname(economist_colors[3:length(economist_colors)]))
  37 +
  38 +##' Economist color scales
  39 +##'
  40 +##' Color scales using the colors in the Economist graphics. These scales use
  41 +##' RGB values and ordering of the colors in the State economist scheme.
  42 +##'
  43 +##' @inheritParams ggplot2::scale_colour_hue
  44 +##' @family colour scales
  45 +##' @rdname scale_economist
  46 +##' @export
  47 +##' @examples
  48 +##' dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
  49 +##' (d <- qplot(carat, price, data=dsamp, colour=clarity) +
  50 +##' + theme_economist()
  51 +##' + scale_colour_economist() )
  52 +scale_colour_economist <- function(...) discrete_scale("colour", "economist",
  53 + economist_pal, ...)
  54 +
  55 +#' @export
  56 +#' @rdname scale_economist
  57 +scale_fill_economist <- function(...) discrete_scale("colour", "economist",
  58 + economist_pal, ...)
  59 +
  60 +##' ggplot color theme based on the Economist
  61 +##'
  62 +##' Style plots similar to those in The Economist.
  63 +##'
  64 +##' @param base_size base font size
  65 +##' @param base_family base font family
  66 +##' @export
  67 +##' @family themes
  68 +##' @examples
  69 +##' dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
  70 +##' (d <- qplot(carat, price, data=dsamp, colour=clarity) +
  71 +##' + theme_economist()
  72 +##' + scale_colour_economist() )
  73 +theme_economist <- function(base_size = 12, base_family="",
  74 + horizontal=TRUE, inner=FALSE) {
  75 + palette_economist_bg <- c(ebg = "#C6D3DF",
  76 + edkbg = "#B2BFCB")
  77 + ret <-
  78 + theme(# Basic
  79 + line = element_line(colour = "black", size = 0.5,
  80 + linetype = 1, lineend = "butt"),
  81 + rect = element_rect(fill = palette_economist_bg['ebg'],
  82 + colour = NA, size = 0.5, linetype = 1),
  83 + text = element_text(family = base_family, face = "plain",
  84 + colour = "black",
  85 + size = base_size, hjust = 0.5, vjust = 0.5, angle = 0,
  86 + lineheight = 0.9),
  87 + ## Axis
  88 + axis.text = element_text(size = rel(0.8)),
  89 + axis.line = element_line(size = rel(0.8)),
  90 + axis.line.y = element_blank(),
  91 + axis.text.x = element_text(vjust = 1),
  92 + axis.text.y = element_text(hjust = 0),
  93 + ## I cannot figure out how to get ggplot to do 2 levels of ticks
  94 + axis.ticks = element_line(),
  95 + axis.ticks.y = element_blank(),
  96 + axis.title.x = element_text(),
  97 + axis.title.y = element_text(angle = 90),
  98 + axis.ticks.length = unit(-0.2 , "cm"),
  99 + axis.ticks.margin = unit(0.6, "cm"),
  100 + legend.background = element_rect(linetype=0),
  101 + legend.margin = unit(0.2, "cm"),
  102 + legend.key = element_rect(linetype=0),
  103 + legend.key.size = unit(1.2, "lines"),
  104 + legend.key.height = NULL,
  105 + legend.key.width = NULL,
  106 + legend.text = element_text(size = rel(0.8)),
  107 + legend.text.align = NULL,
  108 + legend.title = element_text(size = rel(0.8), face = "bold",
  109 + hjust = 0),
  110 + legend.title.align = NULL,
  111 + legend.position = "right",
  112 + legend.direction = NULL,
  113 + legend.justification = "center",
  114 + ## legend.box = element_rect(fill = palette_economist['bgdk'], colour=NA, linetype=0),
  115 + ## Economist only uses vertical lines
  116 + panel.background = element_rect(linetype=0),
  117 + panel.border = element_blank(),
  118 + panel.grid.major = element_line(colour = "white", size=rel(2)),
  119 + panel.grid.minor = element_blank(),
  120 + panel.margin = unit(0.25, "lines"),
  121 + strip.background = element_rect(fill = palette_economist_bg['edkbg'],
  122 + colour = NA, linetype=0),
  123 + strip.text = element_text(size = rel(0.8)),
  124 + strip.text.x = element_text(),
  125 + strip.text.y = element_text(angle = -90),
  126 + plot.background = element_rect(fill = palette_economist_bg['ebg'], colour=NA),
  127 + plot.title = element_text(size = rel(1.2), hjust=0),
  128 + plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
  129 + complete = TRUE)
  130 + if (horizontal) {
  131 + ret <- ret + theme(panel.grid.major.x = element_blank())
  132 + } else {
  133 + ret <- ret + theme(panel.grid.major.y = element_blank())
  134 + }
  135 + if (inner==TRUE) {
  136 + ret <- ret + theme(panel.background=element_rect(fill = palette_economist_bg['edkbg']))
  137 + }
  138 + ret
  139 +}
  140 +## TODO:
  141 +## - white background http://junkcharts.typepad.com/.shared/image.html?/photos/uncategorized/2008/04/18/econ_anglosaxon.gif
  142 +## - white and gray http://www.economist.com/blogs/graphicdetail/2012/09/daily-chart-3
  143 +
  144 +## RGB values from Stata economist scheme
  145 +
  146 +
  147 +## From latticeExtra::theEconomist.theme
  148 +## palette_economist_1 <- c(dkblue="#00526D", ltblue="#00A3DB", dkred="#7A2713",
  149 +## yellow="#939598", ltblue2="#6CCFF6")
  150 +
  151 +## Palette
  152 +## 3 color http://www.economist.com/blogs/graphicdetail/2012/08/daily-chart-olympics-4 (blue, red, lt red)
  153 +## 3 and 4 colors http://www.economist.com/node/21561909
  154 +## 5 colors http://www.economist.com/node/21560601, http://www.economist.com/blogs/graphicdetail/2012/08/daily-chart-6
  155 +## pal_economist_colour <-
  156 +## manual_pal(c(dkblue="#014B62", blue="#00A0D8", ltblue="#79D3F6",
  157 +## red="#781D01", gray="#ACABAC"))
  158 +
  159 +##
  160 +## 1 color http://www.economist.com/node/21561883
  161 +## 2 color http://www.economist.com/node/21560572
  162 +## 3 color http://www.economist.com/node/21560582
  163 +## 4 color http://www.economist.com/blogs/graphicdetail/2012/09/focus
  164 +## 6 color http://junkcharts.typepad.com/.a/6a00d8341e992c53ef01538e032bd5970b-pi
  165 +## 7 color
  166 +## http://www.economist.com/node/14941181?story_id=14941181, http://www.economist.com/blogs/graphicdetail/2012/07/focus-4
  167 +## 8 color http://www.economist.com/node/16834943?story_id=16834943
  168 +## pal_economist_fill <- function(n) {
  169 +## colors <- c(bluegray="#6794a7", dkblue="#014D65", blue="01a29d9",
  170 +## gray="ADADAD", ltgreen="#7AD2F6", ltgreen2="#76C0C1",
  171 +## green="#00887E")
  172 +## if (n == 1) {
  173 +## colors['dkblue']
  174 +## } else if (n == 2) {
  175 +## colors[c("blue", "dkblue")]
  176 +## } else {
  177 +## colors[1:n]
  178 +## }
  179 +## }
  180 +
  181 +## RGB values from Stata economist scheme
  182 +## pal_economist_fill <-
  183 +## manual_pal(c(blue="#014D63", bluegray="#6895A7", gray="#ACADAF",
  184 +## ltblue="#009FD7", ltgreen="#74BBBE", red="#762918",
  185 +## green="#02847B", ltblue2="#75CFF3"))
  186 +
14 man/economist_colors.Rd
... ... @@ -0,0 +1,14 @@
  1 +\docType{data}
  2 +\name{economist_colors}
  3 +\alias{economist_colors}
  4 +\title{Economist colors}
  5 +\format{\code{character} vector of the hex RGB values of the
  6 +colors used by The Economist.}
  7 +\description{
  8 + Economist colors
  9 +}
  10 +\seealso{
  11 + economist_pal
  12 +}
  13 +\keyword{datasets}
  14 +
16 man/economist_pal.Rd
... ... @@ -0,0 +1,16 @@
  1 +\name{economist_pal}
  2 +\alias{economist_pal}
  3 +\title{Economist color palette (discrete)}
  4 +\usage{
  5 + economist_pal(n)
  6 +}
  7 +\arguments{
  8 + \item{n}{\code{integer}. Number of values.}
  9 +}
  10 +\description{
  11 + Economist color palette (discrete)
  12 +}
  13 +\examples{
  14 +show_col(economist_pal(16))
  15 +}
  16 +
29 man/scale_economist.Rd
... ... @@ -0,0 +1,29 @@
  1 +\name{scale_colour_economist}
  2 +\alias{scale_colour_economist}
  3 +\alias{scale_fill_economist}
  4 +\title{Economist color scales}
  5 +\usage{
  6 + scale_colour_economist(...)
  7 +
  8 + scale_fill_economist(...)
  9 +}
  10 +\arguments{
  11 + \item{...}{Other arguments passed on to
  12 + \code{\link{discrete_scale}} to control name, limits,
  13 + breaks, labels and so forth.}
  14 +}
  15 +\description{
  16 + Color scales using the colors in the Economist graphics.
  17 + These scales use RGB values and ordering of the colors in
  18 + the State economist scheme.
  19 +}
  20 +\examples{
  21 +dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
  22 +(d <- qplot(carat, price, data=dsamp, colour=clarity) +
  23 + + theme_economist()
  24 + + scale_colour_economist() )
  25 +}
  26 +\seealso{
  27 + Other res: \code{\link{theme_economist}}
  28 +}
  29 +
26 man/theme_economist.Rd
... ... @@ -0,0 +1,26 @@
  1 +\name{theme_economist}
  2 +\alias{theme_economist}
  3 +\title{ggplot color theme based on the Economist}
  4 +\usage{
  5 + theme_economist(base_size = 12, base_family = "",
  6 + horizontal = TRUE, inner = FALSE)
  7 +}
  8 +\arguments{
  9 + \item{base_size}{base font size}
  10 +
  11 + \item{base_family}{base font family}
  12 +}
  13 +\description{
  14 + Style plots similar to those in The Economist.
  15 +}
  16 +\examples{
  17 +dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
  18 +(d <- qplot(carat, price, data=dsamp, colour=clarity) +
  19 + + theme_economist()
  20 + + scale_colour_economist() )
  21 +}
  22 +\seealso{
  23 + Other res: \code{\link{scale_colour_economist}},
  24 + \code{\link{scale_fill_economist}}
  25 +}
  26 +
3  roxygen
... ... @@ -0,0 +1,3 @@
  1 +#!/usr/bin/env Rscript
  2 +library(roxygen2)
  3 +roxygenize(".", ".", overwrite=TRUE, unlink.target=TRUE)

0 comments on commit 1ff019d

Please sign in to comment.
Something went wrong with that request. Please try again.