簡體   English   中英

在ggplot2中繪制島嶼

[英]Plotting Islands in ggplot2

我正試圖在我的地圖上繪制水體,並在ggplot2中與島嶼作斗爭。 我理解外部/內部環的右/左手規則但是從島到島仍然存在問題。 問題是如何在ggplot2中繪制具有大量孔/島的多邊形? 我相信訣竅是秩序,但是順序是什么?

這是我為了嘗試理解和解決問題而構建的MWE:

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_plotbad_plot

正如我上面評論的那樣,我不知道為什么我們看到好的和壞的例子之間的差異。 但是,這種使用geom_holygon方法適用於不良位置數據。

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") 

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM