createCentroidPlot: Create plot of cluster centroids.

Description Usage Arguments Value Examples

View source: R/plottingKmeans.R

Description

Visualize centroids produced by clustering function like k-means. Plots available are line plot, bar plot, or heatmap. Parameter format specifies which one to create.

Usage

1
2
3
4
5
6
7
8
createCentroidPlot(km, format = "line", groupByCluster = TRUE,
  clusters = rownames(km$centers), dims = colnames(km$centers),
  baseSize = 12, baseFamily = "serif", title = paste("Cluster Centroids",
  format, "Plot"), xlab, ylab = ifelse(format == "heatmap", "cluster",
  ifelse(!is.null(km$scale) && km$scale, "scaled value", "value")),
  legendPosition = ifelse(format == "bar", "none", "right"),
  coordFlip = FALSE, ticks = FALSE, defaultTheme = theme_tufte(base_size =
  baseSize, base_family = baseFamily, ticks = ticks), themeExtra = NULL)

Arguments

km

an object of class "toakmeans" returned by computeKmeans or of class "kmeans" returned by kmeans.

format

type of plot to use: "line", "bar", "bar_dodge" or "heatmap".

groupByCluster

logical: indicates if centroids are grouped by clusters or variables. groupByCluster has no effect when format="heatmap".

clusters

optional vector with clusters to include. If vector has named values then names are used for cluster labels. By default, all clusters are included.

dims

optional vector with dimensions to include. Vector may be contain either dimension indices or names. By default, all dimensions are included.

baseSize

theme base font size.

baseFamily

theme base font family.

title

plot title.

xlab

a label for the x axis, defaults to a description of x.

ylab

a label for the y axis, defaults to a description of y.

legendPosition

the position of legends. ("left", "right", "bottom", "top", or two-element numeric vector). "none" is no legend.

coordFlip

logical flipped cartesian coordinates so that horizontal becomes vertical, and vertical horizontal (see coord_flip).

ticks

logical Show axis ticks using default theme settings (see defaultTheme)?

defaultTheme

plot theme settings with default value theme_tufte. More themes are available here: ggtheme (by ggplot2) and ggthemes.

themeExtra

any additional theme settings that override default theme.

Value

ggplot object

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
if(interactive()){
# initialize connection to Lahman baseball database in Aster 
conn = odbcDriverConnect(connection="driver={Aster ODBC Driver};
                         server=<dbhost>;port=2406;database=<dbname>;uid=<user>;pwd=<pw>")
                         
km = computeKmeans(conn, "batting", centers=5, iterMax = 25,
                   aggregates = c("COUNT(*) cnt", "AVG(g) avg_g", "AVG(r) avg_r", "AVG(h) avg_h"),
                   id="playerid || '-' || stint || '-' || teamid || '-' || yearid", 
                   include=c('g','r','h'), scaledTableName='kmeans_test_scaled', 
                   centroidTableName='kmeans_test_centroids',
                   where="yearid > 2000")
createCentroidPlot(km)
# As clusters fluctuate from run to run cluster names may be inconsistent
createCentroidPlot(km, format="bar_dodge", 
                   clusters=c(Stars=3, Reserve=4), dims=c('g','h'))
createCentroidPlot(km, format="heatmap", coordFlip=TRUE)
}

Example output

Loading required package: RODBC

toaster documentation built on May 30, 2017, 3:51 a.m.