How to export images in R?

This recipe helps you export images in R

Recipe Objective

ggplot2, plotly, lattice, highcharter packages are used for creating well knwon descriptive plots in R. To use these plots in different in presentations or editing, it needs to be exported in appropriate format to the local machine. ​

In this recipe, we will demonstrate how to export images in R.

Sentiment Analysis Project on eCommerce Product Reviews with Source Code

There are different ways to export images but we will use Cairo package to perform this task as it gives us the flexibility to format dimensions, background, resolution and type of image.

Step 1: Creating a scatter plot using ggplot2

Creating a scatter plot Annual Income vs Age using Mall_customers dataset ​

# Data manipulation package library(tidyverse) # ggplot for data visualisation library(ggplot2) # reading a dataset customer_seg = read.csv('R_186_Mall_Customers.csv') # creating a scatter plot Annual Income vs Age fig = ggplot(customer_seg, aes(x = Annual.Income..k.., y = Age)) + geom_point(aes(color = Gender)) + labs(title = "Annual Income vs Age") fig

Step 2: Exporting plot image in .png format

We will use Cairo package to export the plot image. ​

Syntax: Cairo(width = , height = , units =, file = , type , dpi = ) ​

where: ​

  1. width = width of the exported image
  2. height = height of the exported image
  3. units = units of the numerical values of width and height specified
  4. type = type of the exported file
  5. dpi = specifying the resolution of image
  6. file = Name of the file with the extension

# loading Cairo package install.packages("Cairo") library(Cairo) # using Cairo function in the package Cairo::Cairo(file="Scatter_plot_PNG_96.png", type="png", width=7, height=5, units="in", dpi=96) fig invisible(dev.off) # to close the file

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.