Overlay polygons with ggplot2 and create transparent overlay
I would like to do a multi-polygon overlay with ggplot. The overlay fill should be transparent, but their borders should be red. I only want to see the fill of the first polygon, so I decided to make the overlays transparent ... but I can't get them completely transparent. Somehow I would find it easier to just define the fill color as not filled ... but I don't know how to do that. Any ideas?
Here are some examples to reproduce the 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
source to share
If you want the second set geom_polygon
to be empty, just set 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 calls geom_polygon
though
ggplot(datapoly, aes(x=x, y=y)) +
geom_polygon(aes(fill=value, group=id), colour="red", size=1.1)
source to share