Data visualization with googleVis exercises part 10

August 6, 2017
By

[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.

Timeline, Merging & Flash charts

This is part 10 of our series and we are going to explore the features of some interesting types of charts that googleVis provides like Timeline, Flash and learn how to merge two googleVis charts to one.

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 & Data frame

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

Secondly we will create an experimental data frame which will be used for our charts’ plotting. You can create it with:
datTLc <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
Name=c("Washington", "Adams", "Jefferson",
"Adams", "Jefferson", "Burr"),
start=as.Date(x=rep(c("1789-03-29", "1797-02-03",
"1801-02-03"),2)),
end=as.Date(x=rep(c("1797-02-03", "1801-02-03",
"1809-02-03"),2)))

You can explore the “datTLC” data frame with head().

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

Timeline Chart

It is quite simple to create a timeline chart with googleVis. We will use the “datTLC” data frame we just created.
Look at the example below to create a simple timeline chart:
TLC <- gvisTimeline(data=datTLc)
plot(TLC)

Exercise 1

Create a list named “TLC” and pass to it the “datTLC” data frame as a timeline chart. HINT: Use gvisTimeline().

Exercise 2

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

You can select the variables you want as rows and columns with:
TLC <- gvisTimeline(data=dataframe,
rowlabel="var1",
barlabel="var2",
start="var3",
end="var4")
plot(TLC)

Exercise 3

Put “Name” as rowlabel, “Position” as barlabel, “start” as start “end” as end and plot the chart.

Options

You can group your chart by row or not with:
options=list(timeline="{groupByRowLabel:true}")

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 4

Group your timeline chart NOT by rowlabel and plot it.

You can set the colours and size of your chart with:
options=list(timeline="{groupByRowLabel:false}",
backgroundColor='yellow',
height=300,
colors="['blue', 'brown']"))
plot(TLC)

Exercise 5

Set the background color of your chart to white, the “Position” colours to red and green respectively, the height to 400 and plot it.

Merging charts

We will now see how to merge two charts to one. For this purpose we are going to use a Geo Chart and a Table which we saw in parts 6 & 7 respectively.

Geo <- gvisGeoChart(Exports, "Country", "Profit",
options=list(width=400, height=400))
Table <- gvisTable(Exports,
options=list(width=320, height=400))

After you create these two charts you can merge them with:
GeoTable <- gvisMerge(Geo,Table, horizontal=TRUE)
plot(GeoTable)

Exercise 6

Create a Geo chart and Table like the example above and merge them. HINT: Use gvisMerge().

Flash charts

All the following charts require a Flash player.

Motion chart

The most exciting type of chart that googleVis provides, in my opinion, is the motion chart. It is quite simple to create a motion chart with googleVis. We will use the “Fruits” data set for this example. You can see the variables of your data set with head().
Look at the example below to create a simple motion chart:

MotionC=gvisMotionChart(Fruits,
idvar = "Fruit",
timevar = "Year"
)
plot(MotionC)

Exercise 7

Create a list named “MotionC” and pass to it the “Fruits” data set as a motion chart. HINT: Use gvisMotionChart().

Exercise 8

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

As you saw the variables were set automatically, but you can set them as you want with:
MotionC=gvisMotionChart(Fruits,
idvar = "Fruit",
timevar = "Year",
xvar = "Expenses",
yvar = "Sales",
sizevar ="Profit",
colorvar = "Location")
plot(MotionC)

Exercise 9

Create a list named “MotionC” and pass to it the “Fruits” data set as a motion chart. You can use the example above or you can use the variables differently to see the differences. HINT: Use gvisMotionChart().

Exercise 10

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

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.



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

Sponsors

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)