Data Visualization with googleVis exercises part 6

July 11, 2017

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

Geographical Charts

In part 6 of this series we are going to see some amazing geographical charts that googleVis provides.

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.


As you already know, the first thing you have to do is install and load the googleVis package with:

NOTE: The charts are created locally by your browser. In case they are not displayed at once press F5 to reload the page.

Geo Chart

It is quite simple to create a Geo Chart with googleVis. We will use the “Exports” dataset. First let’s take a look at it with head(Exports). As you can see there are three variables (“Country”, “Profit”, “Online”) which we are going to use later.
Look at the example below to create a simple geo chart:
Geo=gvisGeoChart(Exports )

Exercise 1

Create a list named “GeoC” and pass to it the “Exports” dataset as a geo chart. HINT: Use gvisGeoChart().

Exercise 2

Plot the the geo chart. HINT: Use plot().

Furthermore you can add much more information in your chart by using the locationvar and colorvar options to color the countries according to the their profit. Look at the example below.

Exercise 3

Color the countries of your geo chart according to their profit and plot it. HINT: Use locationvar and colorvar.

Google Maps

It is quite simple to create a Google Map with googleVis. We will use the “Andrew” dataset. First let’s take a look at it with head(Andrew) to see its variables. Look at the example below to create a simple google map:
GoogleMap <- gvisMap(Andrew)

Exercise 4

Create a list named “GoogleMap” and pass to it the “Andrew” dataset as a google map. HINT: Use gvisMap().

Learn more about using GoogleVis in the online course Mastering in Visualization with R programming. In this course you will learn how to:

  • Work extensively with the GoogleVis package and its functionality
  • Learn what visualizations exist for your specific use case
  • And much more

Exercise 5

Plot the the google map. HINT: Use plot().

As you can see there ane no data points on it as we did not select something yet. We have to select the latitude and longtitude variables for the dataset like the example below.
GoogleMap <- gvisMap(Andrew,"LatLong" )

Exercise 6

Display the map by addind the “LatLong” variable to your list and plot it.

Exercise 7

Display the “Tip” variable on your google map just like you displayed the “LatLong” and plot it.

There are some useful options that gvisMap() provides to you that can enhance your map. Check the example below.

Exercise 8

Deactivate the Tip information from your map, plot the map and then enable it again. HINT: Use showTip.

Exercise 9

Enable useMapTypeControl and plot the map.

Exercise 10

Set the mapType by default to “terrain” and plot the map.

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)