カスタム R における複数チャートのプロット

multiplot 機能を使用すると、HANA 以外のシナリオでカスタム R コンポーネントの複数チャートをプロットできます。

カスタム R コンポーネントで複数チャートをプロットできるようになりました。この機能の手順と UI コンポーネントは、R コンポーネント作成時の手順と UI コンポーネンと変わりありません。ただし、multiplot 機能を使用するカスタム R 関数を記述する際に従う必要のあるルールがいくつかあります。次に、このルールと、記述規則を説明する例としてカスタム R 関数を示します。

  • ggplot2 パッケージをカスタム R 関数にロードし、multiplotggplot2 プロット関数 (qplotggplot など) を使用します。
  • カスタム R 関数で、pa.config("multiplot","true") を使用して Expert Analyticsmultiplot モードに設定します。
  • 各チャートを変数に割り当て、すべてのチャート変数を return ステートメントのリストとして返します。

次の R コードに、カスタム R 関数で複数チャートをプロットする方法を示します。multiplot 機能の例示を目的としているので、この R 関数では予測アルゴリズムを使用していませんが、インラインコメントに示されているように予測アルゴリズムを追加することができます。

注記 このサンプル R 関数は mtcars データセットを使用するようにハードコーティングされています。このサンプル R 関数を実行するには、はじめに mtcars データセットをロードする必要があります。
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 では現在次のチャートタイプがサポートされています。

  • bar
  • horizontal bar
  • bubble
  • confusion matrix
  • gain
  • lift
  • line
  • model accuracy
  • parallel coordinates
  • scatter plot
  • time series

ここにリストされていない他のチャートタイプのチャートにはデフォルトのアイコンが使用されます。

multiplot 機能は既存の (単一の) プロットを変更しません。単一のプロットだけが必要な場合は、既存のカスタム R 関数を変更する必要はありません。