Data Visualization with googleVis exercises part 1

June 4, 2017

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

Line, Bar and Column Charts

Hello everybody! This is the first part of an exciting data visualization series r 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"),

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)

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"),
series="[{targetAxisIndex: 0},
vAxes="[{title:'val1'}, {title:'val2'}]"

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)

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)

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().

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)