Reputation: 3039
I'd like to make an overlay of several polygons with ggplot. The overlay's fill should be transparent but their borders should be red. I want to see only the fill of the first polygon so I thought to make the overlays transparent...but I can't get them fully transparent. Somehow I would be easier to just define the fill color as no-fill...but I don't know how to do it. Any ideas?
Here is some code to reproduce an example:
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
values <- data.frame(
id = ids,
value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5)
)
positions <- data.frame(
id = rep(ids, each = 4),
x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)
datapoly <- merge(values, positions, by=c("id"))
p <- ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id))
# overlay the same plot with red borders and transparent fill
p <- p + geom_polygon(aes(group=id, alpha=1),colour="red",size=1.1)
p
Upvotes: 4
Views: 6425
Reputation: 58825
If you want the second set of geom_polygon
to not be filled in, just set the fill
to NA
.
ggplot(datapoly, aes(x=x, y=y)) +
geom_polygon(aes(fill=value, group=id)) +
geom_polygon(aes(group=id), alpha=1,colour="red", fill=NA, size=1.1)
In this case, you don't need two geom_polygon
calls, though
ggplot(datapoly, aes(x=x, y=y)) +
geom_polygon(aes(fill=value, group=id), colour="red", size=1.1)
Upvotes: 6