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 7913997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:58:34+00:00 2026-06-03T13:58:34+00:00

I am trying to figure out how to use grconvertX/grconvertX in ggplot. My ultimate

  • 0

I am trying to figure out how to use grconvertX/grconvertX in ggplot. My ultimate goal is to to add annotation to a ggplot2 figure (and possibly lattice) with grid.text and grid.lines by going from user coordinates to device coordinates. I know it can be done with grobs but I am wondering if there is an easier way.

The following code allows me to pass values from user coordinates to ndc coordinates and use those values to annotate the plot with grid.text.

graphics.off()      # close graphics windows   

library(grid)
library(gridBase)


test= data.frame(
  x = c(1,2,3),
  y = c(12,10,3),
  n = c(75,76,73)
  )

par(mar = c(13,5,2,3))

plot(test$y ~ test$x,type="b", ann=F)

for (i in 1:nrow(test))

{
  X=grconvertX(i , from="user", to="ndc")
  grid.text(x=X, y =0.2, label=paste("GRID.text at\nuser.x=", i, "\n", "ndc.x=", (signif( X, 5))   ) ) 
  grid.lines(x=c(X, X), y = c(0.28, 0.33) )
}
#add some code to save as PDF ...

enter image description here

The code is based on the solution from one of my previous posts: Mixing X and Y coordinate systems . You can see how x coordinates from the original plot were converted to ndc. The advantage of this approach is that I can use device coordinates for Y.

I assumed I could easily do the same in ggplot2 (and possibly in lattice).

library(ggplot2)
graphics.off()      # close graphics windows   

qplot(x=x, y=y, data=test)+geom_line()+  opts(plot.margin = unit(c(1,3,8,1), "lines"))

for (i in 1:nrow(test))

{
  X=grconvertX(i , from="user", to="ndc")
  grid.text(x=X, y =0.2, label=paste("GRID.text at\nuser.x=", i, "\n", "ndc.x=", (signif( X, 5))   ) ) 
  grid.lines(x=c(X, X), y = c(0.28, 0.33) )
}

#add some code to save as PDF...

However, it does not work correctly. The coordinates seem to be a bit off. The vertical lines and text don’t correspond to the tick labels on the plot. Can anybody tell me how to fix it? Thanks a lot in advance.

enter image description here

  • 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-03T13:58:36+00:00Added an answer on June 3, 2026 at 1:58 pm

    The grconvertX and grconvertY functions work with base graphics while ggplot2 uses grid graphics. In general the 2 different graphics engines don’t play nicely together (though you have demonstrated using gridBase to help). Your first example works because you started with a base graphic so the user coordinate system exists with the base graph and grconvertX converts from it. In the second case the user coordinate system was never set in the base graphics, so it looks like it might use the default coordinates of 0,1 which are similar but not identical to the top viewport coordinates so you get something similar but not exactly correct (I am actually surprised that you did not get an error or warning

    Generally for grid graphics the equivalent for converting between coordinates is to just create a new viewport with the coordinate system of interest (or push/pop to an existing viewport with the correct coordinate system), then add your annotations in that viewport.

    Here is an example that creates your plot, then moves down to the viewport containing the main plot, creates a new viewport with the same dimensions but with clipping turned off, the x scale is based on the data and the y scale is 0,1, then adds some text accordingly:

    library(ggplot2)
    library(grid)
    
    test= data.frame(   x = c(1,2,3),   y = c(12,10,3),   n = c(75,76,73)   )  
    
    qplot(x=x, y=y, data=test)+geom_line()+  opts(plot.margin = unit(c(1,3,8,1), "lines"))  
    
    current.vpTree()
    downViewport('panel-3-4')
    pushViewport(dataViewport( test$x, clip='off',yscale=c(0,1)))
    
    for (i in 1:nrow(test))  {
        grid.text(x=i, y = -0.2, default.units='native',
            label=paste("GRID.text at\nuser.x=", i, "\n"   ) )
            grid.lines(x=c(i, i), y = c(-0.1, 0), default.units='native' )
     } 
    

    One of the tricky things here is that ggplot2 does not set the viewport scales to match the data being plotted, but does the conversions itself. In this case setting the scale based on the x data worked, but if ggplot2 does something fancier then this might not work. What we would need is some way to get the back tranformed coordinates from ggplot2 to use in the call to grid.text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I'm trying to figure out how use default and custom settings
Trying to figure out which to use.
I'm trying to figure out how to use PredicateBuilder to determine if a specific
I'm trying to figure out how to use NServiceBus in combination withCommon.Logging. I just
I'm trying to figure out how to use Emacs Code Browser (ECB) and one
I'm trying to figure out how to use the java.util.logging features. All of the
I am trying to figure out how to use Apache Commons IO DirectoryWalker .
I'm trying to figure out how to use RJS to swap between two fields...
i'm trying to figure out how to use ApacheBench and benchmark my website. I
I'm trying to figure out how to use Zend_Db_Table_Abstract correctly. I want to return

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.