<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/getting-started-with-plotly-basic-plots/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/01/10/getting-started-with-plotly-basic-plots/?utm_source=rss&utm_medium=rss&utm_campaign=getting-started-with-plotly-basic-plots"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2017/01/three-d-1781592__340.png?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-3867" src="https://i2.wp.com/r-exercises.com/wp-content/uploads/2017/01/three-d-1781592__340-150x150.png?resize=150%2C150" data-recalc-dims="1"></a>Plotly is a d3 based graphing library used to produce interactive and high quality graphs in R. In the following exercises, we will look at the basic plots’ syntax and some basic features in the plotly functions.<br>
We will use datasets available in base R packages.</p>
<p>Refer to the documentation of the plotly packages when in need of help with the functions and arguments.<br>
Also, refer to this <a target="_blank" rel="nofollow" href="https://plot.ly/r/reference/">link</a> for figure references</p>
<p>Installation of the package is straight-forward as it is available on CRAN.<br>
Use this command <code>install.packages(‘plotly’)</code> to install the package. Do not forget to load the package before trying out the exercises.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/01/10/getting-started-with-plotly-basic-plots-solutions/">here</a>.</p>
<div>
<aside><img src="http://r-exercises.com/2017/01/10/getting-started-with-plotly-basic-plots/?utm_source=rss&utm_medium=rss&utm_campaign=getting-started-with-plotly-basic-plots"></aside>
<div><strong>Learn more</strong> about Plotly in Section 17 <em>Interactive Visualizations with Plotly</em> of the online course <a target="_blank" rel="nofollow" href="http://r-exercises.com/product/data-science-and-machine-learning-bootcamp-with-r/">Data Science and Machine Learning Bootcamp with R</a>.</div>
</div>
<p><strong>Exercise 1</strong></p>
<p>a. Generate a histogram for the first column of <code>iris</code> dataset using the <code>plot_ly()</code> function.<br>
For the first run, supply only one argument to the function and see the message it returns.<br>
identify the argument that you need to include to specify a chart type.</p>
<p><strong>Exercise 2</strong></p>
<p>Identify the argument to specify number of bins and generate a histogram with 20 bins using the same data.</p>
<p>Notice the behaviour of plot on mouse hover. There are also a few options available on the right top corner of the plot to zoom, save the plot etc.</p>
<p><strong>Exercise 3</strong></p>
<p>a. Generate a scatter plot for the first two columns of the <code>iris</code> dataset. Identify and use the appropriate arguments to get the plot with first column on the <code>x</code> axis and second column on <code>y</code> axis.<br>
b. Using the <code>color</code> argument, specify the color such that data points are colored based on the <code>Species</code> column. HINT: <code>color</code> argument takes a vector of same length of number of data points data points with the level info.</p>
<p><strong>Exercise 4</strong></p>
<p>To the same code in the previous exercise, use the <code>size</code> argument to specify the markers’ size based on the third column of the iris dataset.</p>
<p><strong>Exercise 5</strong></p>
<p>a. Generate a line plot for 100 values of random normal distribution with the default mean and standard deviation.<br>
HINT: Use index values on <code>x</code> axis. Use <code>type=&quot;scatter&quot;</code> and <code>mode=&quot;lines&quot;</code> to get a line chart</p>
<p><strong>Exercise 6</strong></p>
<p>b. Save the previous plot in an object p. Use <code>layout</code> function to add an appropriate title to the plot.</p>
<p><strong>Exercise 7</strong></p>
<p>To learn how to generate a bar plot, we will simulate sample data and look at how to prepare the data before plotting.<br>
Run the below code to generate the data.<br>
<code> cat &lt;- c(rep(&quot;A&quot;, 2), rep(&quot;B&quot;, 4), rep(&quot;C&quot;, 8))</code></p>
<p>Data has to be in the form of <code>Levels</code> and their <code>Counts</code> . Therefore, using <code>table()</code> function to summarize and <code>as.data.frame</code> to create a data frame.<br>
<code> df &lt;- as.data.frame(table(cat)) </code></p>
<p>a. Now, using <code>type=&quot;bar&quot;</code> and the appropriate <code>x</code> and <code>y</code> arguments, create a bar graph.<br>
b. Add color to each bar using <code> color </code> argument, based on the categorical levels.</p>
<p><strong>Exercise 8</strong></p>
<p>Pie chart is an alternative way of representing categorical data, but, when the levels in the data are more, bar chart is preferred.<br>
Data has to be prepared in the same way as we do it for bar chart. The arguments vary a little. Instead of <code>x</code> and <code>y</code>, we use <code>labels</code> and <code>values</code>.</p>
<p>Generate a pie chart using the same data from the previous exercise and appropriate arguments.</p>
<p>Notice how the tick marks and the axes appear behind the chart.</p>
<p><strong>Exercise 9</strong></p>
<p>For the first column in the iris dataset, generate a box plot using the <code>box</code> trace type.<br>
HINT:For a simple box plot, just the <code>y</code> argument and <code>type</code> argumnets are given.</p>
<p><strong>Exercise 10</strong></p>
<p>Add another argument to the code used in the previous exercies to generate mutliple box plots for the first column of iris dataset, where each box corresponds to data of particular <code>Species</code>.<br>
HINT : Remember the argument we used to specify the color for each species in Exercise 3.</p>
<p>In the coming exercise sets on plotly, we will work on some advanced plots and see how to use argumnets from the <code>layout</code> function to manipulate the plot layout and settings.</p>
<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://r-exercises.com/2017/01/10/getting-started-with-plotly-basic-plots/?utm_source=rss&utm_medium=rss&utm_campaign=getting-started-with-plotly-basic-plots"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/LCcYEQx9pQo"></a></div>