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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:47:16+00:00 2026-05-20T04:47:16+00:00

I would like to use tikzDevice to include annotated ggplot2 graphs in a Latex

  • 0

I would like to use tikzDevice to include annotated ggplot2 graphs in a Latex document.

tikzAnnotate help has an example of how to use it with base graphics, but how to use it with a grid-based plotting package like ggplot2? The challenge seems to be the positioning of the tikz node.

playwith package has a function convertToDevicePixels (http://code.google.com/p/playwith/source/browse/trunk/R/gridwork.R) that seems to be similar to grconvertX/grconvertY, but I am unable to get this to work either.

Would appreciate any pointers on how to proceed.

tikzAnnotate example using base graphics

library(tikzDevice)
library(ggplot2)
options(tikzLatexPackages = c(getOption('tikzLatexPackages'),
                "\\usetikzlibrary{shapes.arrows}"))
tikz(standAlone=TRUE)

print(plot(15:20, 5:10))
#print(qplot(15:20, 5:10))

x <- grconvertX(17,,'device')
y <- grconvertY(7,,'device')
#px <- playwith::convertToDevicePixels(17, 7)
#x <- px$x
#y <- px$y

tikzAnnotate(paste('\\node[single arrow,anchor=tip,draw,fill=green] at (',
                x,',',y,') {Look over here!};'))
dev.off()

resulting image

  • 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-05-20T04:47:17+00:00Added an answer on May 20, 2026 at 4:47 am

    Currently, tikzAnnotate only works with base graphics. When tikzAnnotate was first written, the problem with grid graphics was that we needed a way of specifying the x,y coordinates relative to the absolute lower left corner of the device canvas. grid thinks in terms of viewports and for many cases it seems the final coordinate system of the graphic is not known until it is heading to the device by means of the print function.

    It would be great to have this functionality, but I could not figure out a way good way to implement it and so the feature got shelved. If anyone has details on a good implementation, feel free to start a discussion on the mailing list (which now has an alternate portal on Google Groups) and it will get on the TODO list.

    Even better, implement the functionality and open a pull request to the project on GitHub. This is guaranteed to get the feature into a release over 9000 times faster than if it sits on my TODO list for months.


    Update

    I have had some time to work on this, and I have come up with a function for converting grid coordinates in the current viewport to absolute device coordinates:

    gridToDevice <- function(x = 0, y = 0, units = 'native') {
      # Converts a coordinate pair from the current viewport to an "absolute
      # location" measured in device units from the lower left corner. This is done
      # by first casting to inches in the current viewport and then using the
      # current.transform() matrix to obtain inches in the device canvas.
      x <- convertX(unit(x, units), unitTo = 'inches', valueOnly = TRUE)
      y <- convertY(unit(y, units), unitTo = 'inches', valueOnly = TRUE)
    
      transCoords <- c(x,y,1) %*% current.transform()
      transCoords <- (transCoords / transCoords[3])
    
      return(
        # Finally, cast from inches to native device units
        c(
          grconvertX(transCoords[1], from = 'inches', to ='device'),
          grconvertY(transCoords[2], from = 'inches', to ='device')
        )
      )
    
    }
    

    Using this missing piece, one can use tikzAnnotate to mark up a grid or lattice plot:

    require(tikzDevice)
    require(grid)
    options(tikzLatexPackages = c(getOption('tikzLatexPackages'),
                    "\\usetikzlibrary{shapes.arrows}"))
    
    tikz(standAlone=TRUE)
    
    xs <- 15:20
    ys <- 5:10
    
    pushViewport(plotViewport())
    pushViewport(dataViewport(xs,ys))
    
    grobs <- gList(grid.rect(),grid.xaxis(),grid.yaxis(),grid.points(xs, ys))
    
    coords <- gridToDevice(17, 7)
    tikzAnnotate(paste('\\node[single arrow,anchor=tip,draw,fill=green,left=1em]',
      'at (', coords[1],',',coords[2],') {Look over here!};'))
    
    dev.off()
    

    This gives the following output:

    TikZ Annotation of Grid Graphics


    There is still some work to be done, such as:

    • Creation of a “annotation grob” that can be added to grid graphics.

    • Determine how to add such an object to a ggplot.

    These features are scheduled to appear in release 0.7 of the tikzDevice.

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

Sidebar

Related Questions

I would like to use permalink for external links on my site. For example,
I would like to use Subversion and checkout only source files (for example: checking
I would like to use a language that I am familiar with - Java,
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
I would like to use a component that exposes the datasource property, but instead
I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to use javascript to develop general-purpose GUI applications. Initially these are
I would like to use my laptop as a web development (PHP, Python, etc.)
I would like to use Emacs to edit some VB6 files but Emacs does

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.