1

我正在开发一个看起来有点像的ggplot2扩展:data.frames

data <- data.frame(
  type = c("text", "text", "line", "line"),
  label = c("some label", "another one", NA, NA),
  x = c(0,10,2,4),
  y = c(0,10,3,7),
  xend = c(NA, NA, 8, 10),
  yend = c(NA, NA, 3, 4)
)

这意味着我们有不同的对象(行)type。现在我想根据类型对我内部的数据进行子集化。GeomsStats

考虑以下示例(使用ggplot2标准函数):

library(ggplot2)
ggplot(data, aes(x, y)) + 
  geom_text(aes(label = label)) + 
  geom_segment(aes(xend = xend, yend = yend))

这绘制了人们所期望的(文本作为文本,作为段)。

现在我有我自己的geom_text被调用版本geom_var

GeomVar <- ggproto("GeomVar", ggplot2::GeomText,
                   default_aes = aes(x = x, y = y, label = label, colour = "black", 
                                     size = 4, angle = 0, hjust = 0.5, vjust = 0.5, 
                                     alpha = NA, family = "Arial", fontface = 1, 
                                     lineheight = 1.2, length = 10)
)
geom_var <- function(mapping = aes(label = label), data = NULL, position = "identity", 
                     ..., parse = FALSE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE, 
                     na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) 
{
  ggplot2::layer(data = data, mapping = mapping, stat = StatVar, geom = GeomVar, 
                 position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
                 params = list(parse = parse, check_overlap = check_overlap, 
                               na.rm = na.rm, ...))
}

StatVar <- ggproto("StatVar", ggplot2::Stat,
                   compute_group = function(data, scales, length = 5) {
                     data$label <- sapply(data$label, 
                                          function(x) {paste0(strwrap(x, width = length), 
                                                              collapse = "\n")})
                     data
                   }
)

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

当我使用自己的版本时,情节看起来像:

ggplot(data, aes(x, y)) + 
  geom_var(aes(label = label)) + 
  geom_segment(aes(xend = xend, yend = yend))

在此处输入图像描述


TL;博士

  • 如何更改我的GeomVar和/或StatVar以使NA不再被绘制?

  • 或者:我如何data根据typemyGeomVarStatVar函数对 my 进行子集化?

(我尝试data <- data[data$type == "text", ]过基本上每个地方都data出现在GeomVar,geom_var和函数StatVarstat_var..)

4

1 回答 1

1

您可以将 type 作为审美传递 in geom_var(aes(...)),并指定setup_dataStatVar要注意的函数:

# define setup_data in StatVar
StatVar <- ggproto("StatVar", 
                   ggplot2::Stat,
                   setup_data = function(data, params){
                     # print(data) # I like doing this while debugging code to see what data
                                   # actually looks like at this point
                     data <- data[data$type == "text", ]
                   },
                   compute_group = function(data, scales, length = 5) {
                     data$label <- sapply(data$label, 
                                          function(x) {paste0(strwrap(x, width = length), 
                                                              collapse = "\n")})
                     data
                   }
)

# include type as a required aesthetic mapping in GeomVar
GeomVar <- ggproto("GeomVar", ggplot2::GeomText,
                   required_aes = c("x", "y", "label", "type"),
                   default_aes = aes(x = x, y = y, label = label, colour = "black", 
                                     size = 4, angle = 0, hjust = 0.5, vjust = 0.5, 
                                     alpha = NA, family = "Arial", fontface = 1, 
                                     lineheight = 1.2, length = 10))

# map type in geom_var
ggplot(data, aes(x, y)) + 
  geom_var(aes(label = label, type = type)) + 
  geom_segment(aes(xend = xend, yend = yend))

阴谋

于 2019-04-01T14:51:37.230 回答