I am trying to embed a .eps file for a journal publication requirements.
I create my plot using ggplot2:
p=ggplot(data=sim, aes(x=TIME,y=DV,group=ID))+
theme_few()+
geom_point(aes(shape=as.factor(SEASON2)),size=3,fill="white")+
geom_point(aes(color=as.factor(AGE2),shape=as.factor(SEASON2)),size=3,fill="white",show_guide=F)+
scale_shape_manual(name="Season",values=c(25,24))+
geom_line(aes(color=as.factor(AGE2),linetype=as.factor(MODEL2)),size=0.75)+
scale_linetype_manual(name="Model [Population]",values=c("dotted","solid"))+
scale_color_manual(name="Age",values=as.vector(c(ggthemes_data$few$medium[5],ggthemes_data$few$medium[4])))+
theme(legend.position="bottom",legend.direction="vertical",legend.box="horizontal")+
guides(color=guide_legend(order=1), shape=guide_legend(order=2), linetype=guide_legend(order=3))+
xlab("Clock time [hours]")+
ylab("Testosterone levels [ng/dL]")+
geom_hline(yintercept=300,linetype="dashed",color="black")
print(p)
And then, I generate the .eps
postscript(file.path(directory,"Script","Figure5.eps"),
width=10,
height=12.25,
paper="a4",
horizontal=T,
onefile=TRUE)
print(p)
dev.off()
This .eps was not accepted for the online application when I tried to submit the plot because I have to make the fonts available to ADQ Advisor.
In order to do that I used:
install.packages("extrafont")
library("extrafont")
font_import()
fonts()
loadfonts(device = "postscript") ## for postscript()
embed_fonts("./Figure5.eps", outfile = "./Figure5-embed.eps", options = "-dEPSCrop")
embedFonts(file="Figure5.eps",
outfile="Figure5EMB.eps",
options="-dEPSCrop")
Both of these functions failed and gave me the following error:
Error in embedFonts(file = "Figure5.eps", outfile = "Figure5EMB.eps", :
GhostScript was not found
I have GhostScript 9.18 installed in the following path: C:\Program Files (x86)\gs\gs9.18
Any suggestions?
According to the R documentation you set the location of the Ghostscript executable to be used by R using the R_GSCMD environment variable, failing that the PATH is searched. Do you have that environment variable set or do you have the path to the Ghostscript executable added to the PATH environment variable ?
Note also this, from the R developer mailing list, and this. I have no idea why the documentation thinks there are different executables to view and manipulate PostScript/PDF files, this isn't true (though its possible you might want to use a different application, such as GSView, to view files the Ghostscript display device works as well though much more crudely).
Related
I've been using mapshot a lot to send interactive maps of data but recently, although I can make the maps I want with mapview, I can't save them.
Example:
map<- mapview(mapdata, zcol = "columnofinterest", burst = TRUE)
mapshot(map, url = paste0(getwd(), "/whatIwanttocallmymap.html"))
File whatIwanttocallmymap_files/PopupTable-0.0.1/popup.css not found in resource path
Error: pandoc document conversion failed with error 99
I'm afraid I've messed something up in how I get packages. folders with package names are turning up in the area I've set as my wd instead of in my library for R
Thank you for any help/suggestions you might have
I am learning how to use the tmap package to generate a map and save it to an interactive HTML file. If I created a tmap object and save it to my working directory using the save_tmap function, I got a HTML file as expected. But if I change the directory to other paths, it generated an error message. Please see the following example.
# Load tmap package
library(tmap)
# Load example data
data(World)
# Create example map
example_map <- tm_shape(World, projection="longlat") +
tm_polygons() +
tm_layout("Long lat coordinates (WGS84)",
inner.margins = c(0, 0, .1, 0), title.size = .8)
# Save an HTML object
save_tmap(example_map, "example_map.html")
I will get the example_map.html in my working directory.
# Create a folder in the working directory
if (!dir.exists("tmap_folder")){
dir.create("tmap_folder")
}
# Save the HTML object in "tmap_folder"
save_tmap(example_map, "tmap_folder/example_map.html")
It will not work. I will get the following error message
Error in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="tmap_folder": The system cannot find the file specified
Please share your thoughts if you have any ideas what could cause this error.
This sounds like a struggle between normalizePath and Windows (see another example here). Instead of using a relative path like "tmap_folder/example_map.html" try using an absolute path like C:/users/ycw/tmap_folder/example_map.html.
Another option seems to be using the here package. That way, you don't have to specify the full path and you code is likely to be more portable, play better with rstudio projects and Rmarkdown.
# Save the HTML object in "tmap_folder" with here package
library(here)
save_tmap(example_map, here("tmap_folder","example_map.html"))
I am preparing a paper for submission to a journal which requires vector graphics to be submitted in EPS format, with all text within figures converted to outlines. Converting text to vector outlines helps avoid font problems in the final layout. The journal requirements are available here.
EPS figures can easily be created in R using ggplot2:
library(ggplot2)
ggsave(filename = "file.eps")
or using setEPS() and the postscript device, as explained previously here:
setEPS()
postscript("filename.eps")
plot(1:10)
dev.off()
My question is: how can you convert text within the figures into vector outlines? I have found information on how to do this in other software (e.g. Illustrator or InDesign), but I would like to know if there is a way to do it directly in R.
Install ghostscript using your distro's package manager if you're under linux or download it for windows (there's also a portable version), for mac you can probably use brew. Then invoke gs or gs.exe from within R like in this SO question.
library(ggplot2)
data(diamonds)
gs.cmd <- "gs" # ghostscript executable
# gs.cmd <- "C:/path/to/gs.exe" # Windows
p <- qplot(x=carat, y=price, color=clarity, data=diamonds)
ggsave(p, filename="test.eps")
system(paste(gs.cmd,"-o test_outline.eps -dNoOutputFonts -sDEVICE=eps2write test.eps"))
I can't set my fonts in geom_text. Here is what I tried:
labels_test<-data.frame(a=c("a","b","c"),b=c(1:3),c=c(3:1))
# works
ggplot () + geom_text(data=labels_test,aes(b,c,label=a),color="blue")
# does not work:
ggplot () + geom_text(data=labels_test,aes(b,c,label=a),color="blue",family="Times")
# error message: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,:
# Font family not found in Windows font database
I already imported all fonts as indicated here. Any ideas what is still going wrong?
The other answers didn't solve my problem (Windows 10).
The key for my system was to call extrafont::loadfonts(device="win") prior to library(ggplot2).
extrafont::loadfonts(device="win")
#extrafont::fonttable()
#extrafont::font_import("C:/Windows/Fonts/", pattern = "RobotoCondensed")
library(ggplot2)
Common problem with font locations:
I had installed the fonts from a random folder with extrafont::font_import() previously. As such extrafont::fonttable() referenced the files in my C:\Windows\Fonts\ folder. To fix this I reset my extrafonts::fonttable() with install.packages("extrafontdb") in order to clear a reference to the fonts in a different location.
Edit regarding saving:
Deeper down the rabbit hole. Saving was an additional challenge. In order to extrafont::loadfonts(device="pdf") I had to make sure no fonts in my extrafont::fonttable() had identical family names and bold/italic status. I edited extrafont:::fonttable_file() to resolve any duplicate bold/italic fonts within my family. Using Roboto Condensed I renamed the light fonts' font family to "Roboto Condensed Light".
Saving with ggsave(device="pdf") then worked. Opening the files in acrobat the fonts did not display correctly. I tried embedding the fonts with ghostscript as well as using the cairo_pdf device. The easiest and most functional solution was to open the .pdf files in Illustrator (the fonts display fine there) and immediately re-save them again as .pdf.
Edit 2 regarding saving:
Saving as .eps was the only way to preserve the file in both illustrator and acrobat. The result is perfect. ggsave(g, file="Figure.eps", fonts=c("FONT FAMILIES USED", "Roboto Condensed", "Roboto Condensed Light"))
Final plotting code:
Here is my final set of calls I use before plotting. Comments are setup commands that need to be run once only.
# Plotting
extrafont::loadfonts(device="pdf")
extrafont::loadfonts(device="postscript")
# extrafont::font_import("C:/Windows/Fonts/", pattern = "RobotoCondensed", prompt = F)
# extrafont::fonttable()
# C:/Program Files/R/R-3.3.1/library/extrafontdb/fontmap/ - Change lights to "Roboto Condensed Light"
# After ggsave(device="pdf") or ggsave(device="eps") open and resave the file in Illustrator
library(hrbrthemes)
library(ggplot2)
I would try"
windowsFonts(Times=windowsFont("TT Times New Roman"))
In doing this your specifying explicitly the Windows Font mapping.
You must import the system fonts using the command:
font_import(paths = NULL, recursive = TRUE, prompt = TRUE,pattern = NULL)
I tried the different solutions here but none worked for me (win10, R 3.4.3).
This is what worked for me:
install.packages("extrafont")
library(extrafont)
loadfonts(device = "win")
It did not matter if I did it before or after library(ggplot2)
Sources:
https://cran.r-project.org/web/packages/extrafont/extrafont.pdf
Changing fonts in ggplot2
I'm having trouble with exporting eps files from R and importing into Word 2010.
I'm using ggplot2 plots, eg
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth()
p
Even after calling setEPS() neither of the following produce files which can be successfully imported.
ggsave("plot.eps")
postscript("plot.eps")
print(p)
dev.off()
The strange thing is that if I produce the plot using File -> Save As -> Postscript from the menu in the GUI, it can be imported correctly. However, when the Word document is subsequently exported as a pdf, the fonts in the graphic are a little jagged.
So my questions are:
What combination of (ggsave/postscript) settings allows me to produce eps files that can be imported into Word 2010?
How can I ensure the fonts remain clear when the Word document is exported as a pdf?
Update
After more investigation I have had more luck with cairo_ps to produce the plots. However, no text shows up when imported into Word.
Furthermore, after checking the various eps outputs (cairo_ps, save from the GUI, ggsave) in a latex document, it seems like the eps import filter in Word quite poor as the printed/pdf output doesn't match the quality of the latex'd document. The ggsave version (which uses postscript) did have some issues with colours that the other two methods didn't have though.
The conclusion is that this is a Word issue and therefore fortune(109) does not apply. I'd be happy to be proven otherwise, but I'll award the answer and the bounty to whoever can provide the commands that can replicate the output from the GUI in command form.
This worked for me... following advice in the postscript help page:
postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE, onefile = FALSE,
paper = "special")
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth()
p
#geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to #change the smoothing method.
#Warning message:
#In grid.Call.graphics(L_polygon, x$x, x$y, index) :
# semi-transparency is not supported on this device: reported only once per page
dev.off()
#quartz
# 2
The funny stuff at the end puts you on notice that this is only a Mac-tested solution, so far anyway.
Edit: I just tested it with R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows": Platform: i386-pc-mingw32/i386 (32-bit) and MS Word 2007 in Win XP and it worked. Commands were Insert/Picture.../select eps format/select file.
Edit2: There is another method for saving besides directly using the postscript device. The savePlot method with an "eps" mode is available in Windows (but not in the Mac). I agree that the fonts are not as smooth as they appear on a Mac but I can discern no difference in quality between saving with savePlot and using save as from an interactive window.
savePlot(filename = "Rplot2", type = "eps", device = dev.cur(), restoreConsole = TRUE)
savePlot calls (.External(CsavePlot, device, filename, type, restoreConsole))
I solved the problem with exporting .eps files from R and importing into Word 2010 on Windows 7 using the colormodel="rgb" option (defaults to "srgb") of the postscript command.
postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE,
paper = "special", colormodel = "rgb")
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth(se=FALSE, method="loess")
p
dev.off()
You are probably better of using wmf as a format which you can create on Windows.
Word indeed doesn't support EPS very well.
A better solution is to export your graphs to Word or Powerpoint directly in native Office format. I just made a new package, export, that does exactly that, see
https://cran.r-project.org/web/packages/export/index.html and
for demo
https://github.com/tomwenseleers/export
Typical syntax is very easy, e.g.:
install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species,
size = Petal.Width, alpha = I(0.7))
graph2doc(file="ggplot2_plot.docx", width=6, height=5)
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5)
Output is vector format and so fully editable after you ungroup your graph in Word or Powerpoint. You can also use it to export statistical output of various R stats objects.
You can use R studio to knit html files with all of your plots and then open HTML files with Word.
knitr tutorial