Related
On some computers, the following code used in conjunction with the packages siar and SIBER does not render the delta and/or permil symbol correctly in the axes labels. Instead, either a blank axis label, or text such as "\u2030" is rendered in its place.
plot(0,xlab = expression(paste(delta^13,"C (\u2030)")))
One often encountered problem is that your computer's region settings (i.e. your operating system, not the applications R or Rstudio) is set to use a non-UTF8 character set. If you type
Sys.setlocale()
in the R command window, you should see something like
"en_IE.UTF-8/en_IE.UTF-8/en_IE.UTF-8/C/en_IE.UTF-8/en_IE.UTF-8"
which for me means I'm using UTF-8 in english with Irish region settings.
If you don't see UTF-8 then the \u2030 and other character codes won't work
This is an issue I am encountering for different pieces of codes I am writing in R.
Basically, I would like to generate a window that displays a picture (a .png file). Following for instance guidances from this or this, I come up with this kind of code:
library(tcltk)
tmpFile <- tempfile(fileext = ".png")
download.file("https://www.r-project.org/logo/Rlogo.png", tmpFile)
tcl("image","create","photo", "imageLogo", file=tmpFile)
win1 <- tktoplevel()
tkpack(ttklabel(win1, image="imageLogo", compound="image"))
This works fine under Mac OS, but not on Linux nor on Windows, where I am displayed such an error message:
[tcl] couldn't recognize data in image file
I can find some workarounds when I want to display graphs, using for instance packages tkrplot or igraph. Nonetheless, I would be really eager to understand why I got such errors when running my scripts on Linux or Windows, whereas it works just fine on Mac OS.
Apologies in case this issue is obvious, but I haven't found anything about potential differences with the tcltk package depending on the OS.
Tk's native support for PNG was added in 8.6. Prior to that, you need to have the tkimg extension loaded into Tk to add the image format handler required. If your installation of Tcl/Tk that R is using is set up right, you can probably make it work with:
tclRequire("Img")
once you've initialised things sufficiently. Yes, the name used internally is “Img” for historical reasons, but that's just impossible to search for! (This is the key thing in this mailing list message from way back.)
However, upgrading the versions of Tcl and Tk to 8.6 is likely to be a better move.
Finally and a bit lately, I would like to close this issue and sum up the different suggestions that were kindly made in response of my question:
R comes along with Tcl 8.5, even with the latest version 3.3.2, which means that there is no way for embedding a PNG file with the usual command into a window created thanks to Tcl/Tk. For some reasons it is working on Mac OS, but do not expect this to work easily on other OSs.
In order to display pictures, graphs, etc. in a window generated by Tcl/Tk in R, better look for either using the GIF support (when possible) or trying alternative solutions (see the question for possible alternative options).
In case one really wants to display PNG files, the solution consists of installing Tcl 8.5 (for instance ActiveTcl) along with the extension Img. In order to use the Tcl/Tk package that you've just installed on your computer, you can refer to the R FAQ for Windows for instance (as stated in the FAQ, you need to install Tcl 8.5 - I tried with Tcl 8.6, thereby hoping to solve my issue, but it didn't work). Basically, you need to set up an environment variable (MY_TCLTK) and put the path where the package Tcl/Tk is installed. Needless to be said, Tcl/Tk is commonly used in R in order to implement GUIs; if you have to go through very complex procedures to set up the system, the package definitely loses its advantages.
Finally, since Tcl 8.6 should be available soon or later with R (already implemented in the devel version), this issue will be de facto outdated.
I created several simple ggplot2 plots and saved them to PDF files using the following commands:
p <- ggplot(plotobject, aes(x=Pos, y=Pval),res=300)
ggsave(plot=p,height=6,width=6,dpi=200, filename="~/example.pdf")
If I now open this example.pdf in Adobe Illustrator I get the following error:
The font AdobePiStd is missing. Affected text will be displayed using
a substitute font.
Is there a way in ggplot2 to specify a font (I presume this is for the dots/points) that Adobe will understand or otherwise is there a way to get this font working in Adobe?
/edit just to clarify, the text/labels/titles are all just fine its the dots that are missing for the font
/edit2 I got a working solution by just using the EPS output in ggsave, but still would love to find out a way to also be able to just open/import ggplot2 plots in PDF format directly in AI
Although changing the fonts used by AI works well, an alternative is to restrict ggplot2 from using Dingbats in the first place by adding the argument 'useDingbats' to your ggsave command eg:
ggsave(plot=p,height=6,width=6,dpi=200, filename="~/example.pdf", useDingbats=FALSE)
I ran into this problem as well on Mac OS X 10.8.2.
This resolved the issue for me:
font='/Library/Application Support/Adobe/PDFL/10.9/Fonts/AdobePiStd.otf'
cp $font /Library/Fonts/
On your system, find the file with find:
find / -name AdobePiStd.otf 2>/dev/null
/Applications/Adobe Photoshop CC 2018/Adobe Photoshop CC 2018.app/Contents/Required/PDFL/Resource/Fonts/AdobePiStd.otf
/Applications/Adobe Illustrator CC 2018/Adobe Illustrator.app/Contents/Required/PDFL Resource/Resource/Fonts/AdobePiStd.otf
/Applications/Adobe Acrobat DC/Adobe Acrobat.app/Contents/Resources/Resource/Font/AdobePiStd.otf
For dots or points that don't' display correctly in a pdf, I just substitute Zapf Dingbats for the missing font in Adobe Illustrator. For simple plot markers, this does the job most of the time.
This seems to be a perverse problem in which Adobe Illustrator cannot find a font (AdobePiStd) that is distributed with Adobe Reader and thus is likely present.
If you search your computer, you are likely to find it. On my WindowsXP system, it was in:
C:\Program Files\Adobe\Reader 10.0\Resource\Font
After finding it, you can install it and other fonts in that folder, which should enable Illustrator to find them.
For other font/pdf problems in R, the embedFonts function in grDevices package is useful.
Use useDingbats=FALSE for the pdf output, then Illustrator will no complain.
pdf("example.pdf", useDingbats=FALSE)
ggplot(plotobject, aes(x=Pos, y=Pval),res=300)
dev.off()
You can download the font AdobePiStd - just Google it, download and install. After you reboot, the font should display correctly. You can then use the 'Find Font ...' utility within Illustrator to change it if you wish.
EDIT 2018 The question under was for the initial confusion face. The answer to this question is pdf(useDingbats = FALSE) as stated in numerous answers above. I won't delete this answer, in case you want to read about fonts (which might become a problem too).
OLD ANSWER AI does not recognize Helvetica type fonts. In my computer it doesn't matter that much, since it automatically replaces the font with something suitable. Of course you'd want to change the font in some cases. I often do it manually in AI (just select all the text and change the font type). Here is a thread on how to change the font in ggplot2: Modifying fonts in ggplot2
Ps. There is also another thread that might be helpful: Fonts in R plots
Another option that in simple cases might solve the problem is to in illustrator select the textobjects not displayed correctly and change the font. Helvetica Neue works for me.
I could solve the problem just by adding alpha specification. If you don't need any transparency you can chose an alpha value of 0.9 ...
example:
ggplot() + geom_point(aes(x=..,y=.., color=...), alpha=0.8)
I have a problem with typefaces and PDF-Output in R. On this windows machine there is no Helvetica and the Font used by the device seems to be Arial as you can see below.
The simple problem is, that Arial is used (as I want it to be) but editing the PDF-File it says Helvetica is used. How can I get R to write the correct Name into the PDF-file. pdf(...,family="Arial") won't work, as this family is not known (grDevices version 2.15.1).
Or can I substitude this font in the PDF afterwards, creating a file with the font I want?
R-PDF-Output
Comparision from this Article:Arial vs. Helvetica
Since R-3.1.0, this is much more straightforward. Now, to get an Arial font, just set family="ArialMT":
pdf("Arial.pdf", height=0.3, width=1.45, family="ArialMT")
grid::grid.text("CGJQRSafrst1237")
dev.off()
Quoting from the April 2014 R-3.1.0 release notes:
There is a new family, "ArialMT", for the pdf() and postscript()
devices. This will only be rendered correctly on viewers which
have access to Monotype TrueType fonts (which are sometimes
requested by journals).
To ensure that the pdf will be rendered correctly wherever it's viewed, you'll need to also embed the required Arial fonts in the document. Here's one easy way to do that:
library(extrafont)
loadfonts() ## Really only needed the first time you use extrafont
## Modify this to point to the corresponding Ghostscript executable on your own machine
Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.07/bin/gswin64c.exe")
embed_fonts("Arial.pdf")
You need to set a new font family for use with pdf(). This requires you to have Adobe Font Metric files (*.afm files) for the fonts you wish to use. You can convert the .tty files to .afm ones or find .afm files for Arial on the interweb if you don't already have them.
Arial <- Type1Font(family = "Arial",
metrics = c("ArialMT.afm",
"arial-BoldMT.afm",
"Arial-ItalicMT.afm",
"Arial-BoldItalicMT.afm"))
where the character vector metrics contains the paths to the relevant .afm files, The files should be specified in this order:
plain face
bold face
italic face
bold-italic face
The you use the pdfFonts() function to add a mapping to these new fonts
pdfFonts(Arial = Arial)
where Arial is the object produced by Type1Font() earlier.
The final step is to use the family argument in pdf() which refers to one of the existing families as defined by pdfFonts():
pdf("testArial.pdf", family = "Arial")
plot(1:10, 1:10)
dev.off()
I haven't tried this as I don't have Arial on my system nor too many .afm files lying around, but I pieced this together from several sources:
Paul Murrell and Brian Ripley (2006) Non-standard fonts in PostScript and PDF graphics. R News, 6(2):41-47. PDF
A posting by David L. Carlson on the R mailing list from earlier this year.
An alternative depending on how your system is set-up is to a Cairo-based PDF device as that will use the functions of your system to identify and load fonts based simply on their name. See ?cairo_pdf and then the Cairo Fonts section of ?X11 for details.
As it is stated in the one of the comments, base 14 fonts does'nt need to be embedded and Helvetica is one of these fonts.PDF consumers are supposed to provide replacements for such fonts and Arial (or Arial MT) is often used in place of Helvetica.
I'm not familiar with R but you seem to be able to embed fonts afterward
edit: This question's answer explains how to embed fonts afterward with ghostscript, just be sure to have GS map Helvetica (or Arial) to the version of the font you want. Thanks to Gavin Simpson
for having me search for that :)
R by default does not embed fonts. It just specifies that a Helvetica is to be used. You're probably best off embedding fonts. There's two ways to do that, either use the cairo_pdf device or the extrafont package with the usual pdf device. cairo_pdf is a lot simpler, but the output differs in subtle ways from pdf.
Some of you may have seen my blog post on this topic, where I wrote the following code after wanting to help a friend produce half-filled circles as points on a graph:
TestUnicode <- function(start="25a0", end="25ff", ...)
{
nstart <- as.hexmode(start)
nend <- as.hexmode(end)
r <- nstart:nend
s <- ceiling(sqrt(length(r)))
par(pty="s")
plot(c(-1,(s)), c(-1,(s)), type="n", xlab="", ylab="",
xaxs="i", yaxs="i")
grid(s+1, s+1, lty=1)
for(i in seq(r)) {
try(points(i%%s, i%/%s, pch=-1*r[i],...))
}
}
TestUnicode(9500,9900)
This works (i.e. produces a nearly-full grid of cool dingbatty symbols):
on Ubuntu 10.04, in an X11 or PNG device
on Mandriva Linux distribution, same devices, with locally built R, once pango-devel was installed
It fails to varying degrees (i.e. produces a grid partly or entirely filled with dots or empty rectangles), either silently or with warnings:
on the same Ubuntu 10.04 machine in PDF or PostScript (tried setting font="NimbusSan" to use URW fonts, doesn't help)
on MacOS X.6 (quartz, X11, Cairo, PDF)
For example, trying all the available PDF font families:
flist <- c("AvantGarde", "Bookman","Courier", "Helvetica", "Helvetica-Narrow",
"NewCenturySchoolbook", "Palatino", "Times","URWGothic",
"URWBookman", "NimbusMon", "NimbusSan", "NimbusSanCond",
"CenturySch", "URWPalladio","NimbusRom")
for (f in flist) {
fn <- paste("utest_",f,".pdf",sep="")
pdf(fn,family=f)
TestUnicode()
title(main=f)
dev.off()
embedFonts(fn)
}
on Ubuntu, none of these files contains the symbols.
It would be nice to get it to work on as many combinations as possible, but especially in some vector format and double-especially in PDF.
Any suggestions about font/graphics device configurations that would make this work would be welcomed.
I think you are out of luck Ben, as, according to some notes by Paul Murrell, pdf() can only handle single-byte encodings. Multi-byte encodings need to be converted to a the single-byte equivalent, and therein lies the rub; by definition, single-byte encodings cannot contain all the glyphs that can be represented in a multi-byte encoding like UTF-8, say.
Paul's notes can be found here wherein he suggests a couple of solutions using Cairo-based PDF devices, using cairo_pdf() on suitably-endowed Linux and Mac OS systems, or via the Cairo package under MS Windows.
I have found the cairo_pdf device to be completely insufficient: the output is markedly different from both pdf and on-screen rendering, and its plotmath support is sketchy.
However, there’s a rather simple workaround on OS X: Use the “normal” quartz device and set its type to pdf:
quartz(type = 'pdf', file = 'output.pdf')
Unfortunately, on my computer this ignores the font family and always uses Helvetica (although the documentation claims that the default is Arial).
There are at least two other gotchas:
pdf converts hyphens to minuses. This may not even always be what you want but it’s quite useful to properly typeset negative numbers. The linked thread describes workarounds for this.
It’s of course platform specific and only works on OS X.
(I realise that OP briefly mentions the Quartz device but this thread is frequently viewed and I think this solution needs more prominence.)
Another solution might be to use tikzDevice which can now use XeLaTeX with Unicode characters. The resulting tex file can then be compiled to produce a pdf. The problem is still that you must have a font on your system that contains the characters.
library(tikzDevice)
options(tikzXelatexPackages=c(getOption('tikzXelatexPackages'),
'\\setromanfont{Courier New}'))
tikz(engine='xetex',standAlone=T)
TestUnicode(9500,9900)
dev.off()
The first time, this will take a LONG time.
Have you tried embedding a font in the PDF, or including one for Mac users that would work?