Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:24:50+00:00 2026-06-07T20:24:50+00:00

I’d like to label points in a ggplot interactively, so that mousing over a

  • 0

I’d like to label points in a ggplot interactively, so that mousing over a point shows a label.

I’m trying to adapt the answer given in this question so that it works in the latest version of ggplot2. Influenced by comments on the ggplot google group, here, I used the latest version of geom-point-.r as a template, adding a “label” field to the gp argument in various places. Then I copied the remaining code from kohske’s answer. But it doesn’t work — there aren’t any labels in the resulting svg, and I can’t figure out why.

I did notice that everything in point_grobs_labels is null, and when I look at grid.get(point_grob_names[1])$gp, there is no label field…

library(ggplot2)
library(gridSVG)
library(proto)
library(rjson)

geom_point2 <- function (mapping = NULL, data = NULL, stat = "identity", 
                         position = "identity",
                         na.rm = FALSE, ...) {
  ggplot2:::GeomPoint$new(mapping = mapping, data = data, stat = stat, 
                          position = position, 
                          na.rm = na.rm, ...)
}

GeomPoint2 <- proto(ggplot2:::Geom, {
  objname <- "point"

  draw_groups <- function(., ...) .$draw(...)
  draw <- function(., data, scales, coordinates, na.rm = FALSE, ...) {    
    data <- remove_missing(data, na.rm, 
                           c("x", "y", "size", "shape"), name = "geom_point")
    if (empty(data)) return(zeroGrob())

    with(coord_transform(coordinates, data, scales), 
         ggname(.$my_name(), pointsGrob(x, y, size=unit(size, "mm"), pch=shape, 
                                        gp=gpar(
                                          col=alpha(colour, alpha),
                                          fill = alpha(fill, alpha),  
                                          label = label, 
                                          fontsize = size * .pt)))
    )
  }

  draw_legend <- function(., data, ...) {
    data <- aesdefaults(data, .$default_aes(), list(...))

    with(data,
         pointsGrob(0.5, 0.5, size=unit(size, "mm"), pch=shape, 
                    gp=gpar(
                      col = alpha(colour, alpha), 
                      fill = alpha(fill, alpha), 
                      label = label,
                      fontsize = size * .pt)
         )
    )
  }

  default_stat <- function(.) StatIdentity
  required_aes <- c("x", "y")
  default_aes <- function(.) aes(shape=16, colour="black", size=2, 
                                 fill = NA, alpha = NA, label = NA)

})

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)  

jlabel <- toJSON(point_grobs_labels)

grid.text("value", 0.05, 0.05, just = 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");
for (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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:24:51+00:00Added an answer on June 7, 2026 at 8:24 pm

    Try this:

    library(ggplot2)
    library(gridSVG)
    library(proto)
    library(rjson)
    mtcars2 <- data.frame(mtcars, names = rownames(mtcars))
    
    geom_point2 <- function (...) {
      GeomPoint2$new(...)
    }
    
    GeomPoint2 <- proto(ggplot2:::Geom, {
      objname <- "point"
    
      draw_groups <- function(., ...) .$draw(...)
      draw <- function(., data, scales, coordinates, na.rm = FALSE, ...) {    
        data <- 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(coord_transform(coordinates, data, scales), 
             ggname(name, pointsGrob(x, y, size=unit(size, "mm"), pch=shape, 
                                            gp=gpar(
                                              col=alpha(colour, alpha),
                                              fill = alpha(fill, alpha),  
                                              label = label, 
                                              fontsize = size * .pt)))
        )
      }
    
      draw_legend <- function(., data, ...) {
        data <- aesdefaults(data, .$default_aes(), list(...))
    
        with(data,
             pointsGrob(0.5, 0.5, size=unit(size, "mm"), pch=shape, 
                        gp=gpar(
                          col = alpha(colour, alpha), 
                          fill = alpha(fill, alpha), 
                          label = label,
                          fontsize = size * .pt)
             )
        )
      }
    
      default_stat <- function(.) StatIdentity
      required_aes <- c("x", "y")
      default_aes <- function(.) aes(shape=16, colour="black", size=2, 
                                     fill = NA, alpha = NA, label = NA)
    
    })
    
    p <- ggplot(mtcars2, aes(mpg, wt, label = names)) + 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)
    
    jlabel <- toJSON(point_grobs_labels)
    
    grid.text("value", 0.05, 0.05, just = c(0, 0), name = "text_place", gp = gpar(col = "red"))
    
    script <- '
    var txt = null;
    function f() {
        var id = this.id.match(/geom_point.([0-9]+)\\.points.*\\.([0-9]+)$/);
        txt.textContent = label[id[1]-1][id[2]-1];
    }
    
    window.addEventListener("load",function(){
        var es = document.getElementsByTagName("circle");
        for (i=0; i<es.length; ++i) es[i].addEventListener("mouseover", f, false);
    
        txt = (document.getElementById("text_place").getElementsByTagName("tspan"))[0];
    
    },false);
    '
    grid.script(script = paste("var label = ", jlabel))
    grid.script(script = script)
    
    gridToSVG()
    

    there were no big changes, but I had to add

    mtcars2 <- data.frame(mtcars, names = rownames(mtcars))
    

    and then

    p <- ggplot(mtcars, aes(mpg, wt, label = rownames(mtcars)))
         + geom_point2() + facet_wrap(~ gear)
    

    also changes to

    p <- ggplot(mtcars2, aes(mpg, wt, label = names)) 
         + geom_point2() +facet_wrap(~ gear)
    

    because we have rownames(mtcars)

    rownames(mtcars)
     [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"          "Hornet 4 Drive"     
     [5] "Hornet Sportabout"   "Valiant"             "Duster 360"          "Merc 240D"          
     [9] "Merc 230"            "Merc 280"            "Merc 280C"           "Merc 450SE"
    .....
    

    and then labels (which we manage to get with other modifications) remain the same, i.e. not rearranged by gears, only split by it:

    point_grobs_labels
    [[1]]
     [1] "Mazda RX4"          "Mazda RX4 Wag"      "Datsun 710"         "Hornet 4 Drive"    
     [5] "Hornet Sportabout"  "Valiant"            "Duster 360"         "Merc 240D"         
     [9] "Merc 230"           "Merc 280"           "Merc 280C"          "Merc 450SE"        
    [13] "Merc 450SL"         "Merc 450SLC"        "Cadillac Fleetwood"
    [[2]]
    ....
    

    but having these label names as a column fixes the problem.

    point_grobs_labels 
    [[1]]
     [1] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"             "Duster 360"         
     [5] "Merc 450SE"          "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
     [9] "Lincoln Continental" "Chrysler Imperial"   "Toyota Corona"       "Dodge Challenger"   
    [13] "AMC Javelin"         "Camaro Z28"          "Pontiac Firebird"   
    
    [[2]]
    ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.