Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding some Highcharts JS themes #29

Merged
merged 3 commits into from
Dec 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ Collate:
'tufte.R'
'utils.R'
'wsj.R'
'hc.R'
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions R/ggthemes-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
115 changes: 115 additions & 0 deletions R/hc.R
Original file line number Diff line number Diff line change
@@ -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), ...)
}
Loading