<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/r-weekly-bulletin-vol-viii/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-viii/"> R programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 1200px) 100vw, 1200px" alt="" class="wp-image-8632" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/Weekly-R-Vol-III.jpg?w=450&ssl=1" data-recalc-dims="1"></p>
<p>This week’s R bulletin will cover topics on plotting charts like saving the plot, adding a grid, and plotting multiple data sets in a single plot. </p>
<div></div>
<p>	<a data-link="https://twitter.com/share?text=Learn+some+interesting+R+programming+functions.+%40QuantInsti&amp;url=https://www.quantinsti.com/blog/r-weekly-bulletin-vol-viii/" target="_blank" rel="nofollow" href="https://twitter.com/share?text=Learn+some+interesting+R+programming+functions.+%40QuantInsti&url=https://www.quantinsti.com/blog/r-weekly-bulletin-vol-viii/"><span><span>We will also cover functions like download.file, file.copy, file.rename, and file.remove. </span><span>Click To Tweet<i></i></span></span></a>Hope you like this R weekly bulletin. Enjoy reading!</p>
<h3>Shortcut Keys</h3>
<p>1. Run current document – Ctrl+Alt+R<br>
2. Run from document beginning to current line – Ctrl+Alt+B<br>
3. Run from current line to document end – Ctrl+Alt+E</p>
<h3>Problem Solving Ideas</h3>
<h4>Saving a plot to a file</h4>
<p>R allows you to save a plot in different file formats, such as PNG, JPEG, or PDF. The example below outlines the process for saving plots in R.</p>
<p><strong>Example:</strong> We would like to generate a 2-year closing price series plot for BPCL, and then save the plot in a PNG file. We first call the png function and provide the desired filename, width, and height of the plot. We then plot the price series along with the desired arguments to the plot function. Finally, we close the graphics file using the dev.off function. The file is saved in the current working directory unless you specify some other desired path.</p>
<pre>library(quantmod)
bpcl = getSymbols(&quot;BPCL.NS&quot;, src = &quot;yahoo&quot;, from = &quot;2014-01-01&quot;, to = &quot;2016-01-01&quot;,
                  auto.assign = FALSE)

bpcl_cl = Cl(bpcl)

plot.ts(bpcl_cl, main = &quot;BPCL Price Series&quot;, xlab = &quot;Days&quot;, ylab = &quot;Close Price&quot;,
        type = &quot;l&quot;, col = &quot;red&quot;)</pre>
<p><img sizes="(max-width: 669px) 100vw, 669px" alt="" class="wp-image-8636" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/R-Weekly-8.1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Call the png function, Plot the vectors, and finally close the graphics file
png(&quot;Saving plot.png&quot;, width = 680, height = 480)
plot.ts(bpcl_cl, main = &quot;BPCL Price Series&quot;, xlab = &quot;Days&quot;, ylab = &quot;Close Price&quot;,
        type = &quot;l&quot;, col = &quot;red&quot;)
dev.off()</pre>
<p>pdf<br>
2</p>
<pre># Saving the plot in pdf

pdf(&quot;Saving plot.pdf&quot;)
plot.ts(bpcl_cl, main = &quot;BPCL Price Series&quot;, xlab = &quot;Days&quot;, ylab = &quot;Close Price&quot;,
        type = &quot;l&quot;, col = &quot;red&quot;)
dev.off()</pre>
<p>pdf<br>
2</p>
<h4>Adding a grid to the plot</h4>
<p>A grid can be added to a plot using the grid function. To plot a grid, we first call the plot function with type=“n” to initialize the graphics frame without displaying the data. In the next step, we call the grid function to draw the grid. Finally, we call the lines function to draw the graphics overlaid on the grid. If we want to have point instead of a line, we can use the points function.</p>
<p><strong>Example:</strong></p>
<pre>library(quantmod)
techm = getSymbols(&quot;TECHM.NS&quot;, src = &quot;yahoo&quot;, from = &quot;2015-01-01&quot;, to = &quot;2016-01-01&quot;,
                   auto.assign = FALSE)
techm_cl = coredata(Cl(techm))
days = index(Cl(techm))
# Adding grid to a line chart

plot(days, techm_cl, main = &quot;TECHM Price Series&quot;, xlab = &quot;Days&quot;, ylab = &quot;Close Price&quot;, type = &quot;n&quot;)
grid(col = &quot;red&quot;, lwd = 1.5)
lines(days, techm_cl)</pre>
<p><img sizes="(max-width: 705px) 100vw, 705px" alt="" class="wp-image-8639" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/R-Weekly-8.2.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Adding grid to a point chart

plot(days, techm_cl, main = &quot;TECHM Price Series&quot;, xlab = &quot;Days&quot;, ylab = &quot;Close Price&quot;, type = &quot;n&quot;)
grid(col = &quot;blue&quot;, lwd = 1.5)
points(days, techm_cl)</pre>
<p><img sizes="(max-width: 704px) 100vw, 704px" alt="" class="wp-image-8641" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/R-Weekly-8.3.png?w=450&ssl=1" data-recalc-dims="1"></p>
<h4>Plotting Multiple Datasets</h4>
<p>To plot multiple datasets we use a high-level function like plot, followed by a low-level function like the “lines” function. Since multiple datasets can have different X-axis and Y-axis ranges, it is important to set the range for the two axes in such a way that the plot incorporates all the data points from the multiple datasets.</p>
<p>In the example below, we are plotting the 1-year daily closing price series for two stocks, namely PNB and CANBK. We use the range function to determine the xlim and ylim parameters. Calling the range function on the close price series of the two datasets ensures that we plot all the data points. Thereafter we call the high-level “plot” function on the closing price series of PNB stock and defined the other necessary parameters. We then use the low-level “lines” function to add the closing price series for CANBK stock.</p>
<p><strong>Example:</strong></p>
<pre>library(quantmod)
pnb = getSymbols(&quot;PNB.NS&quot;, src = &quot;yahoo&quot;, from = &quot;2015-01-01&quot;, to = &quot;2015-12-31&quot;,
                  auto.assign = FALSE)

canbk = getSymbols(&quot;CANBK.NS&quot;, src = &quot;yahoo&quot;, from = &quot;2015-01-01&quot;, to = &quot;2015-12-31&quot;,
                    auto.assign = FALSE)

pnb_close = coredata(Cl(pnb))
canbk_close = coredata(Cl(canbk))
date = index(pnb)

main = &quot;PNB-CANBK Daily Close Price Chart for 2015&quot;
xlim = range(as.Date(date))
ylim = range(c(pnb_close, canbk_close))

plot(date, pnb_close, type = &quot;l&quot;, lty = 1, pch = 19, col = &quot;red&quot;, xlab = &quot;Months&quot;,
     ylab = &quot;Price&quot;, main = main, xlim = xlim, ylim = ylim)

# Add a line
lines(date, canbk_close, type = &quot;l&quot;, lty = 1, pch = 18, col = &quot;blue&quot;)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" alt="" class="wp-image-8644" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/R-Weekly-8.4.png?w=450&ssl=1" data-recalc-dims="1"></p>
<h3>Functions Demystified</h3>
<h4>download.file</h4>
<p>The download.file function helps download a file from a website. This could be a webpage, a csv file, an R file, etc. The syntax for the function is given as:</p>
<p><strong>download.file(</strong>url<strong>, </strong>destfile<strong>)</strong></p>
<p>where,<br>
url – The Uniform Resource Locator (URL) of the file to be downloaded<br>
destfile – the location to save the downloaded file, i.e. path with a file name</p>
<p><strong>Example:</strong> In this example, the function will download the file from the path given in the “url” argument, and saved it in the D drive within the “Skills” folder with the name “betawacc.xls”.</p>
<pre>url = &quot;http://www.exinfm.com/excel%20files/betawacc.xls&quot;
destfile = &quot;D:/Skills/wacc.xls&quot;
download.file(url, destfile)</pre>
<h4>file.copy and file.rename</h4>
<p>The file.copy function copies a file from one folder to another folder, while the file.rename function renames an existing file in a given folder.</p>
<p><strong>Example:</strong> The path of the file to be copied is mentioned as the first argument, and the location to copy is mentioned as the second argument in the function.</p>
<pre>file.copy(&quot;C:/Users/MyFolder/TATACHEM.csv&quot;, &quot;D:/Documents/TATACHEM.csv&quot;)</pre>
<p><strong>Example:</strong> The path of the file to be renamed is mentioned as the first argument, and the location of the file after renaming is mentioned as the second argument in the function. The syntax is given as:</p>
<pre>file.rename(from=(path of the file), to=(path of the file))</pre>
<h4>file.exists and file.remove</h4>
<p>The file.exists function is used to check whether a particular file is present in the set working directory or other folder specify as the argument to the function. The file.remove function is used to delete a particular file if it is present in the set working directory.</p>
<p><strong>Example:</strong> This will check whether the file “TATACHEM.csv” exists in the “MyFolder” folder.</p>
<pre>file.exists(&quot;C:/Users/MyFolder/TATACHEM.csv&quot;)
[1] TRUE</pre>
<p>This will delete the “TATACHEM.csv” file from the “MyFolder” folder.</p>
<pre>file.remove(&quot;C:/Users/MyFolder/TATACHEM.csv&quot;)</pre>
<h3><strong>Next Step</strong></h3>
<p>We hope you liked this bulletin. In the next weekly bulletin, we will list more interesting ways and methods plus R functions for our readers.</p>
<div><a target="_blank" rel="nofollow" href="https://www.quantinsti.com/wp-content/uploads/2017/05/QuantInsti%E2%84%A2_R_Bulletin-VIII.pdf"> Download the PDF Now!</a></div>
<p>The post <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-viii/">R Weekly Bulletin Vol – VIII</a> appeared first on <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/"></a>.</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="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-viii/"> R programming</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/eOflYONO97U"></a></div>