# ClojureDocs(beta)

• incanter

1.2.3-SNAPSHOT

## incanter.charts

• (add-polygon chart coords & options)
Adds a polygon outline defined by a given coordinates. The last coordinate will
close with the first. If only two points are given, it will plot a line.

Arguments:
chart -- the chart to add the polygon to.
coords -- a list of coords (an n-by-2 matrix can also be used)

Examples:
(use '(incanter core stats charts))
(def x (range -3 3 0.01))
(def plot (xy-plot x (pdf-normal x)))
(view plot)

;; add polygon to the chart
(add-polygon plot [[-1.96 0] [1.96 0] [1.96 0.4] [-1.96 0.4]])
;; the coordinates can also be passed in a matrix
;; (def points (matrix [[-1.96 0] [1.96 0] [1.96 0.4] [-1.96 0.4]]))
(add-text plot -1.25 0.35 "95% Conf Interval")

;; PCA chart example
(use '(incanter core stats charts datasets))
(def iris (to-matrix (get-dataset :iris)))
;; run the pca
(def pca (principal-components (sel iris :cols (range 4))))
;; extract the first two principal components
(def pc1 (sel (:rotation pca) :cols 0))
(def pc2 (sel (:rotation pca) :cols 1))

;; project the first four dimension of the iris data onto the first
;; two principal components
(def x1 (mmult (sel iris :cols (range 4)) pc1))
(def x2 (mmult (sel iris :cols (range 4)) pc2))

;; now plot the transformed data, coloring each species a different color
(def plot (scatter-plot x1 x2
:group-by (sel iris :cols 4)
:x-label "PC1" :y-label "PC2" :title "Iris PCA"))

(view plot)
;; put box around the first group
(add-polygon plot [[-3.2 -6.3] [-2 -6.3] [-2 -3.78] [-3.2 -3.78]])

### Source incanter/charts.clj:2499 top

```(defn add-polygon
" Adds a polygon outline defined by a given coordinates. The last coordinate will
close with the first. If only two points are given, it will plot a line.

Arguments:
chart -- the chart to add the polygon to.
coords -- a list of coords (an n-by-2 matrix can also be used)

Examples:
(use '(incanter core stats charts))
(def x (range -3 3 0.01))
(def plot (xy-plot x (pdf-normal x)))
(view plot)

;; add polygon to the chart
(add-polygon plot [[-1.96 0] [1.96 0] [1.96 0.4] [-1.96 0.4]])
;; the coordinates can also be passed in a matrix
;; (def points (matrix [[-1.96 0] [1.96 0] [1.96 0.4] [-1.96 0.4]]))
(add-text plot -1.25 0.35 \"95% Conf Interval\")

;; PCA chart example
(use '(incanter core stats charts datasets))
(def iris (to-matrix (get-dataset :iris)))
;; run the pca
(def pca (principal-components (sel iris :cols (range 4))))
;; extract the first two principal components
(def pc1 (sel (:rotation pca) :cols 0))
(def pc2 (sel (:rotation pca) :cols 1))

;; project the first four dimension of the iris data onto the first
;; two principal components
(def x1 (mmult (sel iris :cols (range 4)) pc1))
(def x2 (mmult (sel iris :cols (range 4)) pc2))

;; now plot the transformed data, coloring each species a different color
(def plot (scatter-plot x1 x2
:group-by (sel iris :cols 4)
:x-label \"PC1\" :y-label \"PC2\" :title \"Iris PCA\"))

(view plot)
;; put box around the first group
(add-polygon plot [[-3.2 -6.3] [-2 -6.3] [-2 -3.78] [-3.2 -3.78]])

"
([chart coords & options]
(let [opts (when options (apply assoc {} options))
points (double-array (mapcat identity coords))
anno (XYPolygonAnnotation. points)]