How to create violin plots using SAS9API and R

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

This example shows how to create a violin plot for a SAS dataset using SAS9API.


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 ggplot2 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 , ggplot2  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 (“CARS” in our case),
  • 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:

And let’s have a look at the top rows of the dataframe we received:

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

Step 4 – Creating violin plot

For our plot we will define x axis as Type of vehicle and y axis as City mileage. To create a violin plot in ggplot2  we will use geom_violin  geometry. We will set trim = FALSE  to have long and thin tails (but you can try trim = TRUE  as well). To fill our violins with nice colours we will use scale_fill_brewer  function and palette = "Pastel1" .
Let’s run the code and see our plot!


In this article, we showed how we can create a violin plot for SAS data using SAS9API.
We used retrieve_data  function from rsas9api  package to get data from a 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.

If you want to know how to create a similar violin plot using Python check this article.

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!