7.4 Control Graphics Devices

dev.cur() returns a length-one named integer vector giving the number and name of the active device, or 1, the null device, if none is active.

dev.new() opens a new RStudioGD device.

dev.list() returns the numbers of all open devices, except device 1, the null device.

  • This is a numeric vector with a names attribute giving the device names, or NULL is there is no open device.
  • Usually 2 is RStudioGD 2.

dev.set(which = dev.next()) makes the specified device the active device. If there is no device with that number, it is equivalent to dev.next. If which = 1 it opens a new device and selects that.

dev.prev(which = dev.cur()) dev.next and dev.prev select the next/previous open device in the appropriate direction, unless no device is open.

dev.off(which = dev.cur()) shuts down the specified (by default the current) device.

graphics.off() shuts down all open graphics devices.

Save the current graphic object

library(grid)
grab <- grid.grab()
ggsave(grab, filename = f_name, width=10.9, height=5.82)
  • pdf device uses inches to specify width and height.
  • png and jpeg use pixels to specify dimensions.

7.4.1 Save PNG

plot_png <- function(p, f_name, width, height, ppi=300){
    # a plot wrapper
    png(f_name, width=width*ppi, height=height*ppi, res=ppi)
    print (p)
    dev.off()
}
  • Sometimes the title got cut off. You can change oma to add more top margin.
    • If it doesn’t help, change the dimension of your figure, make it higher by changing the height of the figure output.
    • par(oma=c(bottom, left, top, right)) only works for base-R graphics; use theme(plot.margin = margin(t=7, b=7, r=12, l=7, unit="pt") ) to add more margins to ggplot object.
  • You can check the dimension of your figure by right-click on the plot, then “Copy Image Address”, paste the address somewhere, you get the width and height information in the address. You can use this aspect ratio as a start.
    • unit: px.
  • Don’t make the figure too big as the title, axis ticks, will become too small to see in proportion.

Forward display

x11() opens an interactive graphics device. x11 is Apple’s X server. It is reliable and provides hardware OpenGL acceleration.

https://www.rdocumentation.org/packages/grDevices/versions/3.6.2/topics/x11

https://www.cgl.ucsf.edu/chimera/data/downloads/1.11.2/mac_x11.html

When you are using a native Mac application (not X windows) and then click on a menu within the X window, the mouse click does not bring up the menu. It just activates the X window and another mouse click is needed to show the menu. To make this work with a single mouse click, use the Mac X server wm_click_through preference by typing the following command in a Mac Terminal window:

defaults write org.x.x11 wm_click_through -bool true Disables the default behavior of swallowing window-activating mouse events.

  • Normally Mac OS X swallows window-activating mouse events. This preference causes a window-activating mouse click on an X window to also be processed by the application.
  • X11 must be restarted after any of these settings are changed. The settings are saved in your ~/Library/Preferences/org.x.x11.plist file, so they will apply to future sessions. Reissuing the commands with false instead of true will restore the default preference settings.

An item that provides click-through is one that a user can activate with one click, even though the item is in an inactive window. (To activate an item that does not support click-through, the user must first make the containing window active and then click the item.) Although click-through can make some user tasks easier, it can also confuse users if they click items unintentionally.

x11 preferences

  • Input

    image-20221114225826302

  • windows

    image-20221114225900595

  • security

    image-20221114225922001

Bug: X11 windows do not raise (come to the front) when the application is activated.

http://hints.macworld.com/article.php?story=20050714011418999

bind a new key to an AppleScript mac Opt+Tab

https://apple.stackexchange.com/questions/175215/how-do-i-assign-a-keyboard-shortcut-to-an-applescript-i-wrote

image-20221115084137151

For the Quartz device, you can use quartzFonts() to see what the default font for each of these keywords is

quartzFonts()
# $serif
# [1] "Times-Roman"      "Times-Bold"       "Times-Italic"     "Times-BoldItalic"
# 
# $sans
# [1] "Helvetica"             "Helvetica-Bold"        "Helvetica-Oblique"     "Helvetica-BoldOblique"
# 
# $mono
# [1] "Courier"             "Courier-Bold"        "Courier-Oblique"     "Courier-BoldOblique"

Table of font

http://www.cookbook-r.com/Graphs/Fonts/

Font support in R is generally not very good. It varies between systems, and between output formats.

Sometimes what you see on the screen isn’t necessarily the same as what you get when you output to PNG or PDF.

PNG output has less suport for font variety; PDF has better support.

Short Name Canonical Name
mono Courier
sans Helvetica
serif Times
AvantGarde
Bookman
Helvetica-Narrow
NewCenturySchoolbook
Palatino
URWGothic
URWBookman
NimbusMon
URWHelvetica NimbusSan
NimbusSanCondNimbusSanCond
CenturySchCenturySch
URWPalladio
URWTimes NimbusRom

extrafont package

https://github.com/wch/extrafont