How to export images in R?

How to export images in R?

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.

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( # to close the file

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Loan Eligibility Prediction in Python using
In this loan prediction project you will build predictive models in Python using to predict if an applicant is able to repay the loan or not.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.