How to Generate and Save Fancy Graphs and 3D Plots as PDF in R Programming
Tags: r, datascience, data, beginners
Published: 2023-07-03 13:57:20

This is a continuation of my [previous article](https://dev.to/anvilicious/fundamentals-of-data-analysis-in-r-programming-r-cheat-sheet-code-included-3m1p) on fundamentals of data analysis using R. You can access all the code used in both articles from my [Github repo](https://github.com/BDFL669/Data_Analysis_with_R/blob/main/Fundamentals_of_Data_Analysis_in_R_Programming.ipynb). 

In this section, various visually appealing graphs are showcased, such as 3D plots, level plots, contour plots, interactive plots, and parallel coordinates. 

##Scatter Plot
We begin by creating a 3D scatter plot using the scatterplot3d package.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89gafdi6qddj8shsdc2g.png)

##Heatmaps

A heat map is a visual representation of a two-dimensional data matrix, which can be created using the heatmap() function in R. The following code demonstrates how to calculate the similarity between various flowers in the iris data using dist() and subsequently visualize it as a heat map.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzj68dlrs4b2bdd1zkij.png)

##Level Plots
A level plot is a type of graphical representation that displays the variation in a two-dimensional dataset using contour-like regions or color gradients. To generate a level plot, you can utilize the levelplot() function available in the lattice package. The grey.colors() function can be used to create a vector of gamma-corrected gray colors. Alternatively, you can use the rainbow() function, which generates a vector of continuous colors.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9wg6gdxk1oitxs59jdk.png)

##Contour Plots
A contour plot is a graphical representation that illustrates the variation of a two-dimensional dataset by displaying lines or regions of constant values, often used to visualize continuous data or functions. You can create contour plots in R using the contour() and filled.contour() functions from the graphics package, or by utilizing the contourplot() function available in the lattice package.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w16vfqfcnaw145g3z56t.png)
To interpret the above contour plot, you can examine the lines or regions of constant values, which represent the levels or contours of the data. The contour lines indicate areas of similar values, with closer lines indicating a steeper change in the underlying data. The spacing between contour lines can provide insights into the data's gradient or rate of change. Additionally, the colors or shading used in a filled contour plot further indicate the magnitude or intensity of the data values.

##Surface Plots
An alternative method to visualize a numerical matrix is by using a 3D surface plot, which can be created using the persp() function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnn5b5ak8tquysbbangs.png)
##Parallel Cordinates
Parallel coordinates also offer an effective means of visualizing multidimensional data, and you can generate a parallel coordinates plot using the parcoord() function from the MASS package, or by utilizing the parallelplot() function available in the lattice package.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9i2uxzl9fd78c76gehl.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz6koweonwbscxxxr11f.png)

##Save Charts into Files
If you generate numerous graphs during data exploration, it is advisable to save them as files. R offers several functions for this purpose. Below are examples of saving charts as PDF and PS files using pdf() and postscript() respectively. Additionally, you can generate picture files in BMP, JPEG, PNG, and TIFF formats using bmp(), jpeg(), png(), and tiff() respectively. Remember to close the files (or graphics devices) by using graphics.off() or dev.off() after plotting.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mr5r9ckcf4grtej7h4ko.png)

##Summary
I hope you had as much fun reading this as I did creating the content. Remember to install packages before imprting them using the library() method. Shoutout to Zhao's 'R and Data Mining' Textbook, wouldn't have done this without you!