Book Image

RStudio for R Statistical Computing Cookbook

By : Andrea Cirillo
Book Image

RStudio for R Statistical Computing Cookbook

By: Andrea Cirillo

Overview of this book

The requirement of handling complex datasets, performing unprecedented statistical analysis, and providing real-time visualizations to businesses has concerned statisticians and analysts across the globe. RStudio is a useful and powerful tool for statistical analysis that harnesses the power of R for computational statistics, visualization, and data science, in an integrated development environment. This book is a collection of recipes that will help you learn and understand RStudio features so that you can effectively perform statistical analysis and reporting, code editing, and R development. The first few chapters will teach you how to set up your own data analysis project in RStudio, acquire data from different data sources, and manipulate and clean data for analysis and visualization purposes. You'll get hands-on with various data visualization methods using ggplot2, and you will create interactive and multidimensional visualizations with D3.js. Additional recipes will help you optimize your code; implement various statistical models to manage large datasets; perform text analysis and predictive analysis; and master time series analysis, machine learning, forecasting; and so on. In the final few chapters, you'll learn how to create reports from your analytical application with the full range of static and dynamic reporting tools that are available in RStudio so that you can effectively communicate results and even transform them into interactive web applications.
Table of Contents (15 chapters)
RStudio for R Statistical Computing Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Drawing a route on a map with ggmap


Working with geospatial data is a simple task with R. Thankfully, the ggmap package provides a good number of facilities for this task.

In particular, this recipe gives you the ability to draw on a map a custom defined route from one point to another.

Getting ready

As you can imagine, we first need to install and load the ggmap package:

install.packages("ggmap")
library(ggmap)

How to do it...

  1. Define the route points using the route() function:

    trip      <- (route(from = "rome", to = "milan",structure = "route", output = "simple"))
  2. Create the map where you want to draw the route:

    route_map <- get_map("italy",zoom = 6) 
  3. Define the segment and segment_couple variables to link trip points:

    segment <- c()
    for(i in 1:nrow(trip)){
      if(i == 1){segment[i] <- 1}else{
        if( i %% 2 != 0 ){
          segment [i] <- i-segment[i-1]}else{
            segment [i] <- i/2
          }
      }
    }
    segment_couple <- c(0,segment[-length(segment)])
    trip$segment <- segment
    trip...