<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/data-visualization-with-googlevis-exercises-part-4/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/06/25/data-visualization-with-googlevis-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=data-visualization-with-googlevis-exercises-part-4"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/Rplotset5.5.jpeg" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/Rplotset5.5-150x150.jpeg?resize=150%2C150" alt="" class="wp-image-6412" data-recalc-dims="1"></a><strong>Adding Features to your Charts</strong></p>
<p>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.</p>
<p>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!</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/06/25/data-visualization-with-googleVis-solutions-part-4/" target="_blank">here</a>.</p>
<p><strong>Package &amp; Data frame</strong></p>
<p>As you already know, the first thing you have to do is install and load the googleVis package with:<br>
<code>install.packages(&quot;googleVis&quot;)<br>
library(googleVis)</code></p>
<p>Secondly we will create an experimental data frame which will be used for our charts’ plotting. You can create it with:<br>
<code>df=data.frame(name=c(&quot;James&quot;, &quot;Curry&quot;, &quot;Harden&quot;),<br>
              Pts=c(20,23,34),<br>
              Rbs=c(13,7,9))</code></p>
<p><strong>NOTE</strong>: The charts are created locally by your browser. In case they are not displayed at once press F5 to reload the page.</p>
<p><strong>Customizing Chart</strong></p>
<p>We are going to use the two-axis Line Chart we created in <a rel="nofollow" href="http://www.r-exercises.com/2017/06/04/data-visualization-with-googlevis-exercises-part-1/" target="_blank">part 1</a>. This is the code we used, in case you forgot it:</p>
<p><code>LineC2 &lt;- gvisLineChart(df, &quot;name&quot;, c(&quot;Pts&quot;,&quot;Rbs&quot;),<br>
                       options=list(<br>
                         series=&quot;[{targetAxisIndex: 0},<br>
                         {targetAxisIndex:1}]&quot;,<br>
                         vAxes=&quot;[{title:'Pts'}, {title:'Rbs'}]&quot;<br>
                       ))<br>
plot(LineC2)</code></p>
<p><strong>Colours</strong></p>
<p>To set the color of every line we can use:<br>
<code> series=&quot;[{color:'green', targetAxisIndex: 0,</code></p>
<p><strong>Exercise 1</strong></p>
<p>Change the colours of your line chart to green and yellow respectively and plot the chart.</p>
<p><strong>Line Width</strong></p>
<p>You can determine the line width of every line with:<br>
<code>series=&quot;[{color:'green',targetAxisIndex: 0, lineWidth: 3},</code></p>
<p><strong>Exercise 2</strong></p>
<p>Change the line width of your lines to 3 and 6 respectively and plot the chart.</p>
<p><strong>Dashed lines</strong></p>
<p>You can tranform your lines to dashed with:<br>
<code>series=&quot;[{color:'green', targetAxisIndex: 0,<br>
                          lineWidth: 1, lineDashStyle: [2, 2, 20, 2, 20, 2]},</code></p>
<p>There are many styles and colours available and you can find them <a rel="nofollow" href="https://developers.google.com/chart/interactive/docs/lines" target="_blank">here</a>.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/25/data-visualization-with-googlevis-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=data-visualization-with-googlevis-exercises-part-4"></aside>
<div><strong>Learn more</strong> about using GoogleVis in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/mastering-in-visualization-with-r-programming/" target="_blank">Mastering in Visualization with R programming</a>. In this course you will learn how to:
<ul>
<li>Work extensively with the GoogleVis package and its functionality</li>
<li>Learn what visualizations exist for your specific use case</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 3</strong></p>
<p>Choose two different styles of dashed lines for every line of your chart from the link above and plot your chart.</p>
<p><strong>Point Shape</strong></p>
<p>With the <code>pointShape</code> option you can choose from a variety of shapes for your points.</p>
<p>We will use the scatter chart we built in <a rel="nofollow" href="http://www.r-exercises.com/2017/06/18/data-visualization-with-googlevis-exercises-part-3/" target="_blank">part 3</a> to see how it works. Here is the code:<br>
<code>ScatterCD &lt;- gvisScatterChart(cars,<br>
                             options=list(<br>
                               legend=&quot;none&quot;,<br>
                               pointSize=3,lineWidth=2,<br>
                               title=&quot;Cars&quot;, vAxis=&quot;{title:'speed'}&quot;,<br>
                               hAxis=&quot;{title:'dist'}&quot;,<br>
                               width=600, height=300))<br>
plot(ScatterCD)</code></p>
<p><strong>Exercise 4</strong></p>
<p>Change the shape of your scatter chart’s points to ‘square’ and plot it. <strong>HINT</strong>: Use <code>pointShape</code>.</p>
<p><strong>Exercise 5</strong></p>
<p>Change the shape of your scatter chart’s points to ‘triangle’, their point size to 7 and plot it.</p>
<p><strong>Edit Button</strong></p>
<p>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.<br>
<code>options=list(gvis.editor=&quot;Edit!&quot;))</code></p>
<p><strong>Exercise 6</strong></p>
<p>Add an edit button in the scatter chart you just created. <strong>HINT</strong>: Use <code>gvis.editor</code>.</p>
<p><strong>Chart with more options</strong></p>
<p>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.<br>
<code>LineCD2 &lt;- gvisLineChart(df, &quot;name&quot;, c(&quot;Pts&quot;,&quot;Rbs&quot;),<br>
                        options=list(<br>
                          series=&quot;[{color:'green',targetAxisIndex: 0, lineWidth: 3,<br>
                          lineDashStyle: [14, 2, 2, 7]},<br>
                          {color:'yellow',targetAxisIndex:1,lineWidth: 6,<br>
                          lineDashStyle: [10, 2]}]&quot;,<br>
                          vAxes=&quot;[{title:'Pts'}, {title:'Rbs'}]&quot;<br>
                        ))<br>
plot(LineCD2)</code></p>
<p><strong>Background color</strong></p>
<p>You can decide the background color of your chart with:<br>
<code>backgroundColor=&quot;red&quot;,</code></p>
<p><strong>Exercise 7</strong></p>
<p>Set the background color of your line chart to “lightblue” and plot it. <strong>HINT</strong>: Use <code>backgroundColor</code>.</p>
<p><strong>Title</strong></p>
<p>To give a title and decide its features you can use:<br>
<code>title=&quot;Title&quot;,<br>
                          titleTextStyle=&quot;{color:'orange',<br>
                                           fontName:'Courier',<br>
                                           fontSize:14}&quot;,<br>
</code></p>
<p><strong>Exercise 8</strong></p>
<p>Give a title of your choise to the line chart and set its font to blue, Courier of size 16. <strong>HINT</strong>: Use <code>titleTextStyle</code>.</p>
<p><strong>Curve Type &amp; Legend</strong></p>
<p>Another nice-looking choise that googleVis gives you is to display the lines like curves with:<br>
<code>curveType=&quot;function&quot;</code></p>
<p>You can also move the legend of your chart to the bottom with:<br>
<code>legend=&quot;bottom&quot;</code></p>
<p><strong>Exercise 9</strong></p>
<p>Smooth the lines of your line chart by setting the <code>curveType</code> option to function and move the <code>legend</code> to the bottom. <strong>HINT</strong>: Use <code>curveType</code> and <code>legend</code>.</p>
<p><strong>Axes features</strong></p>
<p>Finally you can “play” with your axes. This is an example:<br>
<code>vAxis=&quot;{gridlines:{color:'green', count:4}}&quot;,<br>
                          hAxis=&quot;{title:'City', titleTextStyle:{color:'red'}}&quot;,<br>
                          series=&quot;[{color:'yellow', targetAxisIndex: 0},<br>
                                   {color: 'brown',targetAxisIndex:1}]&quot;,<br>
                          vAxes=&quot;[{title:'val1'}, {title:'val2'}]&quot;,</code></p>
<p><strong>Exercise 10</strong></p>
<p>Give the title “Name” to your hAxis and color it orange. Separate your vAxis with 3 red gridlines. <strong>HINT</strong>: Use <code>titleTextStyle</code> and <code>gridlines</code></p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/06/11/data-visualization-with-googlevis-exercises-part-2/" title="Data Visualization with googleVis exercises part 2" target="_blank">Data Visualization with googleVis exercises part 2 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/06/20/data-visualization-with-googlevis-exercises-part-3/" title="Data Visualization with googleVis exercises part 3" target="_blank">Data Visualization with googleVis exercises part 3 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/06/04/data-visualization-with-googlevis-exercises-part-1/" title="Data Visualization with googleVis exercises part 1" target="_blank">Data Visualization with googleVis exercises part 1 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/06/25/data-visualization-with-googlevis-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=data-visualization-with-googlevis-exercises-part-4"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/YkT6kFxh9fg" height="1"></a></div>