繁体   English   中英

制作简单的自定义几何图形 - ggplot2 中 geom_rect() 的变体

[英]Making simple custom geom - variation of geom_rect() in ggplot2

我正在尝试制作一个简单的自定义几何图形,它基本上会在 ggplot 面板周围放置一个“框架”。 我知道这也是可以在主题内做的,但我可能会喜欢 map 其他美学,比如颜色等等。 我想要的是简单地修改geom_rect,以便默认情况下它只有Inf和-Inf的xmin,xmax,ymin和ymax值,NA填充,以及黑色或灰色20之类的颜色。

但是,我尝试按照此处的一些代码示例来更新现有的几何图形,但这似乎不起作用。 它不会抛出错误,但它根本不会 plot 任何东西。

library(tidyverse)

GeomFrame <- ggproto("GeomFrame",
                     GeomRect,
                     default_aes = aes(colour = "grey20",
                                       fill = NA,
                                       size = 1,
                                       linetype = 1,
                                       xmin = -Inf,
                                       xmax = Inf,
                                       ymin = -Inf,
                                       ymax = Inf)
)

geom_frame <- function(mapping = NULL, data = NULL, stat = "identity", position = "identity", 
                       ..., linejoin = "mitre", na.rm = FALSE, show.legend = NA, 
                       inherit.aes = TRUE) {
  layer(
    data = data, mapping = mapping, stat = stat, geom = GeomFrame, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
    params = list(linejoin = linejoin, na.rm = na.rm, ...)
  )
}

ggplot() +
  scale_x_continuous(limits = c(1, 10)) +
  scale_y_continuous(limits = c(1, 10)) +
  geom_frame() 

你应该看到这似乎没有做任何事情。 我希望它看起来像这样:

ggplot() +
  scale_x_continuous(limits = c(1, 10)) +
  scale_y_continuous(limits = c(1, 10)) +
  geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf), fill = NA, color = "black", size = 1) 

对此的任何帮助都会很棒!

您的 ggproto object 可能如下所示:

GeomFrame <- ggproto("GeomFrame", GeomRect,
 default_aes = aes(colour = "grey20",
                   fill = NA,
                   size = 1,
                   linetype = 1,
                   alpha = 1,
                   xmin = -Inf, 
                   xmax = Inf, 
                   ymin = -Inf, 
                   ymax = Inf),
 
 required_aes = NULL
)

您实际geom_frame function 作为注释类型的 geom 可能更有意义,但只要您给它一些默认数据和美学映射,它就应该满足您的需求:

geom_frame <- function(mapping = NULL, data = NULL, stat = "identity", 
                       position = "identity", 
                       ..., linejoin = "mitre", na.rm = FALSE, show.legend = NA, 
                       inherit.aes = FALSE) {
  if(is.null(data)) {
    data <- data.frame(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
    mapping <- structure(c(mapping, 
                 aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)),
                 class = "uneval")
  }
  
  layer(
    data = data, mapping = mapping, stat = stat, geom = GeomFrame, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
    params = list(linejoin = linejoin, na.rm = na.rm, ...)
  )
}

例如

ggplot() +
  scale_x_continuous(limits = c(1, 10)) +
  scale_y_continuous(limits = c(1, 10)) +
  geom_frame()

在此处输入图像描述

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + 
  geom_point() +
  geom_frame(size = 3, color = "red4") 

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM