-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* dodge works again * stack, dodge and fill all use same base function * jitter uses width and height arguments to match others (and future)
- Loading branch information
Showing
7 changed files
with
118 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
collide <- function(data, width = NULL, name, strategy) { | ||
|
||
# Determine width | ||
if (!is.null(width)) { | ||
# Width set manually | ||
width <- .$width | ||
data <- within(data, { | ||
xmin <- x - width / 2 | ||
xmax <- x + width / 2 | ||
}) | ||
} else { | ||
# Width determined from data, must be floating point constant | ||
widths <- unique(with(data, xmax - xmin)) | ||
if (length(widths) > 1 && sd(widths) > 1e-6) { | ||
stop(name, " requires constant width", call. = FALSE) | ||
} | ||
width <- widths[1] | ||
} | ||
|
||
# Check for overlap | ||
intervals <- as.numeric(t(unique(data[c("xmin", "xmax")]))) | ||
if (any(diff(intervals) < -1e-6)) { | ||
stop(name, " requires non-overlapping x intervals", call. = FALSE) | ||
# This is where the algorithm from [L. Wilkinson. Dot plots. | ||
# The American Statistician, 1999.] should be used | ||
} | ||
|
||
ddply(data, .(xmin), function(df) strategy(df, width = width)) | ||
} | ||
|
||
# Assumes that each set has the same horizontal position | ||
stack <- function(df, width) { | ||
n <- nrow(df) + 1 | ||
y <- with(df, ifelse(is.na(y), 0, y)) | ||
heights <- c(0, cumsum(y)) | ||
|
||
within(df, { | ||
ymin <- heights[-n] | ||
ymax <- heights[-1] | ||
}) | ||
} | ||
|
||
fill <- function(df, width) { | ||
within(stack(df, width), { | ||
ymin <- ymin / max(ymax) | ||
ymax <- ymax / max(ymax) | ||
}) | ||
} | ||
|
||
# Assumes that each set has the same horizontal position | ||
dodge <- function(df, width) { | ||
n <- nrow(df) | ||
|
||
within(df, { | ||
xmin <- xmin + width / n * seq_len(n) | ||
xmax <- xmin + width / n | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
PositionFill <- proto(Position, { | ||
adjust <- function(., data, scales) { | ||
if (is.null(data)) return() | ||
|
||
y <- scales$get_scales("y") | ||
y$limits <- c(0, 1) | ||
|
||
check_required_aesthetics(c("x", "ymax"), names(data), "position_fill") | ||
if (!all(data$ymin == 0)) warning("Filling not well defined when ymin != 0") | ||
collide(data, .$width, .$my_name(), fill) | ||
} | ||
|
||
objname <- "fill" | ||
desc <- "Stack overlapping objects on top of one another, and standardise have equal height" | ||
|
||
icon <- function(.) { | ||
y <- c(0.5, 0.8) | ||
rectGrob(0.5, c(0.625, 1), width=0.4, height=c(0.625, 0.375), gp=gpar(col="grey60", fill=c("#804070", "#668040")), vjust=1) | ||
} | ||
|
||
|
||
examples <- function(.) { | ||
# See ?geom_bar and ?geom_area for more examples | ||
ggplot(mtcars, aes(x=factor(cyl), fill=factor(vs))) + geom_bar(position="fill") | ||
|
||
cde <- geom_histogram(position="fill", binwidth = 500) | ||
|
||
ggplot(diamonds, aes(x=price)) + cde | ||
ggplot(diamonds, aes(x=price, fill=cut)) + cde | ||
ggplot(diamonds, aes(x=price, fill=clarity)) + cde | ||
ggplot(diamonds, aes(x=price, fill=color)) + cde | ||
} | ||
|
||
|
||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters