Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting Islands in ggplot2

Tags:

r

ggplot2

I'm trying to plot bodies of water on my map and struggling with islands in ggplot2. I understand the right/left-hand rule for exterior/interior rings but there is still a problem going from island to island. The question is how do I plot a polygon with lots of holes/islands in ggplot2? I believe the trick is order, but what order?

Here is the MWE I have built to try and understand and fix the problem:

library(ggplot2)

ids <- letters[1]

# IDs and values to use for fill colour
values <- data.frame(
  id = ids,
  value = c(5)
)

# Example of good polygon and holes
good_positions <- data.frame(
  id = rep(ids, each = 5),
  #     shape        hole       hole       hole
  x = c(1,10,10,1,1, 2,2,3,3,2, 7,7,8,8,7, 5,5,6,6,5 ),
  y = c(1,1,10,10,1, 2,3,3,2,2, 7,8,8,7,7, 5,6,6,5,5)
)

# Example of good polygon and holes
bad_positions <- data.frame(
  id = rep(ids, each = 5),
  #     shape        hole       hole       hole
  x = c(1,10,10,1,1, 2,2,3,3,2, 7,7,8,8,7, 5,5,6,6,5 ),
  y = c(1,1,10,10,1, 2,3,3,2,2, 7,8,8,7,7, 3,4,4,3,3)
)


# Merge positions and values
good_datapoly <- merge(values, good_positions, by=c("id"))
bad_datapoly <- merge(values, bad_positions, by=c("id"))

# Plot polygons
good_plot <- ggplot(good_datapoly, aes(x=x, y=y)) + 
  geom_polygon(aes(group=id, fill=factor(value))) +
  scale_fill_discrete("Key")

bad_plot <- ggplot(bad_datapoly, aes(x=x, y=y)) + 
  geom_polygon(aes(group=id, fill=factor(value))) +
  scale_fill_discrete("Key")

good_plot
bad_plot

good_plot bad_plot

like image 382
Phil Donovan Avatar asked Mar 13 '26 21:03

Phil Donovan


1 Answers

As I commented above, I do not know the reason why we see the discrepancy between the good and bad examples. But, this approach with geom_holygon works for the bad position data.

library(proto)
library(ggplot2)

bad_positions <- data.frame(id = rep(ids, each = 5),
                        x = c(1,10,10,1,1, 2,2,3,3,2, 7,7,8,8,7, 5,5,6,6,5 ),
                        y = c(1,1,10,10,1, 2,3,3,2,2, 7,8,8,7,7, 3,4,4,3,3)
                        )


### Assign unique ids to polygons
bad_positions$id <- rep(c(1,2,3,4), each = 5)

### geom_holygon
### http://rstudio-pubs-static.s3.amazonaws.com/3522_52420d28ca7d443eae79850822ead5b8.html

geom_holygon <- function(mapping = NULL, data = NULL, stat = "identity", position = "identity", 
rule = "winding", ...) {
GeomHolygon$new(mapping = mapping, data = data, stat = stat, position = position, 
    rule = rule, ...)
}

GeomHolygon <- proto(ggplot2:::GeomPolygon, {
objname <- "holygon"

draw <- function(., data, scales, coordinates, rule, ...) {
    n <- nrow(data)
    if (n == 1) 
        return()

    munched <- coord_munch(coordinates, data, scales)
    munched <- munched[order(munched$group), ]

    first_idx <- !duplicated(munched$group)
    first_rows <- munched[first_idx, ]

    ggname(.$my_name(), gTree(children = gList(pathGrob(munched$x, munched$y, 
        default.units = "native", id = munched$group, rule = rule, gp = gpar(col = first_rows$colour, 
            fill = alpha(first_rows$fill, first_rows$alpha), lwd = first_rows$size * 
              .pt, lty = first_rows$linetype)))))
}
})

### Draw the figure
ggplot() +
geom_holygon(bad_positions, mapping = aes(x = x, y = y, group = id), fill = "skyblue") 

enter image description here

like image 53
jazzurro Avatar answered Mar 15 '26 11:03

jazzurro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!