r - How do you relate ggplot2 grobs back to the data? -



r - How do you relate ggplot2 grobs back to the data? -

given ggplot of, example, points, how find out row of info given point corresponded to?

a sample plot:

library(ggplot2) (p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~ gear) )

we can grobs contain points grid.ls + grid.get.

grob_names <- grid.ls(print = false)$name point_grob_names <- grob_names[grepl("point", grob_names)] point_grobs <- lapply(point_grob_names, grid.get)

this lastly variable contains details of x-y coordinates, , pointsize, etc. (try unclass(point_grobs[[1]])), isn't obvious how row of info in mtcars each point corresponds to.

to reply kohske's question why doing this, i'm using gridsvg create interactive scatterplot. when roll mouse on point, want display contextual information. in mtcars example, show tooltip name of auto or other values row of info frame.

my hacky thought far include id column invisible text label:

mtcars$id <- seq_len(nrow(mtcars)) p + geom_text(aes(label = id), colour = na)

then traverse tree of grobs point grob text grob, , display row of dataset indexed label.

this fiddly , not generalisable. if there's way store id value within point grob, much cleaner.

this script generate svg file wherein can interactively annotate points.

library(ggplot2) library(gridsvg) geom_point2 <- function (...) geompoint2$new(...) geompoint2 <- proto(geompoint, { objname <- "point2" draw <- function(., data, scales, coordinates, na.rm = false, ...) { info <- remove_missing(data, na.rm, c("x", "y", "size", "shape"), name = "geom_point") if (empty(data)) return(zerogrob()) name <- paste(.$my_name(), data$panel[1], sep = ".") with(coordinates$transform(data, scales), ggname(name, pointsgrob(x, y, size = unit(size, "mm"), pch = shape, gp = gpar(col = alpha(colour, alpha), fill = fill, label = label, fontsize = size * .pt)))) }} ) p <- ggplot(mtcars, aes(mpg, wt, label = rownames(mtcars))) + geom_point2() + facet_wrap(~ gear) print(p) grob_names <- grid.ls(print = false)$name point_grob_names <- sort(grob_names[grepl("point", grob_names)]) point_grobs_labels <- lapply(point_grob_names, function(x) grid.get(x)$gp$label) library(rjson) jlabel <- tojson(point_grobs_labels) grid.text("value", 0.05, 0.05, = c(0, 0), name = "text_place", gp = gpar(col = "red")) script <- ' var txt = null; function f() { var id = this.id.match(/geom_point2.([0-9]+)\\.points.*\\.([0-9]+)$/); txt.textcontent = label[id[1]-1][id[2]-1]; } window.addeventlistener("load",function(){ var es = document.getelementsbytagname("circle"); (i=0; i<es.length; ++i) es[i].addeventlistener("mouseover", f, false); txt = (document.getelementbyid("text_place").getelementsbytagname("tspan"))[0]; },false); ' grid.script(script = script) grid.script(script = paste("var label = ", jlabel)) gridtosvg()

do know places can upload svg file?

r ggplot2 r-grid

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -