diff --git a/DESCRIPTION b/DESCRIPTION index 22af0866..6e9bd311 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -51,3 +51,4 @@ Collate: 'tufte.R' 'utils.R' 'wsj.R' + 'hc.R' diff --git a/NAMESPACE b/NAMESPACE index 70137670..c9e9b83e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -31,6 +31,7 @@ export(scale_color_solarized) export(scale_color_stata) export(scale_color_tableau) export(scale_color_wsj) +export(scale_color_hc) export(scale_colour_calc) export(scale_colour_colorblind) export(scale_colour_economist) @@ -45,6 +46,7 @@ export(scale_colour_solarized) export(scale_colour_stata) export(scale_colour_tableau) export(scale_colour_wsj) +export(scale_colour_hc) export(scale_fill_calc) export(scale_fill_colorblind) export(scale_fill_continuous_tableau) @@ -60,6 +62,7 @@ export(scale_fill_solarized) export(scale_fill_stata) export(scale_fill_tableau) export(scale_fill_wsj) +export(scale_fill_hc) export(scale_linetype_stata) export(scale_shape_calc) export(scale_shape_circlefill) @@ -99,8 +102,10 @@ export(theme_solid) export(theme_stata) export(theme_tufte) export(theme_wsj) +export(theme_hc) export(tremmel_shape_pal) export(wsj_pal) +export(hc_pal) import(colorspace) import(ggplot2) import(grid) diff --git a/R/ggthemes-data.R b/R/ggthemes-data.R index 2b3a5898..644b411a 100644 --- a/R/ggthemes-data.R +++ b/R/ggthemes-data.R @@ -624,6 +624,15 @@ ggthemes_data <- { red = rgb(255, 39, 0, max = 255), blue = rgb(0, 143, 213, max = 255), green = rgb(119, 171, 67, max = 255)) + + x$hc <- list() + x$hc$palettes <- list() + x$hc$palettes$default <- c("#7cb5ec", "#434348", "#90ed7d", "#f7a35c", + "#8085e9", "#f15c80", "#e4d354", "#8085e8", "#8d4653", "#91e8e1") + x$hc$palettes$darkunica <- c("#2b908f", "#90ee7e", "#f45b5b", "#7798BF", "#aaeeee", "#ff0066", "#eeaaee", + "#55BF3B", "#DF5353", "#7798BF", "#aaeeee") + x$hc$bg <- c(default = "#FFFFFF", + darkunica = "#2a2a2b") ## Return x diff --git a/R/hc.R b/R/hc.R new file mode 100644 index 00000000..e801e5f3 --- /dev/null +++ b/R/hc.R @@ -0,0 +1,115 @@ +##' Wall Street Journal theme +##' +##' Theme based on the plots in \emph{Highcharts JS}. +##' +##' @references +##' +##' \url{http://www.highcharts.com/demo/line-basic} +##' +##' \url{https://github.com/highslide-software/highcharts.com/tree/master/js/themes} +##' +##' @param base_size Base font size. +##' @param theme The background color of plot. One of \code{"default", +##' "darkunica"}, the names of values in +##' \code{ggthemes_data$hc$bg}. +##' @examples +##' (qplot(hp, mpg, data=mtcars, geom="point") +##' + scale_colour_hc() +##' + ggtitle("Diamond Prices") +##' + theme_hc()) +##' ## Use a Dark-Unica theme +##' (qplot(hp, mpg, data=mtcars, geom="point") +##' + scale_colour_hc("darkunica") +##' + ggtitle("Diamond Prices") +##' + theme_hc("darkunica")) +##' @export + + +theme_hc <- function(theme="default", base_size=12) { + + bgcol <- ggthemes_data$hc$bg[theme] + + themes <- list() + + t <- theme( + rect = element_rect(fill=bgcol, linetype=0, colour=NA), + text = element_text(size=base_size), + title = element_text(hjust=.5), + axis.title.x = element_text(hjust=.5), + axis.title.y = element_text(hjust=.5), + panel.grid.major.y = element_line(color='gray'), + panel.grid.minor.y = element_blank(), + panel.grid.major.x = element_blank(), + panel.grid.minor.x = element_blank(), + panel.border = element_blank(), + panel.background = element_blank(), + legend.position = "bottom", + legend.key = element_rect(fill="#FFFFFF00")) + + themes$default <- t + + themes$darkunica <- t + theme( + rect = element_rect(fill=bgcol), + text = element_text(colour = "#A0A0A3"), + title = element_text(colour = "#FFFFFF"), + axis.title.x = element_text(colour = "#A0A0A3"), + axis.title.y = element_text(colour = "#A0A0A3"), + panel.grid.major.y = element_line(color="gray"), + legend.title = element_text(colour = "#A0A0A3")) + + themes[theme] +} + + +##' Highcharts JS color palette (discrete) +##' +##' The Highcharts JS uses many different color palettes in its +##' plots. This collects a few of them. +##' +##' @section Palettes: +##' +##' The following palettes are defined, +##' +##' \describe{ +##' \item{default}{#7cb5ec, #434348, #90ed7d, #f7a35c, #8085e9, #f15c80", #e4d354, #8085e8, #8d4653, #91e8e1 theme. Examples: \url{http://www.highcharts.com/demo}.} +##' \item{darkunica}{#2b908f, #90ee7e, #f45b5b, #7798BF, #aaeeee, #ff0066, #eeaaee, #55BF3B, #DF5353, #7798BF, #aaeeee". Examples: \url{http://www.highcharts.com/demo/line-basic/dark-unica}.} +##' } +##' +##' @param palette \code{character} The color palette to use. This +##' must be a name in +##' \code{\link[=ggthemes_data]{ggthemes_data$hc$palettes}}. +##' +##' @family colour hc +##' @export +hc_pal <- function(palette = "default") { + if (palette %in% names(ggthemes_data$hc$palettes)) { + manual_pal(unname(ggthemes_data$hc$palettes[[palette]])) + } else { + stop(sprintf("palette %s not a valid palette.", palette)) + } +} + +##' Highcharts color and fill scales +##' +##' Colour and fill scales which use the palettes in +##' \code{\link{hc_pal}} and are meant for use with +##' \code{\link{theme_hc}}. +##' +##' @inheritParams ggplot2::scale_colour_hue +##' @inheritParams hc_pal +##' @family colour_hc +##' @rdname scale_hc +##' @export +scale_colour_hc <- function(palette = "default", ...) { + discrete_scale("colour", "hc", hc_pal(palette), ...) +} + +##' @rdname scale_hc +##' @export +scale_color_hc <- scale_colour_hc + +##' @rdname scale_hc +##' @export +scale_fill_hc <- function(palette = "default", ...) { + discrete_scale("fill", "hc", hc_pal(palette), ...) +} \ No newline at end of file diff --git a/README.md b/README.md index ce398cf5..b4feeda9 100644 --- a/README.md +++ b/README.md @@ -70,257 +70,3 @@ Contributions are welcome! If you would like to add a theme, scales, etc., fork the repository, add your theme, and submit a pull request. -## Examples - - -```r -library("ggplot2") -library("ggthemes") -dsamp <- diamonds[sample(nrow(diamonds), 1000), ] -``` - -### Tufte theme and geoms - -Minimal theme and geoms based on plots in *The Visual Display of -Quantitative Information*. - - -```r -(ggplot(mtcars, aes(wt, mpg)) - + geom_point() + geom_rangeframe() - + theme_tufte()) -``` - -![plot of chunk tufte-rangeframe](http://i.imgur.com/xsUuvAk.png) - -The Tufte minimal boxplot. - - -```r -(ggplot(mtcars, aes(factor(cyl), mpg)) - + theme_tufte(ticks=FALSE) - + geom_tufteboxplot()) -``` - -![plot of chunk tufteboxplot](http://i.imgur.com/s5n0nu3.png) - -### Economist theme - -A theme that approximates the style of plots in The Economist -magazine. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_economist() - + scale_colour_economist() - + ggtitle("Diamonds Are Forever")) -``` - -![plot of chunk economist](http://i.imgur.com/LGXlzJY.png) - -### Solarized theme - -A theme and color and fill scales based on the Solarized palette. - -The light theme. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_solarized() - + scale_colour_solarized("blue")) -``` - -![plot of chunk solarized-light](http://i.imgur.com/lH04xRF.png) - -The dark theme. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_solarized(light=FALSE) - + scale_colour_solarized("red")) -``` - -![plot of chunk solarized-dark](http://i.imgur.com/6cRZg9Q.png) - -An alternative theme. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_solarized_2() - + scale_colour_solarized("blue")) -``` - -![plot of chunk solarized-alt](http://i.imgur.com/UjXYTks.png) - - -### Stata theme - -Themes and scales (color, fill, linetype, shapes) based on the graph -schemes in Stata. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_stata() - + scale_colour_stata() - + ggtitle("Plot Title")) -``` - -![plot of chunk stata](http://i.imgur.com/pXSXzV0.png) - -### Excel 2003 theme - -For that classic ugly look and feel. For ironic purposes only. 3D bars -and pies not included. Please never use this theme. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_excel() - + scale_colour_excel()) -``` - -![plot of chunk excel1](http://i.imgur.com/ktOlhC4.png) - - -```r -(ggplot(diamonds, aes(clarity, fill=cut)) - + geom_bar() - + scale_fill_excel() - + theme_excel()) -``` - -![plot of chunk excel2](http://i.imgur.com/sVI9poe.png) - -### Inverse Gray Theme - -Inverse of `theme_gray`, i.e. white plot area and gray background. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_igray()) -``` - -![plot of chunk igray](http://i.imgur.com/LPtRPuC.png) - -### Fivethirtyeight theme - -Theme and color palette based on the plots at [fivethirtyeight.com](http://fivethirtyeight.com). - - -```r -(qplot(hp, mpg, data= subset(mtcars, cyl != 5), geom="point", color = factor(cyl)) - + geom_smooth(method = "lm", se = FALSE) - + scale_color_fivethirtyeight() - + theme_fivethirtyeight()) -``` - -![plot of chunk fivethirtyeight](http://i.imgur.com/oVh1SzL.png) - -### Tableau Scales - -Color, fill, and shape scales based on those used in the Tableau softare. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_igray() - + scale_colour_tableau()) -``` - -![plot of chunk tableau](http://i.imgur.com/PDJPNjx.png) - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_igray() - + scale_colour_tableau("colorblind10")) -``` - -![plot of chunk tableau-colorbind10](http://i.imgur.com/raRbUkZ.png) - -### Stephen Few's Practical Rules for Using Color ... - -Color palette and theme based on Stephen Few's ["Practical Rules for Using Color in Charts"](http://www.perceptualedge.com/articles/visual_business_intelligence/rules_for_using_color.pdf). - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_few() - + scale_colour_few()) -``` - -![plot of chunk few](http://i.imgur.com/GP7BNWA.png) - -### Wall Street Journal - -Theme and some color palettes based on plots in the *The Wall Street Journal*. - - -```r -(qplot(carat, price, data=dsamp, colour=cut) - + theme_wsj() - + scale_colour_wsj("colors6", "") - + ggtitle("Diamond Prices")) -``` - -![plot of chunk wsj](http://i.imgur.com/lmKWR3Z.png) - -### GDocs Theme - -Theme and color palettes based on the defaults in Google Docs. - - -```r -(qplot(carat, price, data=dsamp, colour=clarity) - + theme_gdocs() - + ggtitle("Diamonds") - + scale_color_gdocs()) -``` - -![plot of chunk gdocs](http://i.imgur.com/euAv8BQ.png) - -### Calc Theme - -Theme and color and shape palettes based on the defaults in LibreOffice Calc. - - -```r -(qplot(carat, price, data=dsamp, colour=clarity) - + theme_calc() - + ggtitle("Diamonds") - + scale_color_calc()) -``` - -![plot of chunk calc](http://i.imgur.com/HAECgFG.png) - -### Pander Theme - -Theme and color palettes based on the [pander package](http://rapporter.github.io/pander/). - - -```r -(qplot(carat, price, data = dsamp, colour = clarity) - + theme_pander() - + scale_colour_pander()) -``` - -``` -## Loading required package: pander -``` - -![plot of chunk pander-scatterplot](http://i.imgur.com/3AUzDM6.png) - - -```r -(ggplot(dsamp, aes(clarity, fill = cut)) + geom_bar() - + theme_pander() - + scale_fill_pander()) -``` - -![plot of chunk pander-barplot](http://i.imgur.com/9NI0y1v.png) diff --git a/vignettes/children/examples.Rmd b/vignettes/children/examples.Rmd index 3a29eb75..1a95c0d6 100644 --- a/vignettes/children/examples.Rmd +++ b/vignettes/children/examples.Rmd @@ -191,3 +191,44 @@ Theme and color palettes based on the [pander package](http://rapporter.github.i + theme_pander() + scale_fill_pander()) ``` + +### Highcharts theme + +A theme that approximates the style of plots in [Highcharts JS](http://www.highcharts.com/demo). + +```{r hc-default} +(qplot(carat, price, data=dsamp, colour=cut) + + theme_hc() + + scale_colour_hc() + + ggtitle("Diamonds Are Forever")) +``` +```{r hc-darkunica} +(qplot(carat, price, data=dsamp, colour=cut) + + theme_hc("darkunica") + + scale_colour_hc("darkunica") + + ggtitle("Diamonds Are Forever")) +``` + +```{r dtemp} +dtemp <- data.frame(months = factor(rep(substr(month.name,1,3), 4), levels = substr(month.name,1,3)), + city = rep(c("Tokyo", "New York", "Berlin", "London"), each = 12), + temp = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6, + -0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5, + -0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0, + 3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8)) +``` + +```{r hc-default-line} +qplot(months, temp, data=dtemp, group=city, color=city, geom="line") + + geom_point(size=1.1) + + ggtitle("Monthly Average Temperature") + + theme_hc() + scale_colour_hc() +``` + +```{r hc-darkunica-line} +qplot(months, temp, data=dtemp, group=city, color=city, geom="line") + + geom_point(size=1.1) + + ggtitle("Monthly Average Temperature") + + theme_hc("darkunica") + scale_fill_hc("darkunica") +``` +