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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

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.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.