5

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)
KenS
  • 30,202
  • 3
  • 34
  • 51
twb10
  • 533
  • 5
  • 18

2 Answers2

4

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.
'''
twb10
  • 533
  • 5
  • 18
  • 1
    Your question is actually a good one. But you should improve your answer by explaining why installing `orange juice` font helps, when the used font is presumably `TT Arial`? You don't get the pink boxes in illustrator afterwards? – liborm Jun 13 '19 at 05:32
  • Well, I actually got stuck at this point. Although I was able to add fonts to AI or change the font used in ggplot to a font used by AI the plot's text is still not imported into AI correctly. The ultimate solution I arrived at was to just ggsave as .eps and then change all of the font in AI at once. – twb10 Jun 14 '19 at 02:43
1

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

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
  • 1
    Thanks for pointing out the error. I realized that I hadn't tried exporting the image from AI, and when I do the pink shading is gone. I'm guessing its AI trying to tell me something. It's a bad question. – twb10 Jun 12 '19 at 01:52