<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/plotting-wind-highways-using-rwind/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://allthiswasfield.blogspot.com/2018/11/plotting-wind-highways-using-rwind.html"> long time ago...</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<div lang="en-US"></div>
<div lang="en-US">Hi there!</div>
<div lang="en-US"></div>
<div lang="en-US">Our manuscript about rWind R package has been recently accepted for publication in <a rel="nofollow" href="https://onlinelibrary.wiley.com/doi/pdf/10.1111/ecog.03730" target="_blank">Ecography</a>! As you know, rWind is a tool used to download and manage wind data, with some utilities that make easy to include wind information in ecological or evolutionary analyses (<a rel="nofollow" href="https://www.tandfonline.com/doi/abs/10.1080/15230406.2017.1403376" target="_blank">or others!</a>).</div>
<div lang="en-US"></div>
<div lang="en-US">Though there are several examples in the Supporting material of the publication, and others in the <a rel="nofollow" href="https://cran.r-project.org/web/packages/rWind/vignettes/Shortest_paths.html" target="_blank">vignette</a> included in the CRAN version, here you have a full tutorial with the main utilities of rWind… enjoy!</div>
<div lang="en-US"></div>
<h3 lang="en-US"><u><b>Computing wind connectivity of Azores Archipelago</b></u></h3>
<div><u><b></b></u></div>
<div lang="en-US"><u><b></b></u></div>
<div lang="en-US"></div>
<div align="justify" lang="en-US">In this example, we will compute wind connectivity of Azores Islands with different points of Europe, Africa and North America during June 2018. I will divide each task of this tutorial in different sections for clarification.</div>
<div align="justify" lang="en-US">
<div><a rel="nofollow" href="https://i1.wp.com/4.bp.blogspot.com/-r-qzXxqV3WE/W_RCLAkvrtI/AAAAAAAAAS8/GPg3gQtMpjg9flQMBP9uMn85upxC7SbuACLcBGAs/s1600/paths.png?ssl=1" target="_blank"><img border="0" src="https://i1.wp.com/4.bp.blogspot.com/-r-qzXxqV3WE/W_RCLAkvrtI/AAAAAAAAAS8/GPg3gQtMpjg9flQMBP9uMn85upxC7SbuACLcBGAs/s400/paths.png?resize=450%2C725&ssl=1" data-original-width="450" width="450" data-original-height="725" data-recalc-dims="1"></a></div>
</div>
<h4 align="justify" lang="en-US"> </h4>
<h4 align="justify" lang="en-US">Downloading wind time series using lubridate and wind.dl_2()</h4>
<div align="justify" lang="en-US"></div>
<div align="justify" lang="en-US">In the last version of rWind (1.0.4), we can use lubridate to create a list of dates which will be used latter with wind.dl_2 to download wind time series. The object obtained with wind.dl_2 is an “rWind_series” “list”. This object can be easily used with other rWind functions as we’ll see later.</div>
<div align="justify" lang="en-US"></div>
<div align="justify" lang="en-US"></div>
<pre><code> # Make sure that you have the last version of rWind  <br>   <br> devtools::install_github(&quot;jabiologo/rWind&quot;)  <br> library(rWind)  <br> library(lubridate)  <br>   <br> # Here, we use ymd_hms from lubridate package to create a sequence of dates  <br>   <br> dt &lt;- seq(ymd_hms(paste(2018,6,1,12,00,00, sep=&quot;-&quot;)),  <br>      ymd_hms(paste(2018,6,30,12,00,00, sep=&quot;-&quot;)),by=&quot;1 days&quot;)  <br>   <br> # Now we can use wind.dl_2 with this sequence of dates. Have into account  <br> # that it could take a while, they are 30 datasets and it's a big area.   <br>   <br> ww &lt;- wind.dl_2(dt,-85,5,20,60)  <br></code></pre>
<div align="justify" lang="en-US"></div>
<div align="justify" lang="en-US"></div>
<div align="justify" lang="en-US">
<h4 align="justify" lang="en-US">Obtaining maximum wind speed during the time series with dplyr</h4>
<div align="justify" lang="en-US"></div>
<div align="justify" lang="en-US">We can use the rWind function called “tidy” to play with the “rWind_series” “list” object to obtain, for example, the maximum speed reported for each cell in the study area during the time series.
<pre><code> # After tidy our wind data, we can use dplyr utilities to easily  <br> # obtain several metrics from our wind data. Notice that wind average  <br> # is a special case, and it will be discussed later  <br>   <br> t_ww &lt;- tidy(ww)  <br>   <br> library(dplyr)  <br> g_ww &lt;- t_ww %>% group_by(lat, lon)  <br>   <br> # Now, we select only the maximum speed values for each coordinate  <br>   <br> max_ww &lt;- g_ww %>% summarise(speed = max(speed))  <br> maxw &lt;- cbind(max_ww$lon, max_ww$lat, max_ww$speed)  <br> head(maxw)  <br>   <br> # We can also check the maximum speed value in the whole study area   <br>   <br> max(maxw[,3])  <br>   <br> # Finally, we can transform this data into a raster layer  <br>   <br> rmax &lt;- rasterFromXYZ(maxw)  <br> acol &lt;- colorRampPalette(c(&quot;white&quot;, &quot;blue&quot;, &quot;darkblue&quot;))  <br> plot(rmax, col=acol(1000), main= &quot;Maximum wind speed reported&quot;,  <br>     xlab=&quot;Longitude&quot;, ylab=&quot;Lattitude&quot;)  <br>   <br> library(rworldmap)  <br> lines(getMap(resolution = &quot;high&quot;), lwd=2)  <br></code></pre>
<p></p>
<div><a rel="nofollow" href="https://i0.wp.com/4.bp.blogspot.com/-h9C1SQKcfYc/W_RLcm49ydI/AAAAAAAAATQ/nHaIF2PZjugd9JGgfx61aTF_7QTA1VrCQCLcBGAs/s1600/max_speed.png?ssl=1" target="_blank"><img border="0" src="https://i0.wp.com/4.bp.blogspot.com/-h9C1SQKcfYc/W_RLcm49ydI/AAAAAAAAATQ/nHaIF2PZjugd9JGgfx61aTF_7QTA1VrCQCLcBGAs/s400/max_speed.png?resize=450%2C725&ssl=1" data-original-width="450" width="450" data-original-height="725" data-recalc-dims="1"></a></div>
<h4 align="justify" lang="en-US"> </h4>
<h4 align="justify" lang="en-US">Computing averages of wind speeds and directions </h4>
<div align="justify" lang="en-US"></div>
<div align="justify"><span lang="en-US">Since wind data is a vectorial magnitude given in two components (U and V), to compute speed and direction averages we should use both vectors. To make it easier, rWind includes wind.mean function. Notice that wind.mean is specially </span><span lang="en-US">developed to work with wind.dl_2 outputs, so it will require “rWind_series” object as input.</span></div>
</div>
</div>
<div align="justify" lang="en-US">
<pre><code> # We can easily use wind.mean() function  <br>   <br> w_mean &lt;- wind.mean(ww)  <br>   <br> # And then use wind2raster directly to create a raster layer  <br>   <br> r_mean &lt;- wind2raster(w_mean)  <br><br> # We can plot a subset of this raster around Azores Islands.  <br> # Using &quot;arrowDir&quot; we can include arrows for wind direction with   <br> # &quot;Arrowhead&quot; function from &quot;shape&quot; R package  <br>   <br> plot(r_mean$wind.speed, main= &quot;Wind speed and direction average&quot;,<br>      col=acol(1000), xlim=c(-38,-18), ylim=c(33,43))  <br> alpha &lt;- arrowDir(w_mean)  <br> library(shape)  <br> Arrowhead(w_mean$lon, w_mean$lat, angle=alpha,   <br>      arr.length = 0.2, arr.type=&quot;curved&quot;)  <br> lines(getMap(resolution = &quot;low&quot;), lwd=4)  <br></code></pre>
<p></p>
<div><a rel="nofollow" href="https://i1.wp.com/2.bp.blogspot.com/-p0EVkGbqvxs/W_ROtMgk2pI/AAAAAAAAATc/5yGpmEJ8nfIvdjbVeRRyLhdF8xH3FiejQCLcBGAs/s1600/azores.png?ssl=1" target="_blank"><img border="0" src="https://i2.wp.com/2.bp.blogspot.com/-p0EVkGbqvxs/W_ROtMgk2pI/AAAAAAAAATc/5yGpmEJ8nfIvdjbVeRRyLhdF8xH3FiejQCLcBGAs/s400/azores.png?resize=450%2C725&ssl=1" data-original-width="450" width="450" data-original-height="725" data-recalc-dims="1"></a></div>
<p></p>
<h4 align="justify"><span lang="en-US">W</span><span lang="en-US">ind connectivity between Azores and mainland</span></h4>
<div align="justify"></div>
<div align="justify"><span lang="en-US">To complete this tutorial, we will use the downloaded wind data to compute wind connectivity between several points in mainland </span><span lang="en-US"><span lang="en-US">and Azores Archipelago</span>. This kind of measures can be useful in order to establish theories about island colonization by plants or other organisms that could be influenced by wind currents. We will also use this connectivity to compute least cost paths between two locations, in order to virtually plot a wind highway connecting mainland with Azores islands.</span></div>
<div align="justify"></div>
<pre><code> # First, we define our locations. 7 locations in the East   <br> # and 7 in the West. The last one is a point in the Azores islands  <br>   <br> loc &lt;- matrix(c(-7, -4, -1, -9, -6,-10,-15,   <br>         -60, -56,-63,-76,-76,-81,-80, -27,  <br>         55, 50, 45, 40, 35, 30, 25,   <br>         55, 50, 45, 40, 35, 30, 25, 39), 15, 2)  <br>   <br> colnames(loc) &lt;- c(&quot;lon&quot;, &quot;lat&quot;)  <br> rownames(loc) &lt;- c(seq(1,14,1), &quot;Azores&quot;)  <br>   <br> # Check how &quot;loc&quot; is looking   <br>   <br> tail(loc)  <br>   <br> # You can plot them  <br> #plot(r_mean$wind.speed, col=acol(1000))  <br> #lines(getMap(resolution = &quot;low&quot;), lwd=2)  <br> #points(loc, col=&quot;red4&quot;, pch = 19, cex = 2)  <br></code></pre>
</div>
<div align="justify" lang="en-US">
<div align="justify"><span lang="en-US">Now, we’ll execute the next </span><span lang="en-US">tasks</span><span lang="en-US">:</span></div>
<div align="justify"></div>
<ol>
<li><span lang="en-US">We’ll use wind2raster to obtain raster l</span><span lang="en-US">a</span><span lang="en-US">yers of wind direction and speed of each day in the time series. </span></li>
<li><span lang="en-US">Then, for each day, we</span><span lang="en-US">‘</span><span lang="en-US">ll compute the conductance matrix, </span><span lang="en-US">a matrix with connectivity values between every single cell of the entire study area.</span></li>
<li><span lang="en-US">Later, with each conductance matrix, we’ll compute the cost </span><span lang="en-US">between </span><span lang="en-US">our</span><span lang="en-US"> locations</span><span lang="en-US">and </span><span lang="en-US">will </span><span lang="en-US">store them in a cost_list object.</span></li>
<li><span lang="en-US">Finally, for each day, i</span><span lang="en-US">f the Cost is not Inf</span><span lang="en-US">inite</span><span lang="en-US">, </span><span lang="en-US">we’ll </span><span lang="en-US">get the shortest path between </span><span lang="en-US">two </span><span lang="en-US">selected locations, and </span><span lang="en-US">we’ll</span><span lang="en-US">store </span><span lang="en-US">it</span><span lang="en-US"> into a “paths” object.</span></li>
</ol>
<p></p>
<pre><code> # 1) Raster layers from wind time series  <br>   <br> layers &lt;- wind2raster(ww)  <br>   <br> # 2) Conductance from wind direction and speed  <br>   <br> Conductance &lt;- flow.dispersion(layers, type=&quot;passive&quot;,  <br>                 output=&quot;transitionLayer&quot;)  <br>   <br> # 3) and 4) Using a loop to compute cost between  <br> # locations and least cost paths between location 11  <br> # and Azores islands (as an example).  <br>   <br> cost_list &lt;- array(NA_real_, dim=c(15,15,30))  <br> paths &lt;- list(1:30)  <br>   <br> library(gdistance)  <br>   <br> for (i in 1:30){  <br>  cost_list[,,i] &lt;- costDistance(Conductance[[i]],loc)  <br>  if (costDistance(Conductance[[i]],loc[11,] , loc[15,]) != Inf){  <br>   paths[[i]] &lt;- shortestPath(Conductance[[i]], loc[11,], loc[15,],  <br>                 output=&quot;SpatialLines&quot;)  <br>   print(i)  <br>  }  <br> }<br></code></pre>
<div align="justify"><span lang="en-US">Now, we will manage a little bit the results to plot them in a cool way.</span></div>
<div align="justify"></div>
<div align="justify"><span lang="en-US"><br></span></div>

<p></p>
<pre><code> # Transform costs in connectivity  <br>   <br> connectivity &lt;- 1/cost_list  <br>   <br> # Obtain connectivity averages  <br>   <br> conn_avg &lt;- apply(connectivity, c(1, 2), mean, na.rm = TRUE)  <br>   <br> # Here we filter only connectivity from mainland to Azores  <br> # As connectivity has arbitrary magnitude and units, we will   <br> # multiply connectivity values 1000 times to avoid decimal zeros.  <br>   <br> conn_azores &lt;- conn_avg[,15]  <br> conn_azores &lt;- conn_azores *1000  <br>   <br> # Finally, we can plot connectivity values relative to the width  <br> # of the connections between mainland and Azores. You can power  <br> # the values to increase the differences between them.  <br>   <br> plot(r_mean$wind.speed, main= &quot;Connectivity from mainland to Azores&quot;,<br>     col=acol(1000))  <br> lines(getMap(resolution = &quot;low&quot;), lwd=1)  <br> for (i in 1:14){  <br>  lines(rbind(loc[i,], loc[15,]), lwd = conn_azores[i]^3, col=&quot;red4&quot;)  <br> }  <br> points(loc, col=&quot;red&quot;, pch = 19, cex = 1.5)  <br> text( loc+2, labels=c(round(conn_azores[1:14], 2),&quot;Azores&quot;),  <br>    col=&quot;white&quot;, cex= 1.5, font=2)  <br></code></pre>
<p></p>
<div><a rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-CVH8P2JnOW8/W_SS5L5v5eI/AAAAAAAAATw/OcMXT6cKAGQlzw6H0WXmgNNWSbaGwiKzwCLcBGAs/s1600/connectivity.png?ssl=1" target="_blank"><img border="0" src="https://i1.wp.com/3.bp.blogspot.com/-CVH8P2JnOW8/W_SS5L5v5eI/AAAAAAAAATw/OcMXT6cKAGQlzw6H0WXmgNNWSbaGwiKzwCLcBGAs/s400/connectivity.png?resize=450%2C667&ssl=1" data-original-width="450" width="450" data-original-height="667" data-recalc-dims="1"></a></div>
<p>Last but not least, we can use the shortest paths stored for each day to plot a density heatmap. This can help us to visualize wind corridors across our study area (our wind highways!).</p>
<pre><code> # We'll use spatstat library to convert the shortest  <br> # paths lines into an psp object.  <br>   <br> library(spatstat)  <br>   <br> paths_clean &lt;- paths[!sapply(paths, is.null)]  <br> paths_merged &lt;- paths_clean[[1]]  <br> for (h in 2:length(paths_clean)) {  <br>  paths_merged &lt;- rbind(paths_merged,paths_clean[[h]])  <br> }  <br> paths_psp &lt;- as(paths_merged, &quot;psp&quot;)  <br>   <br> # Now, we can obtain a kernel density of the paths  <br> # and convert it into a raster layer that can be plotted.  <br> # You can play with sigma value to modify the smoothing.  <br>   <br> lines_kernel &lt;- density(paths_psp, sigma=1, dimyx=c(350,410))  <br> kernel &lt;- raster(lines_kernel)  <br> #kernel[kernel&lt;(maxValue(kernel)*0.1)]&lt;-NA  <br>   <br> ext &lt;- extent(r_mean$wind.speed)  <br> kernel &lt;- extend(kernel, ext)  <br> kernel[is.na(kernel)] &lt;- minValue(kernel)  <br>   <br> acol &lt;- colorRampPalette(c(&quot;grey40&quot;,&quot;darkblue&quot;,&quot;red2&quot;,<br>                               &quot;orange&quot;,&quot;yellow&quot;,&quot;white&quot;))  <br> plot(kernel, col=acol(1000), zlim=c(-0.01,5),  <br>       main = &quot;Least cost paths density&quot;, xlab=&quot;Longitude&quot;,  <br>       ylab=&quot;Lattitude&quot;)  <br> lines(getMap(resolution = &quot;high&quot;), lwd=2)<br></code></pre>
<div><a rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-r-qzXxqV3WE/W_RCLAkvrtI/AAAAAAAAATA/H4p61GsR79Et4hq3KFUhYOs4E_qFaM9vwCPcBGAYYCw/s1600/paths.png?ssl=1" target="_blank"><img border="0" src="https://i2.wp.com/1.bp.blogspot.com/-r-qzXxqV3WE/W_RCLAkvrtI/AAAAAAAAATA/H4p61GsR79Et4hq3KFUhYOs4E_qFaM9vwCPcBGAYYCw/s400/paths.png?resize=450%2C725&ssl=1" data-original-width="450" width="450" data-original-height="725" data-recalc-dims="1"></a></div>
<p>Isn’t it cool?</p>
<p> That’s all for now!</p>
<p></div>
<div align="justify" lang="en-US"></div>
<div lang="en-US">

</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://allthiswasfield.blogspot.com/2018/11/plotting-wind-highways-using-rwind.html"> long time ago...</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/wcYFzrXPuCY" height="1"></a></div>