Skip to content

Commit

Permalink
Fix grouping error in geom_crossbar
Browse files Browse the repository at this point in the history
Also bump up fatten to make middles more obvious. Fixes #1125
  • Loading branch information
hadley committed Jun 15, 2015
1 parent a6e045e commit 069abcb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
4 changes: 4 additions & 0 deletions NEWS
@@ -1,6 +1,10 @@
ggplot2 1.0.1.9000
----------------------------------------------------------------

* `geom_crossbar()` sets grouping correctly so you can display multiple
crossbars on one plot. It also makes the default `fatten` argument a little
bigger to make the middle line more obvious (#1125).

* `guide_colorbar()` no longer fails when the legend is empty - previously
this often masked misspecifications elsewhere in the plot (#967)

Expand Down
50 changes: 31 additions & 19 deletions R/geom-crossbar.r
Expand Up @@ -12,8 +12,8 @@
#' @export
#' @examples
#' # See geom_linerange for examples
geom_crossbar <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
fatten = 2, ...) {
geom_crossbar <- function(mapping = NULL, data = NULL, stat = "identity",
position = "identity", fatten = 2.5, ...) {
GeomCrossbar$new(mapping = mapping, data = data, stat = stat,
position = position, fatten = fatten, ...)
}
Expand All @@ -39,7 +39,7 @@ GeomCrossbar <- proto(Geom, {
))
}

draw <- function(., data, scales, coordinates, fatten = 2, width = NULL, ...) {
draw <- function(., data, scales, coordinates, fatten = 2.5, width = NULL, ...) {
middle <- transform(data, x = xmin, xend = xmax, yend = y, size = size * fatten, alpha = NA)

has_notch <- !is.null(data$ynotchlower) && !is.null(data$ynotchupper) &&
Expand All @@ -55,27 +55,39 @@ GeomCrossbar <- proto(Geom, {
middle$xend <- middle$xend - notchindent

box <- data.frame(
x = c(data$xmin, data$xmin, data$xmin + notchindent, data$xmin, data$xmin,
data$xmax, data$xmax, data$xmax - notchindent, data$xmax, data$xmax,
data$xmin),
y = c(data$ymax, data$ynotchupper, data$y, data$ynotchlower, data$ymin,
data$ymin, data$ynotchlower, data$y, data$ynotchupper, data$ymax,
data$ymax),
alpha = data$alpha, colour = data$colour, size = data$size,
linetype = data$linetype, fill = data$fill, group = data$group,
stringsAsFactors = FALSE)

x = c(
data$xmin, data$xmin, data$xmin + notchindent, data$xmin, data$xmin,
data$xmax, data$xmax, data$xmax - notchindent, data$xmax, data$xmax,
data$xmin
),
y = c(
data$ymax, data$ynotchupper, data$y, data$ynotchlower, data$ymin,
data$ymin, data$ynotchlower, data$y, data$ynotchupper, data$ymax,
data$ymax
),
alpha = data$alpha,
colour = data$colour,
size = data$size,
linetype = data$linetype, fill = data$fill,
group = seq_len(nrow(data)),
stringsAsFactors = FALSE
)
} else {
# No notch
box <- data.frame(
x = c(data$xmin, data$xmin, data$xmax, data$xmax, data$xmin),
y = c(data$ymax, data$ymin, data$ymin, data$ymax, data$ymax),
alpha = data$alpha, colour = data$colour, size = data$size,
linetype = data$linetype, fill = data$fill, group = data$group,
stringsAsFactors = FALSE)
x = c(data$xmin, data$xmin, data$xmax, data$xmax, data$xmin),
y = c(data$ymax, data$ymin, data$ymin, data$ymax, data$ymax),
alpha = data$alpha,
colour = data$colour,
size = data$size,
linetype = data$linetype,
fill = data$fill,
group = seq_len(nrow(data)), # each bar forms it's own group
stringsAsFactors = FALSE
)
}

ggname(.$my_name(), gTree(children=gList(
ggname(.$my_name(), gTree(children = gList(
GeomPolygon$draw(box, scales, coordinates, ...),
GeomSegment$draw(middle, scales, coordinates, ...)
)))
Expand Down
2 changes: 1 addition & 1 deletion man/geom_crossbar.Rd
Expand Up @@ -5,7 +5,7 @@
\title{Hollow bar with middle indicated by horizontal line.}
\usage{
geom_crossbar(mapping = NULL, data = NULL, stat = "identity",
position = "identity", fatten = 2, ...)
position = "identity", fatten = 2.5, ...)
}
\arguments{
\item{mapping}{The aesthetic mapping, usually constructed with
Expand Down

0 comments on commit 069abcb

Please sign in to comment.