Multiple charts in Custom R components can be plotted in non-HANA scenarios by using the multiplot feature.
You can now plot multiple charts in custom R components. The feature does not incur any changes in the procedure and the UI components for creating an R component. However, there are some rules that you must follow when writing a custom R function that uses the multiplot feature. The rules are listed below, followed by a custom R function as an example to illustrate the writing principles:
The following R code shows how to plot multiple charts in the custom R function. For the purpose of demonstrating the multiplot feature, no predictive algorithms are used in this R function, but you can add predictive algorithms as indicated in the inline comments.
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"))))
}You must return the charts to be plotted in the return statement as elements of the charts list. The return statement is a high-dimensional list that looks like this:
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”), …), …))
Each element of the charts list is also a list which contains information about a chart to be plotted, including chart variable (chart element), chart name (name element), and chart type (type element). Chart name and chart type provide information for users to distinguish charts from each other on the result window. Different icons are used for different chart types and the chart name displays as a tooltip when the cursor hovers over the icon. You do not have to set chart name and chart type to plot multiple charts, but it is highly recommended.
Expert Analytics currently supports the following chart types:
For other chart types that are not included in the list above, a default icon will be used for the chart.
The multiplot feature does not change the existing (single) plot and there is no need to change the existing custom R function if only (single) plot is needed.