Skip to content

update from master #2

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

Merged
merged 20 commits into from
Jan 25, 2016
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
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ Collate:
'theme-defaults.r'
'theme-elements.r'
'theme.r'
'translate-qplot-base.r'
'translate-qplot-ggplot.r'
'translate-qplot-lattice.r'
'utilities-break.r'
Expand All @@ -210,4 +209,4 @@ Collate:
'zxx.r'
'zzz.r'
VignetteBuilder: knitr
RoxygenNote: 5.0.0
RoxygenNote: 5.0.1
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# ggplot2 2.0.0.9000

* Add access to `bw` argument of `density` in `stat_density`, which makes
it easy to get consistent smoothing between facets for example (@jiho)

## Bug fixes and minor improvements

* `stat_function()` once again works with discrete x axes (#1509).

* All `\donttest{}` examples run.

* Removed a superfluous comma in `theme-defaults.r` code (@jschoeley)

* Fixed a compatibility issue with `ggproto` and R versions prior to 3.1.2.
(#1444)

Expand Down
6 changes: 3 additions & 3 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@
#' }
"msleep"

#' Terms of 10 presidents from Eisenhower to Bush W.
#' Terms of 11 presidents from Eisenhower to Obama.
#'
#' The names of each president, the start and end date of their term, and
#' their party of 10 US presidents from Eisenhower to Bush W.
#' their party of 11 US presidents from Eisenhower to Obama.
#'
#' @format A data frame with 10 rows and 4 variables
#' @format A data frame with 11 rows and 4 variables
"presidential"

#' Vector field of seal movements.
Expand Down
7 changes: 5 additions & 2 deletions R/fortify.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ fortify.data.frame <- function(model, data, ...) model
fortify.NULL <- function(model, data, ...) waiver()
#' @export
fortify.default <- function(model, data, ...) {

stop("ggplot2 doesn't know how to deal with data of class ", class(model), call. = FALSE)
stop(
"ggplot2 doesn't know how to deal with data of class ",
paste(class(model), collapse = "/"),
call. = FALSE
)
}
4 changes: 2 additions & 2 deletions R/margins.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' Define margins.
#'
#' This is a convenience function that creates a grid unit object of the
#' This is a convenient function that creates a grid unit object of the
#' correct length to use for setting margins.
#'
#' @export
#' @param t,b,r,l Dimensions of each margin. (To remember order, think trouble).
#' @param t,r,b,l Dimensions of each margin. (To remember order, think trouble).
#' @param unit Default units of dimensions. Defaults to "pt" so it
#' can be most easily scaled with the text.
#' @export
Expand Down
2 changes: 1 addition & 1 deletion R/plot.r
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ggplot.default <- function(data = NULL, mapping = aes(), ...,
ggplot.data.frame <- function(data, mapping = aes(), ...,
environment = parent.frame()) {
if (!missing(mapping) && !inherits(mapping, "uneval")) {
stop("Mapping should be created with aes or aes_string")
stop("Mapping should be created with `aes() or `aes_()`.", call. = FALSE)
}

p <- structure(list(
Expand Down
6 changes: 1 addition & 5 deletions R/scale-discrete-.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
#' @rdname scale_discrete
#' @export
#' @examples
#' \donttest{
#' ggplot(diamonds, aes(cut)) + stat_bin()
#' ggplot(diamonds, aes(cut)) + geom_bar()
#'
#' \donttest{
#' # The discrete position scale is added automatically whenever you
#' # have a discrete position.
#'
Expand All @@ -29,9 +28,6 @@
#' d + scale_x_discrete("Cut", labels = c("Fair" = "F","Good" = "G",
#' "Very Good" = "VG","Perfect" = "P","Ideal" = "I"))
#'
#' d + scale_y_discrete("Clarity")
#' d + scale_x_discrete("Cut") + scale_y_discrete("Clarity")
#'
#' # Use limits to adjust the which levels (and in what order)
#' # are displayed
#' d + scale_x_discrete(limits=c("Fair","Ideal"))
Expand Down
12 changes: 8 additions & 4 deletions R/stat-density.r
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#' @param adjust see \code{\link{density}} for details
#' @param bw the smoothing bandwidth to be used, see
#' \code{\link{density}} for details
#' @param adjust adjustment of the bandwidth, see
#' \code{\link{density}} for details
#' @param kernel kernel used for density estimation, see
#' \code{\link{density}} for details
#' @param trim This parameter only matters if you are displaying multiple
Expand All @@ -17,7 +20,7 @@
#' @export
#' @rdname geom_density
stat_density <- function(mapping = NULL, data = NULL, geom = "area",
position = "stack", adjust = 1, kernel = "gaussian",
position = "stack", bw = "nrd0", adjust = 1, kernel = "gaussian",
trim = FALSE, na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE, ...) {

Expand All @@ -30,6 +33,7 @@ stat_density <- function(mapping = NULL, data = NULL, geom = "area",
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
bw = bw,
adjust = adjust,
kernel = kernel,
trim = trim,
Expand All @@ -47,7 +51,7 @@ StatDensity <- ggproto("StatDensity", Stat,
required_aes = "x",
default_aes = aes(y = ..density.., fill = NA),

compute_group = function(data, scales, adjust = 1, kernel = "gaussian",
compute_group = function(data, scales, bw = "nrd0", adjust = 1, kernel = "gaussian",
trim = FALSE, na.rm = FALSE) {
if (trim) {
range <- range(data$x, na.rm = TRUE)
Expand All @@ -56,7 +60,7 @@ StatDensity <- ggproto("StatDensity", Stat,
}

compute_density(data$x, data$weight, from = range[1], to = range[2],
adjust = adjust, kernel = kernel)
bw = bw, adjust = adjust, kernel = kernel)
}

)
Expand Down
10 changes: 9 additions & 1 deletion R/stat-function.r
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,17 @@ StatFunction <- ggproto("StatFunction", Stat,
range <- scales$x$dimension()
xseq <- seq(range[1], range[2], length.out = n)

if (scales$x$is_discrete()) {
x_trans <- xseq
} else {
# For continuous scales, need to back transform from transformed range
# to original values
x_trans <- scales$x$trans$inverse(xseq)
}

data.frame(
x = xseq,
y = do.call(fun, c(list(quote(scales$x$trans$inv(xseq))), args))
y = do.call(fun, c(list(quote(x_trans)), args))
)
}
)
37 changes: 9 additions & 28 deletions R/stat-summary.r
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,18 @@
#'
#' # You can supply individual functions to summarise the value at
#' # each x:
#' d + stat_summary(fun.y = "median", colour = "red", size = 2)
#' d + stat_summary(fun.y = "mean", colour = "red", size = 2)
#' d + stat_summary(fun.y = "median", colour = "red", size = 2, geom = "point")
#' d + stat_summary(fun.y = "mean", colour = "red", size = 2, geom = "point")
#' d + aes(colour = factor(vs)) + stat_summary(fun.y = mean, geom="line")
#'
#' d + stat_summary(fun.y = mean, fun.ymin = min, fun.ymax = max,
#' colour = "red")
#'
#' #' d <- ggplot(diamonds, aes(carat, price))
#' d + geom_smooth()
#' d + geom_line(stat = "summary_bin", binwidth = 0.1, fun.y = "mean")
#'
#' d <- ggplot(diamonds, aes(cut))
#' d + geom_bar()
#' d + stat_summary_bin(aes(y = price), fun.y = "mean", geom = "bar")

#' \donttest{
#' # A set of useful summary functions is provided from the Hmisc package:
#' stat_sum_df <- function(fun, geom="crossbar", ...) {
#' stat_summary(fun.data=fun, colour="red", geom=geom, width=0.2, ...)
#' }
#'
#' \donttest{
#' # Don't use ylim to zoom into a summary plot - this throws the
#' # data away
#' p <- ggplot(mtcars, aes(cyl, mpg)) +
Expand All @@ -77,28 +68,18 @@
#' # Instead use coord_cartesian
#' p + coord_cartesian(ylim = c(15, 30))
#'
#' # A set of useful summary functions is provided from the Hmisc package:
#' stat_sum_df <- function(fun, geom="crossbar", ...) {
#' stat_summary(fun.data=fun, colour="red", geom=geom, width=0.2, ...)
#' }
#' d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
#' # The crossbar geom needs grouping to be specified when used with
#' # a continuous x axis.
#' d + stat_sum_df("mean_cl_boot", mapping = aes(group = cyl))
#' d + stat_sum_df("mean_sdl", mapping = aes(group = cyl))
#' d + stat_sum_df("mean_sdl", mult = 1, mapping = aes(group = cyl))
#' d + stat_sum_df("mean_sdl", fun.args = list(mult = 1), mapping = aes(group = cyl))
#' d + stat_sum_df("median_hilow", mapping = aes(group = cyl))
#'
#' # There are lots of different geoms you can use to display the summaries
#'
#' d + stat_sum_df("mean_cl_normal", mapping = aes(group = cyl))
#' d + stat_sum_df("mean_cl_normal", geom = "errorbar")
#' d + stat_sum_df("mean_cl_normal", geom = "pointrange")
#' d + stat_sum_df("mean_cl_normal", geom = "smooth")
#'
#' # Summaries are more useful with a bigger data set:
#' mpg2 <- subset(mpg, cyl != 5L)
#' m <- ggplot(mpg2, aes(x=cyl, y=hwy)) +
#' geom_point() +
#' stat_summary(fun.data = "mean_sdl", geom = "linerange",
#' colour = "red", size = 2, mult = 1) +
#' xlab("cyl")
#' m
#' # An example with highly skewed distributions:
#' if (require("ggplot2movies")) {
#' set.seed(596)
Expand Down
11 changes: 6 additions & 5 deletions R/stat-ydensity.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @export
#' @rdname geom_violin
stat_ydensity <- function(mapping = NULL, data = NULL, geom = "violin",
position = "dodge", adjust = 1, kernel = "gaussian",
position = "dodge", bw = "nrd0", adjust = 1, kernel = "gaussian",
trim = TRUE, scale = "area", na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE, ...) {
scale <- match.arg(scale, c("area", "count", "width"))
Expand All @@ -32,6 +32,7 @@ stat_ydensity <- function(mapping = NULL, data = NULL, geom = "violin",
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
bw = bw,
adjust = adjust,
kernel = kernel,
trim = trim,
Expand All @@ -51,7 +52,7 @@ StatYdensity <- ggproto("StatYdensity", Stat,
required_aes = c("x", "y"),
non_missing_aes = "weight",

compute_group = function(data, scales, width = NULL, adjust = 1,
compute_group = function(data, scales, width = NULL, bw = "nrd0", adjust = 1,
kernel = "gaussian", trim = TRUE, na.rm = FALSE) {
if (nrow(data) < 3) return(data.frame())

Expand All @@ -61,7 +62,7 @@ StatYdensity <- ggproto("StatYdensity", Stat,
range <- scales$y$dimension()
}
dens <- compute_density(data$y, data$w, from = range[1], to = range[2],
adjust = adjust, kernel = kernel)
bw = bw, adjust = adjust, kernel = kernel)

dens$y <- dens$x
dens$x <- mean(range(data$x))
Expand All @@ -75,11 +76,11 @@ StatYdensity <- ggproto("StatYdensity", Stat,
dens
},

compute_panel = function(self, data, scales, width = NULL, adjust = 1,
compute_panel = function(self, data, scales, width = NULL, bw = "nrd0", adjust = 1,
kernel = "gaussian", trim = TRUE, na.rm = FALSE,
scale = "area") {
data <- ggproto_parent(Stat, self)$compute_panel(
data, scales, width = width, adjust = adjust, kernel = kernel,
data, scales, width = width, bw = bw, adjust = adjust, kernel = kernel,
trim = trim, na.rm = na.rm
)

Expand Down
2 changes: 1 addition & 1 deletion R/theme-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ theme_grey <- function(base_size = 11, base_family = "") {
),
axis.title.y = element_text(
angle = 90,
margin = margin(r = 0.8 * half_line, l = 0.8 * half_line / 2),
margin = margin(r = 0.8 * half_line, l = 0.8 * half_line / 2)
),

legend.background = element_rect(colour = NA),
Expand Down
Loading