Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This is part 9 of our series and we are going to explore the features of two interesting types of charts that googleVis provides like histogram and calendar charts.
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)
To run this example we will first create an experimental data frame with:
Hist=data.frame(A=rpois(100, 10),
B=rpois(100, 20),
C=rpois(100, 30))
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.
Histogram
It is quite simple to create a Histogram with googleVis. We will use the “Hist” data frame we just created. You can see the variables of your data frame with head()
.
Look at the example below to create a simple histogram:
HistC <- gvisHistogram(Hist)
plot(HistC)
Exercise 1
Create a list named “HistC” and pass to it the “Hist” data frame as a histogram. HINT: Use gvisHistogram()
.
Exercise 2
Plot the the histogram. HINT: Use plot()
.
Options
To add a legend to your chart you can use:
options=list(
legend="{ position: 'top' }")
Exercise 3
Add a legend to the bottom of your histogram and plot it. HINT: Use list()
.
To decide the colours of your bars you can use:
options=list(
colors="['black', 'green', 'yellow']")
- Work extensively with the GoogleVis package and its functionality
- Learn what visualizations exist for your specific use case
- And much more
Exercise 4
Change the colours of the histogram’s bars to red, green and blue and plot it. HINT: Use colors
.
To set the dimensions of your histogram you can use:
options=list(
width=400, height=400)
Exercise 5
Set width
of your histogram to 500, its height
to 400 and plot it.
Calendar chart
It is quite simple to create a Calendar Chart with googleVis. We will use the “Cairo” data set. You can see the variables of “Cairo” with head()
.
Look at the example below to create a simple calendar chart:
CalC <- gvisCalendar(Cairo)
plot(CalC)
Exercise 6
Create a list named “CalC” and pass to it the “Cairo” data set as a calendar chart. HINT: Use gvisCalendar()
.
Exercise 7
Plot the the calendar chart. HINT: Use plot()
.
Options
You can add title to your chart and set the dimensions with:
options=list(
title="Title",
height=400)
Exercise 8
Add a title to your calendar chart, set height
to 500 and plot it. HINT: Use list()
.
You can change the features of your labels with:
options=list(calendar="{yearLabel: { Name: 'Times-Roman',
Size: 26, color: 'black', bold: false})
Exercise 9
Add labels to your chart ,set the of your labels to “Times-Roman”, their
size
to 30, their color
to black, make them bold and plot the chart.
To find more options about the cells you can use:
cellSize: 15,
cellColor: { stroke: 'red', strokeOpacity: 0.5 },
focusedCellColor: {stroke:'red'}
Exercise 10
Set the size of the cells to 10, the focused color to green and plot the chart.
Related exercise sets:
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.