DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Harmonizing Space, Time, and Semantics: Navigating the Complexity of Geo-Distributed IoT Databases
  • Tips and Tricks for Efficient Coding in R
  • How to Get a Non-Programmer Started with R
  • Python vs. R: A Comparison of Machine Learning in the Medical Industry

Trending

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Key Considerations in Cross-Model Migration
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Scalable System Design: Core Concepts for Building Reliable Software
  1. DZone
  2. Coding
  3. Languages
  4. Removing White Space Around R Figures

Removing White Space Around R Figures

By 
Rob J Hyndman user avatar
Rob J Hyndman
·
Feb. 24, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
7.2K Views

Join the DZone community and get the full member experience.

Join For Free

When I want to insert fig­ures gen­er­ated in R into a LaTeX doc­u­ment, it looks bet­ter if I first remove the white space around the fig­ure. Unfor­tu­nately, R does not make this easy as the graphs are gen­er­ated to look good on a screen, not in a document.

There are two things that can be done to fix this problem.

First, you can reduce the white space gen­er­ated by R. I use the fol­low­ing func­tion when sav­ing fig­ures in R.

savepdf <- function(file, width=16, height=10)
{
  fname <- paste("figures/",file,".pdf",sep="")
  pdf(fname, width=width/2.54, height=height/2.54,
      pointsize=10)
  par(mgp=c(2.2,0.45,0), tcl=-0.4, mar=c(3.3,3.6,1.1,1.1))
}

The width and height are in cen­time­tres. The ratio is about right for a beamer pre­sen­ta­tion, and also to fit two fig­ures on an A4 page.

Then I use the commands

savepdf("filename")
# Plotting commands here
dev.off()

That will gen­er­ate a pdf fig­ure of about the right size and shape for a doc­u­ment, and with nar­row mar­gins of white space, and save it in my fig­ures sub-​​directory.

The sec­ond trick is to trim the pdf files so there is no white space left. On a unix sys­tem, this is eas­ily achieved as follows.

pdfcrop filename.pdf filename.pdf

There are prob­a­bly win­dows and mac ver­sions of the same, but I haven’t used them. Adobe Acro­bat will also crop pdfs, but not from the com­mand line as far as I know.

To apply pdfcrop to every file in a direc­tory (using unix), save the fol­low­ing to a file called cropall.sh:

#!/bin/bash
 
for FILE in ./*.pdf; do
  pdfcrop "${FILE}" "${FILE}"
done

Make the file exe­cutable and run it.

In my post on Make­files, I explain how to include pdfcrop within a Makefile.

If you just use pdfcrop with­out first reduc­ing the white space in R, the pro­por­tions come out a lit­tle odd. So I tend to use both approaches together.

R (programming language) Space (architecture)

Published at DZone with permission of Rob J Hyndman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Harmonizing Space, Time, and Semantics: Navigating the Complexity of Geo-Distributed IoT Databases
  • Tips and Tricks for Efficient Coding in R
  • How to Get a Non-Programmer Started with R
  • Python vs. R: A Comparison of Machine Learning in the Medical Industry

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!