Skip to content

Commit 576fd23

Browse files
committed
Merge pull request #771 from wch/fix-polygon
Fix polygon grouping bug
2 parents c351ad6 + c4c3381 commit 576fd23

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ BUG FIXES
66
* The theme element `legend.box.just` now can be set. It was not properly
77
recognized before.
88

9+
* `stat_density2d` previously resulted in errors when geom="polygon". This
10+
is fixed. (Fixes #741 and #749)
11+
912
ggplot2 0.9.3
1013
----------------------------------------------------------------
1114

R/geom-polygon.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ GeomPolygon <- proto(Geom, {
5858
draw <- function(., data, scales, coordinates, ...) {
5959
n <- nrow(data)
6060
if (n == 1) return()
61+
62+
# Check if group is numeric, to make polygonGrob happy (factors are numeric,
63+
# but is.numeric() will report FALSE because it actually checks something else)
64+
if (mode(data$group) != "numeric")
65+
data$group <- factor(data$group)
6166

6267
munched <- coord_munch(coordinates, data, scales)
6368
# Sort by group to make sure that colors, fill, etc. come in same order

R/stat-contour.r

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ contour_lines <- function(data, breaks, complete = FALSE) {
111111
xs <- unlist(lapply(cl, "[[", "x"), use.names = FALSE)
112112
ys <- unlist(lapply(cl, "[[", "y"), use.names = FALSE)
113113
pieces <- rep(seq_along(cl), lengths)
114-
114+
# Add leading zeros so that groups can be properly sorted later
115+
groups <- paste(data$group[1], sprintf("%03d", pieces), sep = "-")
116+
115117
data.frame(
116118
level = rep(levels, lengths),
117119
x = xs,
118120
y = ys,
119121
piece = pieces,
120-
group = paste(data$group[1], pieces, sep = "-")
122+
group = groups
121123
)
122124
}
123125

visual_test/geom-polygon.r

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
vcontext("geom-polygon")
2+
3+
ggplot(faithful, aes(x = eruptions, y = waiting)) +
4+
stat_density2d(aes(colour = ..level..), geom="path") +
5+
xlim(0.5, 6) + ylim(40, 110)
6+
save_vtest("stat_density2d with paths")
7+
8+
ggplot(faithful, aes(x = eruptions, y = waiting)) +
9+
stat_density2d(aes(fill = ..level..), geom="polygon", colour="white") +
10+
xlim(0.5, 6) + ylim(40, 110)
11+
save_vtest("stat_density2d with filled polygons")
12+
13+
end_vcontext()

0 commit comments

Comments
 (0)