LoginSignup
0
0

More than 5 years have passed since last update.

geom_holygon in ggplot2 2.0

Posted at

その昔、
http://blogs.yahoo.co.jp/igproj_fusion/16633531.html
からの、
http://rpubs.com/kohske/3522
からの、
http://blogs.yahoo.co.jp/igproj_fusion/17011933.html
ということで、穴の開くpolygonを描画するカスタムgeomを作った。

最近、ggplot2 2.0になって動かなくなったんですけど、という問い合わせが来て、
2.0対応のcustom geomを書いた。

最初に定義する。
First, define GeomHolygon and geom_holygon

library(ggplot2)
library(proto)
library(grid)

GeomHolygon <- ggproto(
  "GeomHolygon", 
  GeomPolygon,
  extra_params = c("na.rm", "rule"),
  draw_panel = function(data, scales, coordinates, rule) {
    n <- nrow(data)
    if (n == 1) 
      return(zeroGrob())

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

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

    ggplot2:::ggname(
      "geom_holygon", 
      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)))
  }
)

geom_holygon <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", 
                          na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, rule = "winding", ...) {
  ggplot2::layer(data = data, mapping = mapping, stat = stat, geom = GeomHolygon, 
                 position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
                 params = list(na.rm = na.rm , rule = rule, ...))
}

で、
then,

ggplot(p524072p_df, aes(long, lat, group = group)) + geom_holygon(fill = "skyblue") + coord_map()

Enjoy!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0