Data visualization packages in R-Part I

A good understanding of data is one of the key essentials to designing effective Machine Learning (ML) algorithms. Realizing the structure and properties of the data that you are working with is crucial in devising new methods that can solve your problem. Visualizing data includes the following:

  • Cleaning up the data
  • Structuring and arranging it
  • Plotting it to understand the granularity

R, one of the few widely-used programming languages for ML, has many data-visualization libraries.

In this article, we will explore two of the most commonly used packages in R for analyzing dataм—dplyr and tidyr.

Using dplyr and tidyr

dplyr and tidyr, which were created by Hadley Wickham, are maintained by Hadley Wickham and the RStudio team. An impressive advantage with these libraries is the pipeline operator “%>% that can be used with any of the functions in these libraries. It acts as a pipeline by supplying the output of the left argument as an input to the right-side function thus giving us the flexibility to apply any no. of functions in sequence.

 

tidyr

The operations in the tidyr library include the following:

  1. gather() and its complement spread()
  2. separate() and its complement unite()


gather()

This operation is used to make messy data more structured by gathering all the information into key-value pairs. It takes in a data frame and duplicates the columns as required and then organizes the data into key-value pairs. This is an important task when information about the same attributes is spread across multiple columns in a data frame.

Usage:

OR

key, value names of the columns to be created in output
‘…’ indicates the specification regarding the columns to be included while gathering information. Either single variable names can be used to specify the column names or a list of column names using “:” operator can also be mentioned. To exclude a column from a list, “-“ operator has to be used before the name of that particular column
na.rm takes a Boolean value and is an indicator whether to discard the rows with missing values in output. If set TRUE, the rows with missing values are discarded
convert takes a Boolean value and is used to activate type conversion on key value column
factor_key when set to FALSE treats the key values as character vector. Else they are perceived as factors and the original ordering of the columns are retained


spread()

This operation does the opposite of gather(). It breaks the key-value pair structure and spreads them across multiple columns.

Usage

key, value meanings remain same
fill the value with which both explicit and implicit missing values will be filled in the final output
drop takes a Boolean value and when set to FALSE, will retain the factors with missing values by filling them with “fill” value
sep when set to NULL has no special effect on the names of the columns but when it is not NULL, the column values are given by “key_name”+ “sep” + “key_value”


separate()

This operation is used when a single column contains multiple information. It breaks the column information and builds two independent columns for easier access of data.

unite()

This operation performs the negation of separate().


dplyr

The operations and internal methods (that act as facilitators) of the dplyr library include the following:

1. select()

This method is used to select a specific subset of the variables that are present in the library. A smaller set of original data can be separated by using the names of the columns :

  1. Using a regular expression: select(df, matches(reg))
  2. Using a character string “str”  which is a part of the column name: select(df, fun(str)), fun can take the following values:
    • contains(str): {All the columns that contain the character string str are selected}
    • ends_with(str): Selected when the column name ends with str
    • starts_with(str): Selected when the column name starts with str
    • num_range(“c”,1:N) – selects all the columns with the names c1, c2, c3, …cN
    • Using a set of column names:  select(df, one_of(c(“column1”, “column2”,…))) and a specific column could be removed by mentioning it with a negation: select(df, -column1)
    • To select everything use : everything(): Used to select all columns of the table



2. Extracting data by using observations through the following:

  1. distinct(df): Removes duplicate rows
  2. sample_frac(df, f, replace): – f is a fraction value between 0 and 1. Replace takes up a Boolean value and when set to TRUE will select fraction f rows randomly with replacement. sample_n is similar but selects n rows randomly.
  3. top_n(df, n, par): Selects top n rows based on the parameter par.
  4. slice(df, N1:N2): Slices the rows from N1 to N2 out of original data.
  5. filter(df, req) is also used for the similar purpose of reducing the no. of observations present in data.



3. group_by(df, variable): Groups the data based on the given variable. This is a silent function and does not show any visual effect on the data, however, it is effective when applied in the pipeline along with other functions.

4. summarise(): This operation is used to collect the summary of the data frame.

  1. To apply a summary function over all the columns use summary_each(df, funs(sum_func)). sum_func can take the following values:
    • First, last, nth {denotes the particular value of each vector}, n and n_distinct denote the no. of values and no. of distinct values in each vector respectively.
    • Other functions are, min, max, median, sd, var and mean.

Usage:




5. mutate() is used to compute a new value and add it as an additional variable or to simply add a new variable to the data frame.

Usage:


There are various window functions present in dplyr and in R that can used along with mutate to compute a new value for each vector in data frame. 



6. dplyr provides methods to perform various types of join functions too. 

Usage:


Function list

  1. left_join() → performs the join of rows matching in tab_2 with tab_1
  2. right_join() → performs the join of rows matching in tab_1 with tab_2
  3. inner_join() → performs the join of only rows present in both tab_1 and tab_2
  4. full_join() → performs the join of all the rows present in both the tables irrespective of matching

The 4 join operations, which are mentioned above, are known as mutating joins (that add some data to the existing ones). semi_join(), anti_join() are known as filter joins as they filter out the data based on the given condition and refine it out. There are other merging functions available like intersect(), union(), setdiff(), bind_rows(), bind_cols() and combine().



7. Another important feature of dplyr is the arrange() function that lets us sort the data in ascending order based on a specific column value. If required, the data can be sorted in descending order using desc() function.

  1. Implementation: arrange(column_name) -- for ascending order
  2. arrange(desc(col_name)) -- for descending order

Using functions in ggplot2() will help us visualize the data in a more efficient way through plotting them.

The examples below explain how raw data can be processed by using the  functions mentioned above and then be plotted to visualize the average change in value of stock for every month of the year.

Example

For bar graph visualization with time as color coding parameter



bargraph

For scatter plot of same data


scaplot


Regression models are designed to understand patterns that cannot be observed easily in the data. R equips you with libraries similar to those that we discussed in this article. If you think deeper, they are not only data-visualization tools, but also tools to improve your aptitude towards understanding granular aspects of data.

In the next article, we'll explore more data-visualization libraries.

About the Author

Krunal is passionate about exactly 3 things in life. Writing, travelling and marketing. In the rest of his time he learns, or fascinates himself with, Game Theory, Big Data, Artificial Intelligence and whole lotta food.
Share