In ggplot2, what function do you use to map variables in your data to visual features of your plot?

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by David Mark from Pixabay

programming has become one of the best data analytics tools especially when it comes for visual analytics. A great community contribution makes it easier to learn, use and share for the effective visualization. It is imperative to say that proper visualization is a very important factor for data scientists & AI specialists. Even if you are only interested to work with business communication with impactful visualizations, R can provide you a comprehensive way of work where you have full freedom to play with your data and create useful graphs for your audiences. It is an open-sourced tool by the way. RStudio is the most favorable IDE(Integrated Development Environment) for R.

ggplot2

ggplot2 is the most popular data visualization package in the R community. It was created by Hadley Wickham in 2005. It was implemented based on Leland Wilkinson’s Grammar of Graphics — a general scheme for data visualization which breaks up graphs into semantic components such as scales and layers. While using ggplot2, you provide the data, call specific function, map your desired variables to aesthetics, define graphical arguments, rest it will take care! For details, you can go through its documentation.

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image source : tidyverse , ggplot2

tidyverse

tidyverse is a collecttion of packages for data science introduced by the same Hadley Wickham. ‘tidyverse’ encapsulates the ‘ggplot2’ along with other packages for data wrangling and data discoveries. More details can be found in its documentation.

Install Packages

Let’s install the required packages first. You do not need to install any package more than once in our system unless willing to upgrade it. Note : If you install tidyverse, then you do not need to install ggplot2 separately!

  • tidyverse for All plots
  • ggalt for Dumbbell plot
  • GGally for Scatter Matrix plot
  • ggridges for Ridge Plot

Load Packages

Now we need to load our packages. Unlike installing, loading packages is required every time you start your system.

Explore the Datasets

In this exercise we will use four datasets. Two of them are standard datasets and used worldwide for practicing data visualizations. these are iris and diamonds datasets. Other two are specially curated datasets for this work purpose. names.csv has the data of three female names’ uses along the years from 1880 to 2017 and life_expectency.csv contains contains fifteen countries’ life expectancy in years for 1967 and for 2007. Please download these two datasets either from my github repository or from google drive whichever is convenient. Note : all these datasets are open-sourced.

Now, Let’s import the datasets

Here are three options to check on your imported data,

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author
In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author
In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

About the Functions

We will use several functions from the ggplot2 package. These functions work together to yield the desired graphs. Every ggplot2 graph starts with the function ggplot() . It basically creates the coordinate system. Over this the graphical layers are added. The first argument of this function is the input dataset for the intended graph. Then comes the geom functions which add the layers of plotting on the coordinate system according to its geom i.e. geom_point, geom_line, geom_bar etc. Every geom function needs a mapping argument. This defines how the variables in the dataset are mapped to visual properties. The aesthetic function aes() is assigned to the mapping argument. The main arguments of the aes() function are axes augments-x, yand differentiating arguments like color, size, fill, alpha. The differentiating arguments become common featured arguments when they are put outside of the aes() function. ggtitle(), xlab(), ylab(), theme() these functions are used for the labelling and thematic attributes. Note : You can find detail of these functions in the help tab by executing the command — ?function_name , like ?geom_point.

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

The functions can be organized in below templates

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Photo by Gábor Juhász on Unsplash

dataset- iris

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Basic Scatter Plot

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Scatter Plot with feature differentiation by color & shape

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Scatter Plot with feature differentiation by size & transparency

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Photo by Emma Louise Comerford on Unsplash

dataset- names

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
image by Author

Basic Line Plot

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Line Plot with feature differentiation by color & line type

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Photo by All Bong on Unsplash

dataset- diamonds

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Basic Bar Plot

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Basic Bar Plot with polar transformation

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Basic Bar Plot- 2

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Stacked Bar Plot

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Stacked Bar Plot in same height

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Grouped Bar Plot

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Stacked Bar Plot with polar transformation

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Stacked Bar Plot with polar transformation- 2

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Photo by Viktor Forgacs on Unsplash

dataset- iris

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Basic Scatter Matrix Plot

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Scatter Matrix Plot with feature differentiation

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Photo by Clarisse Meyer on Unsplash

dataset- diamonds

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Basic Histogram

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Stacked Histogram

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Photo by CHUTTERSNAP on Unsplash

dataset- diamonds

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Basic Box Plot

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Grouped Box Plot

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

Box Plot with polar transformation

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Image by Author

want to believe that you find this writeup very helpful and I recommend you to practice further these graphs with your own datasets. This can be a good start for visual analytics and your journey towards data science !!

In ggplot2, what function do you use to map variables in your data to visual features of your plot?
Photo by Chirag Saini on Unsplash