Skip to content

Commit a916e7d

Browse files
committed
Create geom_rect
* move common code out of geom_bar and geom_tile * clarify role of reparameterise * make zero in resolution optional
1 parent d2a6c1e commit a916e7d

File tree

5 files changed

+88
-62
lines changed

5 files changed

+88
-62
lines changed

R/geom-bar-.r

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,19 @@ GeomBar <- proto(Geom, {
44
default_pos <- function(.) PositionStack
55
default_aes <- function(.) aes(colour=NA, fill="grey60", size=1, linetype=1, width = resolution(x) * 0.9)
66

7-
required_aes <- c("x")
7+
required_aes <- c("x", "width")
88

99
reparameterise <- function(., df) {
10+
if (is.null(df$width)) df$width <- resolution(df$x, FALSE) * 0.9
11+
1012
transform(df,
11-
ymin = 0,
12-
ymax = y,
13-
xmin = x - width / 2,
14-
xmax = x + width / 2,
15-
width = NULL
13+
ymin = 0, ymax = y,
14+
xmin = x - width / 2, xmax = x + width / 2, width = NULL
1615
)
1716
}
1817

1918
draw <- function(., data, scales, coordinates, ...) {
20-
if (coordinates$muncher()) {
21-
data <- transform(data, top=max, bottom=min, left=x - width/2, right=x + width/2)
22-
ggname("bar",gTree(children=do.call("gList", lapply(1:nrow(data), function(i) {
23-
data <- data[i, ]
24-
df <- cbind(with(data, rbind(
25-
cbind(y=top, x=left),
26-
cbind(y=top, x=right),
27-
cbind(y=bottom, x=right),
28-
cbind(y=bottom, x=left),
29-
cbind(y=top, x=left)
30-
)), data[rep(1,5), setdiff(names(.$default_aes()), c("min","max"))])
31-
GeomPolygon$draw(df, scales, coordinates)
32-
}))))
33-
} else {
34-
with(coordinates$transform(data),
35-
ggname(.$my_name(), rectGrob(xmin, ymax, width=xmax-xmin, height=ymax-ymin, default.units="native", just=c("left", "top"),
36-
gp=gpar(col=colour, fill=fill, lwd=size * .pt, lty=linetype, lineend="butt"))
37-
))
38-
}
39-
19+
GeomRect$draw(data, scales, coordinates, ...)
4020
}
4121

4222
# Documentation -----------------------------------------------

R/geom-rect.r

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
GeomRect <- proto(Geom, {
2+
3+
default_stat <- function(.) StatIdentity
4+
default_pos <- function(.) PositionIdentity
5+
default_aes <- function(.) aes(colour=NA, fill="grey60", size=1, linetype=1)
6+
7+
required_aes <- c("xmin", "xmax", "ymin", "ymax")
8+
9+
draw <- function(., data, scales, coordinates, ...) {
10+
if (coordinates$muncher()) {
11+
aesthetics <- setdiff(names(data), c("xmin","xmax", "ymin", "ymax"))
12+
13+
polys <- alply(data, 1, function(row) {
14+
poly <- with(row[i, ], rect_to_poly(xmin, xmax, ymin, ymax))
15+
aes <- row[rep(1,5), aesthetics]
16+
17+
GeomPolygon$draw(cbind(poly, aesthetics), scales, coordinates)
18+
})
19+
20+
ggname("bar",do.call("grobTree", polys))
21+
} else {
22+
with(coordinates$transform(data),
23+
ggname(.$my_name(), rectGrob(
24+
xmin, ymax,
25+
width = xmax - xmin, height = ymax - ymin,
26+
default.units = "native", just = c("left", "top"),
27+
gp=gpar(
28+
col=colour, fill=fill,
29+
lwd=size * .pt, lty=linetype, lineend="butt"
30+
)
31+
))
32+
)
33+
}
34+
35+
}
36+
37+
# Documentation -----------------------------------------------
38+
objname <- "rect"
39+
desc <- "2d rectangles"
40+
guide_geom <- function(.) "tile"
41+
42+
icon <- function(.) {
43+
rectGrob(c(0.3, 0.7), c(0.4, 0.8), height=c(0.4, 0.8), width=0.3, vjust=1, gp=gpar(fill="grey60", col=NA))
44+
}
45+
46+
examples <- function(.) {
47+
df <- data.frame(
48+
x = sample(10, 20, replace = TRUE),
49+
y = sample(10, 20, replace = TRUE)
50+
)
51+
ggplot(df, aes(xmin = x, xmax = x + 1, ymin = y, ymax = y + 2)) +
52+
geom_rect()
53+
}
54+
55+
})
56+
57+
rect_to_poly <- function(xmin, xmax, ymin, ymax) {
58+
data.frame(
59+
y = c(ymax, ymax, ymin, ymin, ymax),
60+
x = c(xmin, xmax, xmax, xmin, xmin)
61+
)
62+
}

R/geom-tile.r

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
11
GeomTile <- proto(Geom, {
2-
draw_groups <- function(., ...) .$draw(...)
3-
draw <- function(., data, scales, coordinates, ...) {
4-
if (nrow(data) == 1) return(NULL)
5-
data$colour[is.na(data$colour)] <- data$fill[is.na(data$colour)]
6-
7-
data <- transform(data,
8-
xmin = x - width/2,
9-
xmax = x + width/2,
10-
ymin = y - height/2,
11-
ymax = y + height/2
12-
)
13-
if (coordinates$muncher()) {
14-
data <- transform(data, top=y + height/2, bottom= y - height/2, left=x - width/2, right=x + width/2)
15-
ggname(.$my_name(), gTree(children=do.call("gList", lapply(1:nrow(data), function(i) {
16-
data <- data[i, ]
17-
df <- cbind(with(data, rbind(
18-
cbind(y = top, x=left),
19-
cbind(y = top, x=right),
20-
cbind(y = bottom, x=right),
21-
cbind(y = bottom, x=left)
22-
)), data[rep(1,4), names(.$default_aes())])
23-
GeomPolygon$draw(df, scales, coordinates)
24-
}))))
25-
} else {
26-
with(coordinates$transform(data),
27-
ggname(.$my_name(), rectGrob(
28-
xmin, ymax,
29-
width=(xmax-xmin) * size, height=(ymax-ymin) * size,
30-
default.units="native", just=c("left","top"),
31-
gp=gpar(col=colour, fill=fill))
32-
)
2+
reparameterise <- function(., df) {
3+
if (is.null(df$width)) df$width <- resolution(df$x, FALSE)
4+
if (is.null(df$height)) df$height <- resolution(df$y, FALSE)
5+
6+
transform(df,
7+
xmin = x - width/2, xmax = x + width/2, width = NULL,
8+
ymin = y - height/2, ymax = y + height/2, height = NULL
339
)
34-
}
10+
}
11+
12+
draw_groups <- function(., data, scales, coordinates, ...) {
13+
data$colour[is.na(data$colour)] <- data$fill[is.na(data$colour)]
14+
GeomRect$draw(data, scales, coordinates, ...)
3515
}
3616

3717
draw_legend <- function(., data, ...) {
3818
data <- aesdefaults(data, .$default_aes(), list(...))
39-
4019
rectGrob(gp=gpar(col=NA, fill=data$fill))
4120
}
4221

@@ -50,7 +29,7 @@ GeomTile <- proto(Geom, {
5029
}
5130

5231
default_stat <- function(.) StatIdentity
53-
default_aes <- function(.) aes(fill="grey50", colour=NA, width = resolution(x), height = resolution(y), size=1, linetype=1)
32+
default_aes <- function(.) aes(fill="grey50", colour=NA, width = resolution(x), height = resolution(y), size=0.1, linetype=1)
5433
required_aes <- c("x", "y")
5534
guide_geom <- function(.) "tile"
5635

R/utilities-geoms.r

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
# @arguments numeric vector
88
# @keyword hplot
99
# @keyword internal
10-
resolution <- function(x) {
11-
un <- unique(c(0, as.numeric(x)))
10+
resolution <- function(x, zero = TRUE) {
11+
if (zero) {
12+
un <- unique(c(0, as.numeric(x)))
13+
} else {
14+
un <- unique(as.numeric(x))
15+
}
1216

1317
if (length(un) == 1) return(1)
1418
min(diff(sort(un)))

R/xxx.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ geom_point <- GeomPoint$build_accessor()
2525
geom_pointrange <- GeomPointrange$build_accessor()
2626
geom_polygon <- GeomPolygon$build_accessor()
2727
geom_quantile <- GeomQuantile$build_accessor()
28+
geom_rect <- GeomRect$build_accessor()
2829
geom_ribbon <- GeomRibbon$build_accessor()
2930
geom_rug <- GeomRug$build_accessor()
3031
geom_segment <- GeomSegment$build_accessor()

0 commit comments

Comments
 (0)