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)
Related
Had some recent issues with my workflow. I normally print Rplots as .emf files using the devEMF package, and then edit them in powerpoint. [Import the emf file to powerpoint, right click on image, and select ungroup.] However, recently powerpoint isn't recognizing the vector format when I "ungroup."
I'm using a windows machine and powerpoint 2016.
Any thoughts would be VERY helpful.
Nate
Just to leave a definitive answer here -- Powerpoint does not yet allow editing ("ungrouping") of EMF+ graphics. Thus the solution is to tell devEMF to not use emfPlus features (the downside being that features such as transparency will not be available):
emf(file = "test.emf", emfPlus = FALSE)
plot(1:10)
dev.off()
The original poster's comment worked because the older version of devEMF did not use emfPlus by default.
I constructed dendrogram in R with the code:
data(iris)
aver<-sapply(iris[,-5],function(x) by(x,iris$Species,mean))
matrix<-dist(aver)
clust<-hclust((matrix),"ave")
clust$labels<-row.names(aver)
plot(as.dendrogram(clust))
I wanted to save the dendrogram as svg file using the code:
install.packages("Cairo")
library(Cairo)
svg("plot.svg")
plot(as.dendrogram(clust))
dev.off()
Here the problem started:
When I imported the "plot.svg" into Inkscape (ver: 0.48.4) and selected any label (e.g. "setosa") it was not recognized as a text, but rather as some "user defined" object. Specifically, when I selected any "letter" in the label and inspect it with the XML Editor (ctrl+shift+X) in Inkscape I obtained this information:
**id**: use117
**x**: 142.527344
**xlink:href**: #glyph0-8
**y**: 442.589844
On the other hand, when I manually wrote "setosa" using "create and edit text objects" tool, and inspected in XML Editor, it returned:
**id**: text4274
**sodipodi:linespacing**: 125%
**style**: font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Palatino Linotype;-inkscape-font-specification:Palatino Linotype
**transform**: scale(0.8,0.8)
**x**: 176.02016
**xml:space**: preserve
**y**: 596.96674
It is likely that Inkscape did not recognize the labels as a text according to the attribute "id" from XML Editor. Hence, I am not able to change neither font, size as well as use other functions related to text objects in Inkscape.
Here is the svg file, that I made with the previous code and imported into Inkscape
I checked previous steps using other versions of Inkscape as well as R, but it would be the same.
Here is the question:
Do you have any suggestion how I can gather labels as a text attribute instead of a "user defined" (or whatever it is object...) when importing svg files from R into Inkscape?
UPDATE
#baptiste linked to the SO thread where #Oscar Perpiñán suggested three packages (gridSVG, SVGAnnotation and RSVGTipsDevice) that manipulate SVG. Unfortunately, neither of packages suggested could solve the problem with the text issue.
So far I found SO thread where #Mo Sander suggested RSvgDevice package since it can preserve text object rather than glyphs. Being stuck with the RSvgDevice installation procedure, I found that it RSvgDevice is only available for 32-bit installations and R < 2.15.0. Otherwise, R returned warning message:
Warning message:
package ‘RSvgDevice’ is not available (for R version 3.0.1)
Beside the requirements for older R versions, currently only RSvgDevice can preserve a text object in SVG.
I'm a bit late to the party, but I've been dealing with this myself. I found a trick to make it work. First, I export the plot as PDF instead of SVG because PDF fonts are recognized by inkscape.
This, however brings a new problem as the text often ends up being defined letter by letter meaning that you can change the font, but the spacing is still defined and it becomes immensely annoying. I found that it was due to the x coordinate being defined at each letter.
I wrote a perl script and put it in this gist to remove all the trailing coordinates. After that I'm able to manipulate all the fonts I wished. Note, that this will only work for horizontal text.
Hope that helps this problem you had over a year ago :)
This is a failing in Cairo. Major, from my point of view.
The cairo SVG surface (i.e. the back-end in Cairo used to "draw" on SVG) simply does not support the "text" tag. It does not understand about strings at all. Instead, it places each character (glyph) individually. So any SVG generated with Cairo is not useful if you want to post-process contained text with a vector editor. :(
The only mention I found on the cairo list was this one:
http://lists.cairographics.org/archives/cairo/2011-February/021777.html
The svglite package exports text on Linux as desired.
[EDIT] According to this thread, there is also a way to remove the squeezing of the edited text into the fixed box width. Just remove the textLength field from the object in the XML editor.
Cheers
Can't directly comment on mgrewe answer because of my low reputation but thank you for the solution.
Implemented the textLength edit into R:
svgitem<-readLines('file.svg')
svgitem<-gsub('textLength=','tL=',svgitem)
writeLines(svgitem,'without_textLength.svg')
Text-box seems to be no longer affected after edition in Inkscape using the without_textLength.svg file and keeps a trace of old textLength renamed 'tL'.
Thanks again mgrewe, I've lost so many hours reformating text in Inkscape before seeing your answer.
R is clearly not using the standard SVG text objects for producing its labels. I have no idea why. I am not an R user.
Perhaps by default it uses it's own custom font that it manually inserts glyph-by-glyph into the output. Are you using the same font in both cases? In Inkscape you are using Palatino. Is that what you are using for the labels in R?
Does anyone know why savePlot can't save to pdf in linux by default?
> savePlot("rv-3.pdf", type="pdf")
Error in match.arg(type) :
'arg' should be one of “png”, “jpeg”, “tiff”, “bmp”
lizard:~images$ R --version
R version 2.14.1 (2011-12-22)
...
?savePlot is pretty clear about this:
This works by copying the image surface to a file.
Hence you start with a raster representation and therefore can only go to a raster representation. It would be somewhat perverse to pipe a raster version of the plot in a PDF, which is a vector format (yes I know you can have rasters inside PDFs).
The functionality is limited to cario-based X11 devices and the documentation refers to copying the "on screen" representation hence the restrictions.
I suppose the other Answer to your question is: that functionality has not been implemented yet.
dev.copy2pdf does what you want:
plot(1:10)
dev.copy2pdf(file="~/test.pdf")
From reading the help files, I take it this will effectively replot your figure as a vector image in the file, which will usually be preferable to exporting your vector image into a raster format, as savePlot appears to do.
Try this:
pdf(file="rv-3.pdf")
plot(x,y)
dev.off()
you can also change the size by by adding height= or width= to the pdf function.
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?