Site icon R-bloggers

Data Visualization with googleVis exercises part 6

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

Package

As you already know, the first thing you have to do is install and load the googleVis package with:
install.packages("googleVis") library(googleVis)

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 ) plot(Geo)

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.
Geo=gvisGeoChart(Exports, locationvar="Country", colorvar="Profit") plot(Geo)

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) plot(GoogleMap)

Exercise 4

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

< aside class='stb-icon'>
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.
options=list(showTip=TRUE, showLine=TRUE, mapType='terrain', useMapTypeControl=TRUE)

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.

Related exercise sets:

  1. Data Visualization with googleVis exercises part 2
  2. Data Visualization with googleVis exercises part 3
  3. Data Visualization with googleVis exercises part 4
  4. Explore all our (>1000) R exercises
  5. Find an R course using our R Course Finder directory

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

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.