How to create interactive 3D plots using SAS9API and R

  • Date August 30, 2019
  • Written by Olga Pavlova
  • Category R

This example shows how to create interactive 3D plots for SAS datasets using SAS9API.

Prerequisites

Before you start using this guide you’ll need the following:

  • Access to SAS9API proxy,
  • R and RStudio installed.

Step 1 – Getting the libraries needed

We will create our violin plot using plotly package and we will use some nice colours from RColorBrewer .
Also we will need rsas9api package to send requests to SAS9API and to install it from GitHub we will need devtools package.
Packages devtools , plotly and RColorBrewer  are available on CRAN, so if you don’t have them already installed run the following code:

And to get rsas9api from GitHub run:

Now let’s load our packages:

Packages are loaded and we can go to the next step.

Step 2 – Defining connection properties for SAS9API proxy

To send requests to SAS9API endpoints you need to define:

  • URL for SAS9API proxy,
  • SAS workspace server name.

You can do that by replacing your_url and your_server in the following code and then running it.

After specifying connection properties we can send requests to SAS9API.

Step 3 – Getting SAS dataset data

We will be using retrieve_data function from rsas9api package. This function allows us to get data from a SAS dataset and to store it.
To send a request using retrieve_data function you will need to define:

  • library name of the dataset (“SASHELP” in our case),
  • dataset name (we will use “TOURISM” for our first plot),
  •  limit number: number of records to get from the dataset (we will use the maximum value of 10000),
  • offset number: number of records to skip from the beginning of dataset (we will leave it at 0),
  • asDataFrame flag (TRUE in our case, as we want our request to return a dataset).

Let’s run the code:

Now that we have the data we can start creating the plot.

Step 4a – Creating 3D line plot

To create a 3D plot in plotly we will use function plot_ly with type scatter3d .
And for this dataset we will use lines mode.
For our plot we will define x axis as ‘Year’, y axis as ‘The number of holidays in Spain taken by US residents’ and z axis as ‘An exchange rate index of Spanish pesetas against the US dollar’.

Let’s run the code and see our plot!

Now let’s try something different!

Step 4b – Creating 3D scatter plot

We will use SASHELP.THICK dataset for this plot, so let’s get the data:

For this time we will create 3D scatter plot, so we will use markers mode. And we will define size of our markers as 1. And for the colours we will use Greys palette from RColorBrewer .

And we will define our scales as ‘East’ for x, ‘North’ for y and ‘Coal Seam Thickness’ for z.

Step 4c – Creating 3D mesh plot

Now we will use SASHELP.QUAKES dataset to create a 3D mesh plot.
QUAKES dataset has 15’578 rows, so we can not get the whole dataset in one request as the maximum limit for this endpoint is 10’000 rows. Instead, we will send two requests: one with offset = 0  and one with offset = 10000 :

Now let’s bind the results into one dataframe:

Now we have the whole dataset.

To create 3D mesh plot we need to set type = "mesh3d" .
We will define our scales as ‘Latitude’ for x and ‘Longitude’ for y. For depth dimension, we want our points to be below zero, so we will define z axis as ‘Depth * -1’.
To apply colours along z axis we need to define intensity = ~Depth*-1  and colors = "PRGn" .

Conclusion

In this article, we showed how to create 3D plots for SAS data using SAS9API.

We used retrieve_data function from rsas9api package to get data from SAS dataset in the dataframe format. SAS9API proxy allows you to send different requests to SAS server, including getting and posting data. And with the help of rsas9api package you can send requests to all SAS9API endpoints using R language.

To see other examples for SAS9API and R go to Examples > R page.

Feel free to contact us if you have any questions or comments!

[ninja_form id=1]