Saving ggplot in EPS format for editing in Adobe Illustrator -- problem with text

Viewed 3570

Question

I'd like to save a ggplot from R for editing in Adobe Illustrator (AI). I can save the plot in EPS or PS format with ggsave, but the plot always brings along some shading around the text. Is there a way to fix this in R or Adobe Illustrator?

For example, my plot looks like this:

png

But, when I import it into AI, it looks like this (pink shading around text):

screenshot

Code

# Saving a plot for editing in Adobe Illustrator.

library(ggplot2) # for plotting
library(cowplot) # for ggsave

# Generate an example scatter plot.
# From: http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
options(scipen=999)  # turn-off scientific notation like 1e+48
theme_set(theme_gray())  
data("midwest", package = "ggplot2")

plot <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state, size=popdensity)) + 
  geom_smooth(method="loess", se=F) + 
  xlim(c(0, 0.1)) + 
  ylim(c(0, 500000)) + 
  labs(subtitle="Area Vs Population", 
       y="Population", 
       x="Area", 
       title="Scatterplot", 
       caption = "Source: midwest")
plot

# Save the plot as .eps with ggsave. 
file <- "myplot.eps"
ggsave("myplot.jpg",plot)
2 Answers

Update 11/03/2020

I now insure that I have explcity set the font to be 'Arial' before generating any plots. You can set a custom font with the extrafont package.

library(extrafont)

font_import(path=font_path, prompt=FALSE)

fonts() # check to see which fonts are available

choose_font("Arial")

# plotting code ...

Where font_path specifies the path to a directory containing your desired "font", e.g. arial.ttf.

Old Partial Solution

I apologize for the poor question. The pink shading behind the plot's text after being imported into Adobe Illustrator indicates that the font is not recognized by AI. If you export the plot from AI, this shading is gone.

To add a font to AI, you can try following these instructions:

Adding a new font to Adobe Illustrator

Source

  • Create a new AI document (file > new)
  • Start typing in new document.
  • Download the orange juice font from dafont.com.
  • Extract the file (orange juice 2.0.ttf)
  • Install the TrueType file (right click > install).
  • The font should now be recognized in AI.

To check which font ggplot is using:

> mytheme <- ggplot2::theme_gray()
> mytheme$family
[1] "" # The default is sans.

# To check which sans font is being used:
> windowsFonts()
$`serif`
[1] "TT Times New Roman"

$sans
[1] "TT Arial" 

$mono
[1] "TT Courier New"

# My PC's default sans font is TT Arial.
'''

Since you are already using ggplot2 you can change the last line to

ggsave("myplot.eps",plot)

OR

setEPS()
postscript("whatever.eps")
#Plot Code

Follow the below link for other possible solutions:

Export a graph to .eps file with R

To ensure that you export documents that you can edit via graphical editors. You need to select a theme (including fonts & etc.) for the plot that is supported by the graphical editors.

Refer the below links for GGPLOT2 theme documentation :

https://ggplot2.tidyverse.org/reference/theme.html

https://www.rdocumentation.org/packages/ggplot2/versions/2.1.0/topics/theme

Related