Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Hello everybody! This is the first part of an exciting data visualization series r exercises.com is going to provide you.
Data visualization involves the creation and study of the visual representation of data. The increasing amounts of data created by Internet activity make the “data visualization” tool totally necessary for data scientists.
One of the best packages that R language provides for this purpose is googleVis and guess what, we are going to see its amazing features together. In this first part we are going to see basic stuff of three useful types of charts (Line Chart, Bar Chart and Column Chart) before “digging deeper” in the next parts.
NOTE: The charts are created locally by your browser. In case they are not displayed at once press F5 to reload the page.
Read the examples below to understand the logic of what we are going to do and then test yous skills with the exercise set we prepared for you. Lets begin!
Answers to the exercises are available here.
Install the Package.
Exercise 1
Install and load the package googleVis.
Create a data frame
First of all let’s create an experimental data.frame
to use for all our plots. This is an example:
dfr=data.frame(name=c("GRE", "ARG", "BRA"),
val1=c(20,32,19),
val2=c(25,52,12))
Exercise 2
Create a data frame named “df”. Give as variables the “Pts” (Points) and “Rbs” (Rebounds) of three NBA players. Names and values are up to you.
Line Chart
To produce a Line Chart you can use:
LineC <- gvisLineChart(df)
plot(LineC)
Exercise 3
Create a list
named “LineC” and pass to it the “df” data frame you just created as a line chart. HINT: Use gvisLineChart()
.
Exercise 4
Plot the line chart. HINT: Use plot()
.
Line chart with two axis
Below there is an example of this type of Line Chart:
LineC2 <- gvisLineChart(df, "name", c("val1","val2"),
options=list(
series="[{targetAxisIndex: 0},
{targetAxisIndex:1}]",
vAxes="[{title:'val1'}, {title:'val2'}]"
))
plot(LineC2)
As you can see you create a list
(options) that corresponds values 1 & 2 in the two axes respectively.
Exercise 5
Create a single axis Line chart that displays only the “Pts” of the “df” data frame.
Exercise 6
Now create a two axis line chart that displays both “Pts” and “Rbs” of the “df” data frame. HINT: Use list()
.
Bar Chart
It is quite simple to create a bar chart with googleVis with:
BarC <- gvisBarChart(df)
plot(BarC)
Exercise 7
Create a list
named “BarC” and pass to it the “df” data frame you just created as a bar chart. HINT: Use gvisBarChart()
.
Exercise 8
Plot the bar chart. HINT: Use plot()
.
Column Chart
Column charts could be considered as vertical bar charts and are quite simple to be created too.
ColumnC <- gvisColumnChart(df)
plot(ColumnC)
Exercise 9
Create a list
named “ColumnC” and pass to it the “df” data frame you just created as a column chart. HINT: Use gvisColumnChart()
.
Exercise 10
Plot the column chart. HINT: Use plot()
.
Related exercise sets:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.