Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In the second part of our series we are going to meet three more googleVis charts. More specifically these charts are Area Chart, Stepped Area Chart and Combo Chart.
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:
df=data.frame(name=c("James", "Curry", "Harden"),
Pts=c(20,23,34),
Rbs=c(13,7,9))
NOTE: The charts are created locally by your browser. In case they are not displayed at once press F5 to reload the page.
Area chart
It is quite simple to create an area chart with googleVis with:
AreaC <- gvisBarChart(df)
plot(AreaC)
Exercise 1
Create a list named “AreaC” and pass to it the “df” data frame you just created as an area chart. HINT: Use gvisAreaChart()
.
Exercise 2
Plot the area chart. HINT: Use plot()
.
Stepped Area chart
Creating a stepped area chart is a little different than the area chart. You have to set the X and Y variables and also make it stacked in order to display the values correctly. Here is an example:
SteppedAreaC <- gvisSteppedAreaChart(df, xvar="name",
yvar=c("val1", "val2"),
options=list(isStacked=TRUE))
plot(SteppedAreaC)
Exercise 3
Create a list named “SteppedAreaC” and pass to it the “df” data frame you just created as a stepped area chart. You should also set the X variable as the players’ names and Y variable as their values. HINT: Use gvisSteppedAreaChart()
, xvar
and yvar
.
Exercise 4
Plot the stepped area chart. HINT: Use plot()
.
- Work extensively with the GoogleVis package and its functionality
- Learn what visualizations exist for your specific use case
- And much more
Exercise 5
Now transform your stepped area chart to stacked to correct it and plot it.
Combo chart
The next chart we are going to meet is the combination of lines and bars chart known as combo chart. You can produce it like this:
ComboC <- gvisComboChart(df, xvar="country",
yvar=c("val1", "val2"),
options=list(seriesType="bars",
series='{1: {type:"line"}}'))
plot(ComboC)
Exercise 6
Create a list named “ComboC” and pass to it the “df” data frame you just created as a combo chart. You should also set the X variable as the players’ names and Y variable as their values. HINT: Use gvisComboChart()
, xvar
and yvar
.
Exercise 7
Plot the chart. What kind of chart do you see? HINT: Use plot()
.
In order to add the bars we have to set it as the example below.
options=list(seriesType="bars",
series='{1: {type:"line"}}')
Exercise 8
Transform the chart you just created into a combo chart with bars and lines and plot it. HINT: Use list()
.
Exercise 9
In the previous exercise “Pts” are represented by the bars and “Rbs” by the lines. Try to reverse them.
You can easily transform your combo chart into a column chart or a line chart just be setting series='{1: {type:"line"}}'
to 2
Exercise 10
Transform the combo chart into a column chart and then into a line 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.