Site icon R-bloggers

Data Visualization with googleVis exercises part 3

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

Scatter & Bubble chart

This is the third part of our data visualization series and at this part we will explore the features of two more of the 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 Installation

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.

Scatter chart

It is quite simple to create a scatter chart with googleVis. We will use the cars dataset. Look at the example below:
ScatterC <- gvisScatterChart(cars) plot(ScatterC)

Exercise 1

Create a list named “ScatterC” and pass to it the cars dataset as a scatter chart. HINT: Use gvisScatterChart().

Exercise 2

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

Titles

It is time to learn how to enhance the appearance of our googleVis charts. We shall give a title to the chart and also name hAxis and vAxis. Look at the example:
options=list(title="Cars", vAxis="{title:'speed'}", hAxis="{title:'dist'}" )

Exercise 3

Name your chart “Cars”, your chart’s vAxis “speed”, your chart’s hAxis “dist” and plot the chart. HINT: Use list().

Size

You can adjust the size with width and height.

Exercise 4

Set your chart’s width to 600 and height to 300.

Legend

You can deactivate your chart’s legend if you set it to “none”.

Exercise 5

Deactivate your chart’s legend.

< 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

Point size & Line width

You can determine the size of the chart’s points with pointsize and also choose to unite them with line with linewidth. For example:
pointSize=4,linewidth=3

Exercise 6

Set point size to 3 and line width to 2.

Bubble Chart

Another amazing type of chart that googleVis provides is the bubble chart. You can create a simple Bubble Chart of the Fruits dataset like this:
BubbleC <- gvisBubbleChart(Fruits) plot(BubbleC)

Exercise 7

Create a list named “BubbleC” and pass to it the Fruits dataset as a bubble chart. HINT: Use gvisBubbleChart().

Exercise 8

Plot the chart. HINT: Use plot().

Bubble Chart’s Features

As you can see, you created a bubble chart but it seems to be useless. In order to make it useful you should pass to it some of your dataset’s variables as features. It depends on what you want to be displayed and how. If you type head(Fruits) you can easily recognize the numeric variables of your dataset. Then you can use them like this:
BubbleC <- gvisBubbleChart(Fruits,idvar="VAR1", xvar="VAR2", yvar="VAR3", colorvar="VAR4", sizevar="VAR5")

Exercise 9

Find the numeric variables of Fruits, then set “Fruit” as idvar, “Sales” as xvar, “Expenses” as yvar, “Year” as colorvar and “Profit” as sizevar and plot your chart. HINT: Use head().

Data range

You can also adjust the minimum and maximum number of hAxis and vAxis that you want to be displayed. Look at the example below:
options=list( hAxis='{minValue:50, maxValue:150}')

Exercise 10

Set your hAxis range from 70 to 130 and your vAxis range from 50 to 100.

Related exercise sets:

  1. Data Visualization with googleVis exercises part 2
  2. Data Visualization with googleVis exercises part 1
  3. Shiny Application Layouts exercises part 3
  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.