Данные подмножества в пользовательском расширении `ggplot2` в зависимости от типа - PullRequest
1 голос
/ 01 апреля 2019

Я работаю над расширением 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.Теперь я хочу установить подмножество данных внутри моих Geoms и Stats на основе типа.

Рассмотрим следующий пример (с использованием 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))

enter image description here


TL; DR :

  • Как я могу изменить свои GeomVar и / или StatVar, чтобы NA не делалиВы больше не можете строить графики?

  • Или: Как я могу поднастроить мои data на основе type в моих GeomVar и StatVar функциях?

(я пробовал data <- data[data$type == "text", ] практически в каждом месте data встречается в функциях GeomVar, geom_var, StatVar и stat_var ..)

1 Ответ

1 голос
/ 01 апреля 2019

Вы можете передать тип как эстетику в geom_var(aes(...)), и указать функцию setup_data для вашего StatVar, чтобы следить за ним:

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

plot

...