Exploring R Code in SAP Analytics Cloud

Objective

After completing this lesson, you will be able to work with R Code.

Examples of R Visualizations and the R Code

By choosing insert and R visualization, you can create a wide variety of charts using a script from R.

In this lesson, we will show you nine examples of R visualizations created in SAP Analytics Cloud and the associated R code from the script editor.

The code can be copied and used in the SAP Hands-on Practice System to experiment with the various R visualizations used in this unit.

Line Graph

In this example, you can see a line graph has been created to show sales revenue over time by region.

An example of a line graph in an SAP Analytics Cloud story. The R code used to create it follows the example.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
1234567891011
library(ggplot2) BestRunCorpRetail$`Sales Revenue` <- BestRunCorpRetail$`Sales Revenue` / 1000 # Line Graph for Sales Over Time ggplot(BestRunCorpRetail, aes(x = Date, y = `Sales Revenue` , group=Region, color=Region )) + geom_line() + labs(title = "Sales Revenue (in k) by Region Over Time", x = "Date", y = "Sales Revenue") + theme(legend.position="bottom") theme_minimal()

Stacked Horizontal Bar Chart

In this example, you can see a stacked horizontal bar chart has been created to show quantity sold by region and lines.

An example of a stacked horizontal bar chart in an SAP Analytics Cloud story. The R code used to create it follows the example.

Some R visualizations can be created as interactive charts. This example is a chart that users can interact with to isolate and focus on specific data, as you can see in the following image.

Some charts are interactive. On the left, you can see African data is isolated. On the right, European data is excluded.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
1234567
library(ggplot2) library(plotly) # Bar Chart for Sales by Product Category p1 <- ggplot(BestRunCorpRetail, aes(x = Lines, y = `Quantity sold`/1000 , fill = Region )) + geom_bar(stat = "identity") + coord_flip() ggplotly(p1, tooltip = c("x", "y", "fill"))

Scatter Bubble Chart

In this example, you can see an interactive scatter bubble chart has been created to show a comparison of sales revenue by quantity sold for each sales manager.

An example of an interactive scatter bubble chart in an SAP Analytics Cloud story. The R code used to create it follows the example.

This is another example of an interactive chart. Users can play the interaction, as well as hover over elements of it to view additional information, as you can see in the following image.

Example of an interactive chart. The image on the top left has the Play button highlighted. The bottom right image has a tooltip that provides additional information.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
12345678910111213141516
# import packages ggplot2 and plotly library(ggplot2) library(plotly, warn.conflicts = FALSE) # create a ggplot frame through function ggplot() and add data points, labels and scale settings BestRunCorpRetail$`Sales Revenue` <- BestRunCorpRetail$`Sales Revenue` / 1000 BestRunCorpRetail$`Quantity sold` <- BestRunCorpRetail$`Quantity sold` / 1000 gg <- ggplot(BestRunCorpRetail, aes(`Quantity sold`, `Sales Revenue`, color = Manager)) + geom_point(aes(size = Discount , frame = Year, ids = Manager)) + labs(title = "Comparison Sales Revenue (in k) and Quantity Sold (in k) versus Discount" , y = "Sales Revenue", x = "Quantity Sold") + scale_x_log10() # convert the ggplot frame to a plotly frame through function ggplotly() ggplotly(gg)

Density Chart

In this example, you can see a density chart has been created to show the density of the product lines in each region.

An example of a density chart in an SAP Analytics Cloud story. The R code used to create it follows the example.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
123456
# import packages ggplot2 and plotly library(ggplot2) library(plotly, warn.conflicts = FALSE) ggplot(BestRunCorpRetail, aes( Region , Lines )) + labs(title = "Density of the Product Lines in the Different Region" ) + geom_jitter(aes(color = Lines ), size = 1) + theme(legend.position="none")

Word Cloud

In this example, you can see a word cloud has been created to show the relationship between the cities and the associated gross margin.

An example of a Word Cloud in an SAP Analytics Cloud story. In shades of blue, the names of the cities are sized in relation to the gross margin. The R code used to create it follows the example.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
12345678
# load package library(wordcloud) # get words words <- BestRunCorpRetail$`City` # get frequency frequency <- BestRunCorpRetail$`Gross Margin` # generate word cloud wordcloud(words, frequency, scale = c(4, 1.5), rot.per=0.2, colors=brewer.pal(8, "Blues"))

Sankey Network Diagram

In this example, you can see a Sankey network diagram has been created to show complaints by region, country, and city.

An example of a Sankey network diagram in an SAP Analytics Cloud story. The R code used to create it follows the example.

This is example of a chart that users can hover over elements to view the related information, as you can see in the following image.

Example of a Sankey Network Diagram chart. Mouse is hovered over France, showing 31,815 complaints for all of France. All of France's data is now showing as dark gray.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
12345678910111213141516171819202122232425262728293031323334353637
# import package dplyr and networkD3 suppressPackageStartupMessages(library(dplyr)) library(networkD3) # de-factorize three level columns in SankeyData2 to strings BestRunCorpRetail$Region <- as.character(BestRunCorpRetail$Region) BestRunCorpRetail$Country <- as.character(BestRunCorpRetail$Country) BestRunCorpRetail$City <- as.character(BestRunCorpRetail$City) # create the first- and second-level decomposition dataset as list of (source, target, amount) level_1 <- aggregate(BestRunCorpRetail[,4], by=list(BestRunCorpRetail$Region, BestRunCorpRetail$Country), FUN=sum) level_2 <- aggregate(BestRunCorpRetail[,4], by=list(BestRunCorpRetail$Country, BestRunCorpRetail$City), FUN=sum) # rename the headers in the two level data frames names(level_1) <- c("Source","Target","Complains") names(level_2) <- c("Source","Target","Complains") # stack the two level data frames together sankeyData <- suppressWarnings(bind_rows(level_1, level_2)) # create a list of nodes in all levels and index them from 0 sankeyNodes <- data.frame(Name = unique(c(sankeyData$Source, sankeyData$Target))) sankeyNodes$index <- 0:(nrow(sankeyNodes)-1) # create a sankeyLinks data frame by masking node names in sankeyData with their indexes in sankeyNodes sankeyLinks <- merge(sankeyData, sankeyNodes, by.x="Source", by.y="Name") sankeyLinks <- merge(sankeyLinks, sankeyNodes, by.x="Target", by.y="Name") # select the masked columns and rename the headers sankeyLinks <- sankeyLinks[, c(4,5,3)] names(sankeyLinks) <- c("Source","Target","Complains") # call the sankeyNetwork() function to generate the sankey diagram sankeyNetwork(Links = sankeyLinks, Nodes = sankeyNodes, Source = "Source", Target = "Target", Value = "Complains", NodeID = "Name", fontSize = 10, nodeWidth = 5, nodePadding = 5 , unit="Complains")

Polar Bar Chart

In this example, you can see a polar bar chart has been created to show gross margin and sales revenue by region.

An example of a polar bar chart in an SAP Analytics Cloud story. The R code used to create it follows the example.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
123456789101112131415161718192021222324252627
library(ggplot2) library(reshape2) SalesRevenue <- as.numeric(BestRunCorpRetail$`Sales Revenue`) / 1000 total_GrossMargin <- sum(BestRunCorpRetail$`Gross Margin`) Region <- as.character(BestRunCorpRetail$Region) percentage <- BestRunCorpRetail$`Gross Margin` * 100/ total_GrossMargin percentage_char <- as.character(formatC(percentage, digits = 2, format = "f")) width <- percentage * 3.6 GrossMargin <- c(width[1]/2) label <- paste(percentage_char, seq = "%", sep = "") for (i in 2:length(width)) { GrossMargin <- c(GrossMargin, sum(width[1:i-1]) + width[i]/2) } angle <- GrossMargin for (i in 1:length(angle)) { if (angle[i] >= 180) { angle[i] <- angle[i] - 180 } } rose <- data.frame(SalesRevenue, Region, GrossMargin) p <- ggplot(rose, aes(x=GrossMargin, y=SalesRevenue, fill=Region))+ geom_bar(width=width, stat="identity")+ geom_text(aes(x=GrossMargin, y=SalesRevenue+40, label=label, angle=angle-90), size=5)+ labs(x="Gross Margin %", y="Sales Revenue ") p + coord_polar(theta="x", start=0, direction=-1)

Lag Plot

In this example, you can see a lag plot has been created to show the number of issues reported in each quarter.

An example of a lag plot in an SAP Analytics Cloud story. The R code used to create it follows the example.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
1234567891011121314151617181920212223242526272829303132333435363738394041
library(lubridate) library(dplyr) library(ggplot2) library(forecast) ## Prepare data BestRunCorpRetail$`Number of Issues reported` <- BestRunCorpRetail$`Number of Issues reported` / 1000 df <- BestRunCorpRetail %>% mutate(Year = as.integer(as.character(Year))) %>% mutate(quarter_num = as.numeric(substr(Quarter, 2, 2))) %>% mutate(first_month_of_quarter = (quarter_num * 3) -2) %>% mutate(first_day_of_quarter = make_date( Year, first_month_of_quarter, 1)) %>% select(first_day_of_quarter, `Number of Issues reported`) %>% arrange(first_day_of_quarter) ## Convert to ts object first_period = min(df$first_day_of_quarter) last_period = max(df$first_day_of_quarter) tso <- ts(df$`Number of Issues reported`, start = c(year(first_period), quarter(first_period)), end = c(year(last_period), quarter(last_period)), frequency = 4) ## Plot ts <- window(tso,start=2021) p <- gglagplot(ts, do.lines = TRUE, seasonal=TRUE, colour=TRUE, diag=FALSE , lags = 9) + labs(title = "Lag plot - Quarterly Number of Issues reported (in k)") ## Layout p + theme( plot.title = element_text(color="red", size=15, face="bold", hjust=0.5 ,margin = margin(t = 0, r = 0, b = 16, l = 0)) , panel.border = element_rect(colour = "blue", fill=NA) , axis.ticks = element_line( size = 1, linetype = 'solid') , axis.ticks.length = unit(8, "pt"), axis.text.x = element_text(color="black", size = 8, face = "bold" , margin = margin(t = 6, r = 0, b = 0, l = 0)) , axis.title.x = element_text(color="blue", size = 12, face = "bold", margin = margin(t = 10, r = 0, b = 0, l = 0)) , axis.text.y = element_text(color="red", size = 8, face = "bold" , margin = margin(t = 0, r = 6, b = 0, l = 0)) , axis.title.y = element_text(color="yellow", size =12, face = "bold", margin = margin(t = 0, r = 10, b = 0, l = 0)) , axis.line = element_line(colour = "black") , strip.text = element_text(color="blue", size = 12, face = "bold") )

Pareto Chart

In this example, you can see a pareto chart has been created to show sales revenue by region.

An example of a pareto chart in an SAP Analytics Cloud story. The R code used to create it follows the example.

The example above was created using the following R code in the R visualization script editor:

Code Snippet
123456789
library(ggplot2) library(qcc) Sales = BestRunCorpRetail$`Sales Revenue` / 1000 names(Sales) = BestRunCorpRetail$`Country` pareto.chart(Sales, ylab = "Sales",main='Pareto Chart For Sales Revenue',col=heat.colors(length(Sales)))

Log in to track your progress & complete quizzes