可以通过使用 multiplot 功能通过非 HANA 方案在自定义 R 中绘制多个统计图。
现在可以在自定义 R 部件中绘制多个统计图。该功能对创建 R 部件的过程和 UI 部件不做任何更改。但是,在编写使用 multiplot 功能的自定义 R 函数时必须遵守一些规则。这些规则列示如下,后面跟一个自定义 R 函数,作为示例说明编写的原则:
以下 R 代码显示如何在自定义 R 函数中绘制多个统计图。为了演示 multiplot 功能,不能在 R 函数中使用预测算法,但是可以在注释行里添加预测算法作为提示。
myanalysisfunction <- function(mydataframe){
# Load ggplot2 library library(ggplot2)
# Set PA to multiplot mode pa.config("multiplot","true")
#######################################
# Put predictive algorithms here
#######################################
# Plot a histogram chart using qplot and assign the chart to variable my_p1
my_p1 <- qplot(mydataframe$hp, geom="histogram")
# Plot a scatterplot chart and assign the chart to variable my_p2
my_p2 <- qplot(mydataframe$wt, mydataframe$hp)
# Plot a simple linear model and assign the chart to variable my_p3
my_model <- lm(mydataframe$wt ~ mydataframe$hp)
my_p3 <- qplot(hp, wt, data = mydataframe) + geom_abline(intercept=coef(my_model)[1],
slope=coef(my_model)[2])
# plot a scatterplot chart with a linear smooth line and assign the chart to variable my_p6
my_p5 <- ggplot(mydataframe, aes(x = wt, y=mpg), .~cyl)+ geom_point()
my_p6 <- my_p5 + geom_smooth(aes(group=cyl),method="lm")
# plot a pie chart and assign the chart to variable my_p7
my_p7 <- ggplot(data=mydataframe, aes(x=factor(1), fill= factor(cyl))) + geom_bar(width=1)+
coord_polar(theta="y")
# plot a time series chart and assign it to chart variable my_p8
year <- as.numeric(unlist(strsplit("1998 1999 2000 2001 2002 2003 2004 2005 2006 2007", "\\s+")))
revenue <- as.numeric(unlist(strsplit("10 6 13 14 12 8 10 10 6 9", "\\s+")))
mydataframe2 <- data.frame(year, revenue)
my_p8 <- ggplot(mydataframe2, aes(year, revenue)) + geom_bar(stat="identity", fill="white",
colour="black") + geom_line(colour="red") + stat_smooth(se=F, size=3)
# Return all chart variables and set metadata for each chart
return(list(out=mydataframe, charts=list(list(chart=my_p1, type="bar", name="chart 1"),
list(chart=my_p2, type="scatter plot", name="chart 2"), list(chart=my_p3, type="line", name="chart 3"),
list(chart=my_p8, type="time series", name="chart 8"), list(chart=my_p7, type="pie",
name="chart 7"), list(chart=my_p6, type="line", name="chart 6"))))
}必须将要在 return 语句中绘制的统计图作为统计图列表的元素返回。return 语句是一个高维列表,类似于:
return(list(…, charts=list(…, list(chart=chart_variable_1,name=”chart_name_1”, type=”chart_type_1”), list(chart=chart_variable_2,name=”chart_name_2”, type=”chart_type_2”), …), …))
统计图列表的每个元素也是一个列表,包含要绘制的统计图的信息,其中包括统计图变量(统计图元素)、统计图名称(名称元素)和统计图类型(类型元素)。统计图名称和统计图类型为用户提供信息以便在结果窗口区分不同的统计图。不同图标用来表示不同的统计图类型,当光标悬停在图标上时统计图名称会显示为工具提示。插入统计图时设置统计图名称和统计图类型不是必须的,但强烈推荐这么做。
Expert Analytics 当前支持以下统计图类型:
对未包含在上面列表中的其它统计图类型,会使用一个默认的图标。
multiplot 功能不会更改现有的(单一)图表,因此如果只需要(单一)图表,则无需更改现有的自定义 R 函数。