# Data Visualization in R – A Comprehensive Guide | R Graphics

## 1. Data Visualization in R – Objective

Today, in this R Tutorial, we will discuss the Data Visualization in R. Moreover, we will see the history and need for R Data Visualization. Also, we will discuss R Graphics and Data Visualization in R using ggplot2. Along with this, we will discuss the pros and cons of Data Visualization in R

So, let’s start the R Data Visualization Tutorial.

## 2. What is the R Data Visualization?

In R, the most appealing things are its ability to create data visualization in R with just a couple of lines of code. Also, it is an art of how to turn numbers into useful knowledge.

First, let us see the history of data visualization in R along with motivation for the same. Consequently, we will learn why R data visualization and types of it.

## 3. Motivation for Data Visualization in R

- As we know humans are outstanding at detecting patterns and structures with their eyes.
- Its methods try to explore these capabilities.
- Its methods also have several problems, particularly with large datasets.

## 4. History of R Data Visualization

### i. Evolution of Data Visualization

The below picture shows a complete evolution of R Data Visualization.

### ii. Scientific Data Visualization in R

- We use visualization to communicate Data and analysis.
- The leading software for statistical analyses is the statistical programming language R.
- The leading R extension of data visualization is ggplot2.

### iii. Use of R

**a**. Most of all we will use the environment RStudio for our work in R.

**b**. Rstudio in R has 4 panels

**Console**: This is the actual R window, you can enter R commands here. And thus execute them by pressing enter.**Source**: This is where we can edit scripts. It is where you should always be working. Control-enter sends selected codes to console.**Plots/Help**: This is where plots and helps pages will be shown.**Workspace**: Shows which objects you currently have.

**c**. Anything following a # symbol is treated as a comment.

## 5. Why R Data Visualization?

- Because it provides clear understanding of patterns in data
- Also it has an ability to detect hidden structures in data

As we are learning in this tutorial about data visualization. So for this, knowledge of Graphics, graphs in R, and graphics devices etc. is mandatory. Now, we will proceed to study about graphics.

## 6. R Graphics

### i. Standard Graphics

R Standard Graphics, available through package graphics, include several functions that provide statistical plots, like:

- Scatterplots
- Boxplots
- Piecharts
- Barplots etc.

We use these graphs which are typically a single function call.

Data Visualization in R Example: A scatterplot

Plot ( 1: 10 , sin ( 1: 10 ))

### ii. Graphics Devices

- Its functions produce output. That totally depends on the active graphics device.
- A screen is the default and more frequently device.
- R graphical devices, like the pdf device, the jpeg device, etc.
- The user just needs to open the graphics output device she/he wants. Hence, R takes care of producing the type of output required by the device.
- This means that to produce a certain plot on the screen or as a GIF R graphics file the R code is exactly the same. You only need to open the target output device before!
- Several devices may be open at the same time, but only one is the active device.

### iii. The Basics of the grammar of Graphics

**Key elements of a statistical graphic**:

- data
- aesthetic mappings
- geometric objects
- statistical transformations
- scales
- coordinates system
- Faceting

Now, let us discuss each of them.

**a. Aesthetic Mappings**

- It controls the relation between data variables and graphics variables:
- Also, it helps to map the Temperature variable of a data set into the X variable in a scatter plot.
- Basically, it helps to map the species of a plant into the color of dots in graphics.

**b. Geometric Objects**

It shows each observation by a point using the aesthetic mappings that map two variables in the data set into the x,y variables of the plot.

**c. Statistical Transformations**

- It allows us to calculate and also to do statistical analysis over the data in the plot.
- Also, the statistical transformation uses the data and approximates it by a regression line x,y coordinates.
- Basically, it counts occurrences of certain values.

**d. Scales**

It maps the data values into values in the coordinate system of the graphics device.

**e. Coordinate system**

We use it to plot the data.

– Cartesian

– Plot etc.

**f. Faceting**

It splits the data into subgroups and draws sub-graphs for each group. Now let us study in deep the Data Visualization in R using ggplot2

## 7. Data Visualization in R using ggplot2

**“As data visualization has different plotting system but ggplot2 is the plot which is mostly used.”**

What type of data visualization in R to use for what sort of problem? I will tell you things which helps you choose the right type of chart for your specific objectives. Also, helps how to implement it in R using ggplot2.

This blog is primarily geared towards those who have some basic knowledge of the R programming language. And also who want to make complex and nice looking charts with R ggplot2:

- Introduction to ggplot2
- Customizing the Look and Feel

### i. Introduction to ggplot

It is a plotting system. We use it to build for making professional looking. Also, use plots quickly with minimal code. It helps in to take care of many of the complicated details that make plotting difficult. Hence, ggplot is very different from base R plotting but also very flexible and powerful.

**It uses data frames as input**:

- Data must be in long format.
- This means that in each row is an observation and each column a variable.
- Use reshape2 to get data in long format.

### ii. Important things to remember for ggplot

- It was developed by Hadley Wickham as an implementation of the grammar of graphics.
- Basically, ggplot is relatively complete and powerful graphics package.
- It Can do many things but not 3D.

### iii. How to install the ggplot2 package

- Ggplot2 can be easily installed by typing.
- install.packages(“ggplot2”)
- Although, make sure that you are using the latest version of R to get the most recent version of ggplot2.

### iv. Application of ggplot2

**Aesthetics**: It refers to visual attributes that affect how data are displayed in a graphic, e.g., color, point size, or line type.**Geometric objects**: We use it for a visual representation of observations such as points, lines, polygons, etc.**Faceting**: Generally, it is applied to the same type of graph.**Annotation**: Moreover, it allows us to add text and/or external graphics to a ggplot.**Positional adjustments**: it helps to reduce overplotting of points.

### v. Why ggplot?

- Generally, it is used Professionally
- It’s very Pretty
- Also, it is easy to manipulate
- Although, it has great support online
- Also, it has knowledge transfers to other packages/languages
- It has steep learning curve
- Besides, it has lots of syntaxes
- Also, it can be slow
- Basically, it has defaulted to weird colors

Any Doubt yet in Data Visualization in R? Please comment.

## 8. What to Learn in Data Visualization in R

R Programming helps us to learn this art by offering a set of inbuilt functions and also libraries to build visualizations and present data. Moreover, before we move forward for the technical implementations of the visualization, let’s see first how to select the right chart type.

Selecting the Right Chart Type

**There are four basic presentation types**:

- Comparison
- Composition
- Distribution
- Relationship

In your day-to-day activities, you’ll come across the below listed 7 charts most of the time.

- Scatter Plot
- Histogram
- Bar & Stack Bar Chart
- Box Plot
- Area Chart
- Heat Map
- Correlogram

Now we will go to discuss each of them:

**a. Scatter Plot**

**When to use**:

To see the relationship between two continuous variables.

**b. Histogram**

**When to use:**

A histogram is used to plot continuous variable. Also, It helps to break the data into bins and shows frequency distribution of these bins. Thus, we can always change the bin size and see the effect it has on visualization.

**c. Bar & Stack Bar Chart**

**When to use**:

We use Bar charts to plot a categorical variable.

**d. Box Plot**

**When to use**:

Box Plots are used to plot a combination of categorical and continuous variables. Also, used for visualizing the spread of the data and detect outliers. Moreover, it shows five statistically significant numbers;

the minimum;

the 25th percentile;

the median;

the 75th percentile and

the maximum.

**e. Area Chart**

**When to use**:

We use it to show the continuity across a variable or data set. Almost it is same as a line chart. Also, we can use it for time series plots. Alternatively, also we can use it to plot continuous variables and analyze the underlying trends.

**f. Heat Map**

**When to use**:

We use it for an intensity of colors. it is also used to display a relationship between two or three or many variables in a two-dimensional image. Thus, it allows us to explore two dimensions of the axis and the third dimension by an intensity of color.

**g. Correlogram**

**When to use**:

We use it to test the level of correlation and also among the variable available in the dataset. Thus, the cells of the matrix can be shaded or colored to show the co-relation value.

## 9. Pros and Cons of Data Visualization in R

Let’s have a look at the pros and cons of data visualization in R applications.

### i. PROS

**a. Understanding**

To look into the business may be more appealing. And it’s easy to understand through graphics and charts when compared to a written document comprising text and numbers. Thus can attract a wider audience. Logically, it means a far reached. Also, widespread utilization of those business insights to arrive at better decisions.

**b. Efficiency**

Its app allows us to display a lot of information in a small space. While the process of decision making in business is inherently complex and multifaceted, displaying evaluation findings in a graphic can allow the companies to organize lots of interrelated information in useful ways.

**c. Location**

Its app that uses features like geographical maps and GIS can be especially relevant for extensive businesses when a location is so often a very relevant factor. We use maps to show business insights from different places, giving an idea of the severity of issues, the reasons behind them and also the workarounds to address them.

### ii. CONS

**a. Cost**:

Its applications cost a decent sum of money, and it may not be possible for especially small companies to spend that many resources upon purchasing them. Further, dedicated professionals need to be hired for generating reports and charts from them, which may inflate the costs involved even further. Small enterprises are often working in resource-limited settings, and also getting evaluation results in a timely manner can often be of high importance.

**b. Distraction**:

Although at times, the Data Visualization apps create reports and charts laden with highly complicated and fancy graphics, which may be tempting for the users to focus more on form than on function. The overall value of the graphic will be minimal if we first add visual appeal. In a resource-setting, it is also important to think carefully about how resources can best be used. And also not get caught up in the graphics trend without a clear purpose.

So, this was all on Data Visualization in R.

## 10. Conclusion – Data Visualization in R

Hence, we have studied data visualization in R in deep. Moreover, we have discussed all W’s. And we have also focused on ggplot2 in R which is mainly used in data visualization. Apart from ggplot2, we have also learned about visualization along with their pros and cons.

Still, if you have any doubt regarding Data Visualization in R, ask in the comment tab.