Data Visualization with googleVis exercises part 4
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Adding Features to your Charts
We saw in the previous charts some basic and well-known types of charts that googleVis offers to users. Before continuing with other, more sophisticated charts in the next parts we are going to “dig a little deeper” and see some interesting features of those we already know.
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.
Customizing Chart
We are going to use the two-axis Line Chart we created in part 1. This is the code we used, in case you forgot it:
LineC2 <- gvisLineChart(df, "name", c("Pts","Rbs"),
options=list(
series="[{targetAxisIndex: 0},
{targetAxisIndex:1}]",
vAxes="[{title:'Pts'}, {title:'Rbs'}]"
))
plot(LineC2)
Colours
To set the color of every line we can use:
series="[{color:'green', targetAxisIndex: 0,
Exercise 1
Change the colours of your line chart to green and yellow respectively and plot the chart.
Line Width
You can determine the line width of every line with:
series="[{color:'green',targetAxisIndex: 0, lineWidth: 3},
Exercise 2
Change the line width of your lines to 3 and 6 respectively and plot the chart.
Dashed lines
You can tranform your lines to dashed with:
series="[{color:'green', targetAxisIndex: 0,
lineWidth: 1, lineDashStyle: [2, 2, 20, 2, 20, 2]},
There are many styles and colours available and you can find them here.
Exercise 3
Choose two different styles of dashed lines for every line of your chart from the link above and plot your chart.
Point Shape
With the pointShape
option you can choose from a variety of shapes for your points.
We will use the scatter chart we built in part 3 to see how it works. Here is the code:
ScatterCD <- gvisScatterChart(cars,
options=list(
legend="none",
pointSize=3,lineWidth=2,
title="Cars", vAxis="{title:'speed'}",
hAxis="{title:'dist'}",
width=600, height=300))
plot(ScatterCD)
Exercise 4
Change the shape of your scatter chart’s points to ‘square’ and plot it. HINT: Use pointShape
.
Exercise 5
Change the shape of your scatter chart’s points to ‘triangle’, their point size to 7 and plot it.
Edit Button
A really useful and easy feature that googleVis provides is the edit button which gives the user the ability to customize the chart in an automated way.
options=list(gvis.editor="Edit!"))
Exercise 6
Add an edit button in the scatter chart you just created. HINT: Use gvis.editor
.
Chart with more options
Now let’s see how we can create a chart with many features that can enhance its appearance. We will use again the 2-axis line that we used before.
LineCD2 <- gvisLineChart(df, "name", c("Pts","Rbs"),
options=list(
series="[{color:'green',targetAxisIndex: 0, lineWidth: 3,
lineDashStyle: [14, 2, 2, 7]},
{color:'yellow',targetAxisIndex:1,lineWidth: 6,
lineDashStyle: [10, 2]}]",
vAxes="[{title:'Pts'}, {title:'Rbs'}]"
))
plot(LineCD2)
Background color
You can decide the background color of your chart with:
backgroundColor="red",
Exercise 7
Set the background color of your line chart to “lightblue” and plot it. HINT: Use backgroundColor
.
Title
To give a title and decide its features you can use:
title="Title",
titleTextStyle="{color:'orange',
fontName:'Courier',
fontSize:14}",
Exercise 8
Give a title of your choise to the line chart and set its font to blue, Courier of size 16. HINT: Use titleTextStyle
.
Curve Type & Legend
Another nice-looking choise that googleVis gives you is to display the lines like curves with:
curveType="function"
You can also move the legend of your chart to the bottom with:
legend="bottom"
Exercise 9
Smooth the lines of your line chart by setting the curveType
option to function and move the legend
to the bottom. HINT: Use curveType
and legend
.
Axes features
Finally you can “play” with your axes. This is an example:
vAxis="{gridlines:{color:'green', count:4}}",
hAxis="{title:'City', titleTextStyle:{color:'red'}}",
series="[{color:'yellow', targetAxisIndex: 0},
{color: 'brown',targetAxisIndex:1}]",
vAxes="[{title:'val1'}, {title:'val2'}]",
Exercise 10
Give the title “Name” to your hAxis and color it orange. Separate your vAxis with 3 red gridlines. HINT: Use titleTextStyle
and gridlines
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.