Skip to content

Commit

Permalink
fix theme_nima + add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
nhejazi committed Apr 11, 2016
1 parent 0d4c615 commit 45f38f1
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 19 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: nima
Title: Nima Hejazi's Miscellaneous R Code
Version: 0.3.0
Version: 0.3.5
Authors@R: person("Nima", "Hejazi", email = "nhejazi@berkeley.edu",
role = c("aut", "cre"))
Description: Miscellaneous R functions developed over the course of statistical
Expand All @@ -17,7 +17,9 @@ Imports:
survival,
gridExtra,
assertthat,
ggthemes,
ggplot2,
scales,
grid
Suggests:
devtools,
Expand Down
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ export(objsizes)
export(openfile)
export(qqPlot_gg)
export(qrD)
export(scale_color_nima)
export(scale_fill_nima)
export(survPlot_gg)
export(theme_nima)
export(uniqlen)
importFrom(assertthat,assert_that)
importFrom(ggplot2,aes)
importFrom(ggplot2,discrete_scale)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_line)
importFrom(ggplot2,element_rect)
importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_abline)
importFrom(ggplot2,geom_bar)
importFrom(ggplot2,geom_hline)
Expand All @@ -27,6 +34,7 @@ importFrom(ggplot2,geom_step)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,rel)
importFrom(ggplot2,scale_colour_discrete)
importFrom(ggplot2,scale_colour_manual)
importFrom(ggplot2,scale_linetype_manual)
Expand All @@ -36,8 +44,11 @@ importFrom(ggplot2,theme)
importFrom(ggplot2,theme_bw)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(ggthemes,theme_foundation)
importFrom(grid,unit)
importFrom(gridExtra,grid.arrange)
importFrom(plyr,arrange)
importFrom(scales,manual_pal)
importFrom(stats,na.omit)
importFrom(stats,ppoints)
importFrom(stats,qnorm)
Expand Down
47 changes: 35 additions & 12 deletions R/theme_nima.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@
#' @param base_family Base font family
#' @param ... Passed to \code{\link[ggplot2]{theme}}
#'
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 ggplot rel element_text element_rect element_line
#' @importFrom ggplot2 element_blank
#' @importFrom grid unit
#' @importFrom ggthemes theme_foundation
#'
#' @export
#'
#' @return An object as returned by \code{\link[ggplot2]{theme}}
#'
#' @examples
#' library(ggplot2)
#' mtcars$cyl <- factor(mtcars$cyl)
#' p <- ggplot(mtcars, aes(y=mpg, x = disp, color = cyl))
#' p <- ggplot(mtcars, aes(y = mpg, x = disp, color = factor(cyl)))
#' p <- p + geom_point() + scale_fill_nima() + scale_color_nima() + theme_nima()
#' p
#'
#' @seealso
#' \code{\link[ggplot2]{theme}}


theme_nima <- function(base_size = 14, base_family = "Arial") {
library(grid)
library(ggthemes)
theme_nima <- function(base_size = 14, base_family = "Helvetica") {
(theme_foundation(base_size = base_size, base_family = base_family)
+ theme(plot.title = element_text(face = "bold",
size = rel(1.2), hjust = 0.5),
Expand Down Expand Up @@ -56,22 +55,46 @@ theme_nima <- function(base_size = 14, base_family = "Arial") {
))
}

#' @rdname theme_nima
#' @export
nima_theme <- theme_nima


#' Nima's ggplot2 theme - supplement: scale_fill
#'
#' Nima's ggplot2 theme scale_fill supplement: colors optimized via ColorBrewer
#'
#' @param ... Passed to \code{\link[ggplot2]{ggplot}}
#'
#' @importFrom ggplot2 discrete_scale
#' @importFrom scales manual_pal
#'
#' @export
#'

scale_fill_nima <- function(...){
library(scales)
discrete_scale("fill","nima",
manual_pal(values = c("#386cb0","#fdb462","#7fc97f",
"#ef3b2c","#662506","#a6cee3",
"#fb9a99","#984ea3","#ffff33")), ...)
}


#' Nima's ggplot2 theme - supplement: scale_color
#'
#' Nima's ggplot2 theme scale_color supplement: colors optimized via ColorBrewer
#'
#' @param ... Passed to \code{\link[ggplot2]{ggplot}}
#'
#' @importFrom ggplot2 discrete_scale
#' @importFrom scales manual_pal
#'
#' @export
#'

scale_color_nima <- function(...){
library(scales)
discrete_scale("colour","nima",
manual_pal(values = c("#386cb0","#fdb462","#7fc97f",
"#ef3b2c","#662506","#a6cee3",
"#fb9a99","#984ea3","#ffff33")), ...)
}

#' @rdname theme_nima
#' @export
nima_theme <- theme_nima
15 changes: 15 additions & 0 deletions man/scale_color_nima.Rd

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

15 changes: 15 additions & 0 deletions man/scale_fill_nima.Rd

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

11 changes: 5 additions & 6 deletions man/theme_nima.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/test-attribute_names.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library(nima)
context("Checking whether attribute names are properly extracted")

test_that("attrnames finds the names of the attributes of a matrix", {
x <- matrix(1:100, ncol = 5)
colnames(x) <- LETTERS[1:5]
expect_equal(attrnames(x),
c("dim", "dimnames") )
})

test_that("attrnames finds the names of the attributes of a data frame", {
x <- matrix(1:100, ncol = 5)
colnames(x) <- LETTERS[1:5]
y <- data.frame(x)
expect_equal(attrnames(y),
c("names", "row.names", "class") )
})

0 comments on commit 45f38f1

Please sign in to comment.