470,594 Members | 1,558 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Lesson 4 (part2) – data management and visualization

nbiswas
149 100+
Sorting data in a vector is quite easy in R. You simply pass the vector to the sort function and new sorted vector is generated.
The default sort order is ascending, but this can be changed using the argument decreasing=TRUE.
However, in our scenario the data is not in a vector, it is in a data frame. Let's examine how we sort data stored contained in a data frame.
The data frame n.2010 contains three columns: Year, Name and Frequency.
There is no need to keep the Year attribute so we decide to filter our data frame to remove the Year column.
Here is an example using the bracket notation of how we can filter out the Year column by specifying a character vector of column names within the brackets.
If you are familiar with SQL, this task would be equivalent to using projection to specify the columns in the SELECT list for an SQL statement.
We use the head() function to verify that the Year column has been removed. The function shows that the data is still in its original sort order by name.
To find the most popular names we must sort by Frequency in decreasing or descending order.
We can't use the function sort as we did with vectors, but with data frames we will use the order function within our bracket notation.
Here we want the data sorted by decreasing frequency values and all of the columns should be included in our output.
The final expression converts the Factor vector into a simple character vector to determine that the most popular name was: Ethan, followed by Liam, Jacob, Lucas, and Noah.
Whenever we have explored a data set and wish to save it for future reference we can use one of the write() family of functions.
Here we see some examples of exporting the sorted male names from the year 2010 to a comma separated values or csv file and also to a tab delimited file.
In many data frames the row names are not really used to identify data so we have used the row.names=FALSE option to avoid writing the row name information in our output file.
So far we have worked with data within data frames. Now let's visualize trends in our data using Data Visualizations or graphics.
In this task we would like to provide an interactive experience so that the user can provide a baby name and then view a single visualization of its popularity from 1917 to 2010.

Let's get started.
So far we have imported data from external files, but we have not obtained input from users before.
We can use the readline() function to read data from text files, websites, or in this example we will use the function to obtain data from the standard input device or keyboard. We specify the prompt with the optional argument.
To visualize trends in baby names we would decide to use a scatter plot visualization based on frequencies over the years 1917 to 2010.
To do this we create a new data frame which is a subset of frequencies for the name provided by the user. The built-in toupper() function is used to convert the input character string to all uppercase so we can match the data stored in the data.frame. We also only require the two variables of Year and Frequency. If you are familiar with SQL you can imagine how this query would look using an SQL SELECT statement, but in R we do things a bit different.
We then sort the data within the data frame and call the plot() function.
The plot function uses the values provided to generate a visualization.
Plot is an example of a generic function in R as it can accept many different data formats and it will attempt to create a visualization.
Let's take a look at the visualization.
When the data is provided to the plot function as a data frame of 2 variables, plot will create a scatterplot using circles for each observation.
Notice how default labels and scales were generated.
The x and y axis labels are determined based on the defined column names.
We will examine a few additional commonly used options for the plot() function next.
Most visualizations should have a title. The main argument is used to provide a title for a visualization.
The label names can be specified for each axis and the method of plotting can be changed from the default to lines, stairs, or other representations.
Let's look at another example with our baby name data frame.
This plot uses the same data, but it uses the paste function to construct a more precise title for our chart and the type of plot is changes from using circles to using stairs.
R has been able to help us spot trends in our data set across 2 variables. It is fairly obvious from this data that the first name or given name of "GRANT" was most popular in the early 1960s and from our earlier analysis we know that it was definitely not one of the top 5 names of newborns in 2010.
This plot uses the same data, but it uses the paste function to construct a more precise title for our chart and the type of plot is changes from using circles to using stairs.
R has been able to help us spot trends in our data set across 2 variables. It is fairly obvious from this data that the first name or given name of "GRANT" was most popular in the early 1960s and from our earlier analysis we know that it was definitely not one of the top 5 names of newborns in 2010.
Let's look at one more example of trend analysis and also learn a few more plotting options along the way.
In this dataset we plan to examine the maximum and minimum temperatures during the month of July 2013 in Toronto.
After the data is loaded we check the structure of the data frame and decide to move forward.
Note that there are some missing values of wind speed in our data, but we will only examine temperatures so we can ignore this variable at this time.
We would like our graph to include both maximum and minimum values for each day of the month.
Therefore, we need to determine the range of values to set our axis properly.
Here we have created a variable called yrange that contains the lowest temperature and the highest temperature for the month.
As we plot the data we will specify a line graph type with a line width of 6 pixels and a colour of blue. The y-axis limit is specified using our yrange vector and we have also included a title in our initial plot.
At this point we are not finished as we now wish to add the maximum temperatures and our axis labels to the same plot.
The par() function can be used to control various features for our graph. Here we are telling R that our next plot() function call should reuse the existing plot.
Now we add the red line of maximum temperatures for the month. We also decide to add the x and y axis labels at this time.
The final step is to include the axis range of values to our chart.
R has helped us explore how July was very warm in the middle of July followed by an extended cool period lasting until the end of the month.
We can use summary statistics to determine the mean or average temperature in the month of July or examine trends like we did in the previous example.
Now we would like to examine the distribution of temperatures during the month. A distribution of values are often visualized using a histogram were the data is grouped into categories and the categories are visualized.
Here is an example that shows the most frequent maximum temperature in July was between 22 and 24 degrees Celsius.
The breaks argument can be used to determine the size of the categories.
So now if someone asks what was it like in July 2013 in Toronto you will have lots to discuss based on learning R.
R has many more data visualization capabilities.
One of the most popular R extensions for graphics is called ggplot2 by Hadley Wickham and if your goal is to use R to create data visualizations then exploring the ggplot2 package is a good idea.
Sep 4 '14 #1
1 4194
zmbd
5,440 Expert Mod 4TB
nbiswas:
Please provide proper citations for these articles.
-z
Sep 29 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by ben moretti | last post: by
reply views Thread by Enorme Vigenti | last post: by
2 posts views Thread by Bryan.Fodness | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.