Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In the eighth part of our series we are going to learn about the features of some interesting types of charts. More specifically we will talk about Annotation and Sankey 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
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. All charts require an Internet connection.
Annotation chart
It is quite simple to create an Annotation Chart with googleVis. We will use the “Stocks” dataset. You can see the variables of your dataset with head()
.
Look at the example below to create a simple Annotation Chart:
AnnoC <- gvisAnnotationChart(Stock)
plot(AnnoC)
Exercise 1
Create a list named “AnnoC” and pass to it the “Stock” dataset as an annotation chart. HINT: Use gvisAnnotationChart()
.
Exercise 2
Plot the the annotation chart. HINT: Use plot()
.
Set the variables
As you can see the annotation chart you built is empty so we have to fill it with some information. We will use the variables of ths “Stock” dataset for this purpose like this:
datevar="Date",
numvar="Value",
idvar="Device",
titlevar="Title",
annotationvar="Annotation"
- Work extensively with the GoogleVis package and its functionality
- Learn what visualizations exist for your specific use case
- And much more
Exercise 3
Use the example above to fill your annotation chart with the proper information and plot the chart.
Dimensions
You can use height
and width
to change the dimensions of the annotation chart.
options=list(width=500, height=300)
Exercise 4
Set height
to 700, width
to 500 and plot the chart. HINT: Use list()
.
Colours
You can change the colours of the lines with:
options=list(colors="['green','yellow']")
Exercise 5
Set the line colours to green and red and plot the chart.
With the fill
option you can color the relevant areas and adjust how filled these areas will be. Look at the example:
options=list(colors="['green','yellow']",fill=20)
Exercise 6
Set fill
to 50 and plot the chart.
Exercise 7
Now set fill
to 150 to see the difference and plot the chart.
Sankey Chart
Before creating a sankey chart we have to create a data frame that will help us as an example, so copy and paste this code to crate the data frame “exp” first:
exp <- data.frame(From=c(rep("A",3), rep("B", 3)),
To=c(rep(c("X", "Y", "Z"),2)),
Weight=c(6,9,7,9,3,1))
As you can see we created a data frame with the variabls “From”, “To” and “Weight”. You can use head()
in order to see them.
It is quite simple to create an Sankey Chart with googleVis. We will use the “exp” data frame.
Look at the example below to create a simple Sankey Chart:
SankeyC <- gvisSankey(exp )
plot(SankeyC)
Exercise 8
Create a list named “SankeyC” and pass to it the “exp” dataset as a sankey chart. HINT: Use gvisSankey()
.
Exercise 9
Plot the the sankey chart. HINT: Use plot()
.
You can change the link colours with:
options=list(sankey="{link: {color: { fill: 'red' } }
Exercise 10
Color the links of ths sankey chart green and plot it.
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.