如何使用R語言在SAP Analytics Cloud裏繪製各類統計圖表

插入一個R visualization:編輯器

必定要確保圖形出現這個model的小圖標,表明這個R visualization的模型數據成功綁定以後才能進行下一步操做:ide

模型綁定成功後,在R script編輯器Environment標籤頁的Data下拉菜單裏能看到模型數據。ui

使用這個SAP Analytics Cloud官方教程裏提供的excel文件做爲數據源:url

https://www.sapanalytics.clou...spa

該excel內容以下:excel

excel系統導入SAP Analytics Cloud後,須要使用simple transformation,將;分號分隔的值拆分紅三列:code


逐一拆分:orm

拆分完畢以後,生成Model. 將這個url裏包含的R腳本複製粘貼到R編輯器裏:
https://www.sapanalytics.clou...blog

# Discription:
# Creating a histogram of the log returns, adding the kernel density of the log returns
# and the normal density as reference distribution 
#
# Requirements: 
# ggplot requires a data frame
# 
# Output:
# Histogram Plot
# 

library(ggplot2)

Simulated_data <- data.frame(Simulated_data)

histgg <- ggplot(data = Simulated_data, aes(logreturns))

histgg + geom_histogram(aes(y = ..density..),fill = "lightblue",color = "black", alpha = 0.8, position = "identity") +
  geom_density(aes(color = "Kernel Density"), size = 1) +
  stat_function(aes(color = "Normal Distribution"), fun = dnorm, args = list(mean = mean(Simulated_data$logreturns), sd = sd(Simulated_data$logreturns)), size = 1) +
  ggtitle("Histogram") +
  theme(panel.grid = element_line(linetype = "dashed", color = "lightgrey"), panel.background = element_rect(fill = "white"),
        panel.border = element_rect(colour = "black", fill=NA),
        plot.title = element_text(hjust = 0.5)) +
  scale_colour_manual("Density", values = c("red", "darkgreen")) +
  xlab(" ")+
  ylab("Frequency")

點擊Execute按鈕,就能夠看到R腳本繪製出來的圖形了:教程

要獲取更多Jerry的原創文章,請關注公衆號"汪子熙":