forked from incanter/incanter
/
charts.clj
3451 lines (2737 loc) · 120 KB
/
charts.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; charts.clj -- Charts library for Clojure built on JFreeChart
;; by David Edgar Liebke http://incanter.org
;; March 11, 2009
;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.htincanter.at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
;; CHANGE LOG
;; March 11, 2009: First version
(ns ^{:doc "This is the core charting library for Incanter.
It provides basic scatter plots, histograms, box plots
xy plots, bar charts, line charts, as well as
specialized charts like trace plots and Bland-Altman
plots.
This library is built on the JFreeChart library
(http://www.jfree.org/jfreechart/).
"
:author "David Edgar Liebke"}
incanter.charts
(:use [incanter.core :only ($ matrix? to-list plus minus div group-on
bind-columns view save $group-by conj-cols
grid-apply set-data col-names)]
[incanter.stats :only (quantile quantile-normal cumulative-mean sd)]
[clj-time.coerce :only (to-date)])
(:import (java.io File)
(javax.imageio ImageIO)
(javax.swing JSlider JFrame JLabel JPanel)
(java.awt BorderLayout)
(org.jfree.data.statistics HistogramDataset
HistogramType
DefaultBoxAndWhiskerCategoryDataset)
(org.jfree.chart ChartFactory
ChartUtilities
ChartFrame
ChartTheme
StandardChartTheme)
(org.jfree.chart.plot PlotOrientation
DatasetRenderingOrder
SeriesRenderingOrder)
(org.jfree.data.xy DefaultHighLowDataset
XYSeries
XYSeriesCollection)
(org.jfree.data.category DefaultCategoryDataset)
(org.jfree.data.general DefaultPieDataset)
(org.jfree.chart.renderer.xy XYLineAndShapeRenderer)
(org.jfree.ui TextAnchor)
(org.jfree.chart.annotations XYPointerAnnotation
XYTextAnnotation
XYPolygonAnnotation)))
(defmulti set-background-default
"
Examples:
(use '(incanter core stats charts datasets))
(doto (histogram (sample-normal 1000) :title (str :Test-Tittle))
set-theme-bw
view)
(doto (histogram (sample-normal 1000))
set-background-default
(add-histogram (sample-normal 1000 :mean 1))
view)
(doto (scatter-plot :speed :dist :data (get-dataset :cars))
set-theme-bw
view)
(doto (scatter-plot :speed :dist :data (get-dataset :cars))
set-theme-bw
(set-stroke :dash 5)
(add-points (plus ($ :speed (get-dataset :cars)) 5) (plus ($ :dist (get-dataset :cars)) 10))
view)
(doto (scatter-plot :speed :dist :data (get-dataset :cars))
set-background-default
(set-stroke :dash 5)
(add-function sin 0 25)
view)
(doto (xy-plot :speed :dist :data (get-dataset :cars) :legend true)
set-background-default
view)
(doto (scatter-plot :speed :dist :data (get-dataset :cars))
set-background-default
view)
(doto (box-plot (sample-gamma 1000 :shape 1 :rate 2)
:legend true)
view set-background-default
(add-box-plot (sample-gamma 1000 :shape 2 :rate 2))
(add-box-plot (sample-gamma 1000 :shape 3 :rate 2)))
(doto (bar-chart [:a :b :c] [10 20 30] :legend true)
view
set-background-default
(add-categories [:a :b :c] [5 25 40]))
(doto (line-chart [:a :b :c] [10 20 30] :legend true)
view
set-background-default
(add-categories [:a :b :c] [5 25 40]))
;; time-series-plot
(def epoch 0)
(defn num-years-to-milliseconds [x]
(* 365 24 60 60 1000 x))
(def dates (map num-years-to-milliseconds (range 100)))
(def chart1 (time-series-plot dates (range 100)))
(def cw1 (view chart1))
(add-lines chart1 dates (mult 1/2 (range 100)))
(def chart2 (time-series-plot (take 10 dates) (mult 1/2 (range 10))))
(def cw2 (view chart2))
"
(fn [chart] (type (.getPlot chart))))
(defmulti set-theme-default
(fn [chart & options] (type (-> chart .getPlot .getDataset))))
(defmulti set-theme-bw
"
Examples:
(use '(incanter core stats charts datasets))
(doto (histogram (sample-normal 1000))
set-theme-bw
view)
(doto (histogram (sample-normal 1000))
set-theme-bw
(add-histogram (sample-normal 1000 :mean 1))
view)
(doto (scatter-plot :speed :dist :data (get-dataset :cars))
set-theme-bw
view)
(doto (scatter-plot :speed :dist :data (get-dataset :cars))
set-theme-bw
(set-stroke :dash 5)
(add-points (plus ($ :speed (get-dataset :cars)) 5) (plus ($ :dist (get-dataset :cars)) 10))
view)
(doto (scatter-plot :speed :dist :data (get-dataset :cars))
set-theme-bw
(set-stroke :dash 5)
(add-function sin 0 25)
view)
(doto (xy-plot :speed :dist :data (get-dataset :cars))
set-theme-bw
view)
(doto (scatter-plot :speed :dist :data (get-dataset :cars))
set-theme-bw
(add-lines :speed :dist :data (get-dataset :cars))
view)
(doto (box-plot (sample-gamma 1000 :shape 1 :rate 2)
:legend true)
view
(add-box-plot (sample-gamma 1000 :shape 2 :rate 2))
(add-box-plot (sample-gamma 1000 :shape 3 :rate 2))
set-theme-bw)
(doto (bar-chart [:a :b :c] [10 20 30] :legend true)
view
set-theme-bw
(add-categories [:a :b :c] [5 25 40]))
(doto (line-chart [:a :b :c] [10 20 30] :legend true)
view
set-theme-bw
(add-categories [:a :b :c] [5 25 40]))
"
(fn [chart & options] (type (-> chart .getPlot .getDataset))))
(defn set-theme
" Changes the chart theme.
Arguments:
chart -- an Incanter/JFreeChart object
theme -- either a keyword indicating one of the built-in themes, or a JFreeChart ChartTheme object.
Built-in Themes:
:default
:dark
Examples:
(use '(incanter core charts))
(def chart (function-plot sin -4 4))
(view chart)
;; change the theme of chart to :dark
(set-theme chart :dark)
;; change it back to the default
(set-theme chart :default)
;; Example using JFreeTheme
(use '(incanter core stats charts datasets))
(import '(org.jfree.chart StandardChartTheme)
'(org.jfree.chart.plot DefaultDrawingSupplier)
'(java.awt Color))
(def all-red-theme
(doto
(StandardChartTheme/createJFreeTheme)
(.setDrawingSupplier
(proxy [DefaultDrawingSupplier] []
(getNextPaint [] Color/red)))))
(def data (get-dataset :airline-passengers))
(def chart (bar-chart :month :passengers :group-by :year :legend true :data data))
(doto chart
;; has no effect
(set-theme all-red-theme)
view)
References:
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/StandardChartTheme.html
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartTheme.html
"
([chart theme]
(let [built-in-theme? (some #{theme} #{:dark :legacy :gradient})
_theme (if built-in-theme?
(cond
(= theme :dark)
(StandardChartTheme/createDarknessTheme)
(= theme :legacy)
(StandardChartTheme/createLegacyTheme)
:default
(StandardChartTheme/createJFreeTheme))
(cond
(= theme :bw)
set-theme-bw
(instance? ChartTheme theme)
#(.apply theme %)
:default
set-theme-default))
;; bar-painter
;; (org.jfree.chart.renderer.xy.StandardXYBarPainter.)
]
(do
(if built-in-theme?
(do
(.setShadowVisible _theme false)
(.apply _theme chart))
(do
;; (doto (-> chart .getPlot .getRenderer)
;; (.setBarPainter bar-painter)
;; (.setSeriesOutlinePaint 0 java.awt.Color/lightGray)
;; (.setShadowVisible false)
;; (.setDrawBarOutline true))
(_theme chart)))
chart))))
(defn add-histogram*
([chart x & options]
(let [opts (when options (apply assoc {} options))
data (:data opts)
_x (if (coll? x) (to-list x) ($ x data))
data-plot (.getPlot chart)
n (.getDatasetCount data-plot)
nbins (or (:nbins opts) 10)
series-lab (or (:series-label opts) (str 'x))]
(do
(.addSeries (.getDataset data-plot) series-lab (double-array _x) nbins)
(.setSeriesOutlinePaint (-> chart .getPlot .getRenderer) n java.awt.Color/lightGray)
(.setSeriesRenderingOrder data-plot org.jfree.chart.plot.SeriesRenderingOrder/FORWARD)
(.fireChartChanged chart)
chart))))
(defmacro add-histogram
"
Adds a histogram to an existing histogram plot, returns the modified
chart object.
Options:
:nbins (default 10) number of bins for histogram
:series-label (default x expression)
Examples:
(use '(incanter core charts stats datasets))
(doto (histogram (sample-normal 1000)
:legend true)
view
(add-histogram (sample-normal 1000 :sd 0.5)))
(with-data (get-dataset :iris)
(doto (histogram :Sepal.Length :legend true)
(add-histogram :Petal.Length)
view))
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart x & options]
`(let [opts# ~(when options (apply assoc {} options))
series-lab# (or (:series-label opts#) (str '~x))
args# (concat [~chart ~x]
(apply concat (seq (apply assoc opts#
[:series-label series-lab#]))))]
(apply add-histogram* args#))))
(defn add-box-plot*
([chart x & options]
(let [opts (when options (apply assoc {} options))
data (:data opts)
_x (if (coll? x) (to-list x) ($ x data))
data-plot (.getCategoryPlot chart)
n-col (.getColumnCount (.getDataset data-plot))
n-row (.getRowCount (.getDataset data-plot))
series-label (or (:series-label opts) (str 'x))
category-label (or (:category-label opts)
(str n-col))]
(do
(.add (.getDataset data-plot) _x series-label category-label)
chart))))
(defmacro add-box-plot
"
Adds an additional box to an existing box-plot, returns the modified chart object.
Options:
:series-label (default x expression)
Examples:
(use '(incanter core charts stats))
(doto (box-plot (sample-normal 1000) :legend true)
view
(add-box-plot (sample-normal 1000 :sd 2))
(add-box-plot (sample-gamma 1000)))
(with-data (get-dataset :iris)
(doto (box-plot :Sepal.Length :legend true)
(add-box-plot :Petal.Length)
(add-box-plot :Sepal.Width)
(add-box-plot :Petal.Width)
view))
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart x & options]
`(let [opts# ~(when options (apply assoc {} options))
series-lab# (or (:series-label opts#) (str '~x))
args# (concat [~chart ~x] (apply concat (seq (apply assoc opts#
[:series-label series-lab#]))))]
(apply add-box-plot* args#))))
(defn add-categories*
([chart categories values & options]
(let [opts (when options (apply assoc {} options))
data (:data opts)
_values (if (coll? values) (to-list values) ($ values data))
_categories (if (coll? categories) (to-list categories) ($ categories data))
_group-by (when (:group-by opts)
(if (coll? (:group-by opts))
(to-list (:group-by opts))
($ (:group-by opts) data)))
_chart chart
series-label (:series-label opts)
data-plot (.getCategoryPlot _chart)
n-col (.getColumnCount (.getDataset data-plot))
n-row (.getRowCount (.getDataset data-plot))]
(do
(doseq [i (range 0 (count _values))] (.addValue (.getDataset data-plot)
(nth _values i)
(cond
_group-by
(nth _group-by i)
series-label
series-label
:else
(str 'values))
(nth _categories i)))
chart))))
(defmacro add-categories
"
Adds an additional categories to an existing bar-chart or line-chart, returns the modified chart object.
Options:
:group-by
:series-label
Examples:
(use '(incanter core charts stats datasets))
(def seasons (mapcat identity (repeat 3 [\"winter\" \"spring\" \"summer\" \"fall\"])))
(def years (mapcat identity (repeat 4 [2007 2008 2009])))
(def x (sample-uniform 12 :integers true :max 100))
(def plot (bar-chart years x :group-by seasons :legend true))
(view plot)
(add-categories plot years [10 20 40] :series-label \"winter-break\")
(add-categories plot
(plus 3 years)
(sample-uniform 12 :integers true :max 100)
:group-by seasons)
(def plot2 (line-chart years x :group-by seasons :legend true))
(view plot2)
(add-categories plot2 (plus 3 years) (sample-uniform 12 :integers true :max 100) :group-by seasons)
(with-data (get-dataset :iris)
(doto (line-chart :Species :Sepal.Length
:data ($rollup mean :Sepal.Length :Species)
:legend true)
(add-categories :Species :Sepal.Width :data ($rollup mean :Sepal.Width :Species))
(add-categories :Species :Petal.Length :data ($rollup mean :Petal.Length :Species))
(add-categories :Species :Petal.Width :data ($rollup mean :Petal.Width :Species))
view))
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart categories values & options]
`(let [opts# ~(if options (apply assoc {} options) {})
group-by# (:group-by opts#)
series-lab# (or (:series-label opts#)
(if group-by#
(format "%s, %s (0)" '~categories '~values)
(format "%s, %s" '~categories '~values)))
args# (concat [~chart ~categories ~values]
(apply concat (seq (apply assoc opts# [:series-label series-lab#]))))]
(apply add-categories* args#))))
(defmulti add-lines* (fn [chart x y & options] (type (-> chart .getPlot .getDataset))))
(defmethod add-lines* org.jfree.data.xy.XYSeriesCollection
([chart x y & options]
(let [opts (when options (apply assoc {} options))
data (:data opts)
_x (if (coll? x) (to-list x) ($ x data))
_y (if (coll? y) (to-list y) ($ y data))
data-plot (.getPlot chart)
n (.getDatasetCount data-plot)
series-lab (or (:series-label opts) (format "%s, %s" 'x 'y))
data-series (XYSeries. series-lab)
points? (true? (:points opts))
line-renderer (XYLineAndShapeRenderer. true points?)
;; data-set (.getDataset data-plot)
data-set (XYSeriesCollection.)]
(dorun
(map (fn [x y]
(if (and (not (nil? x))
(not (nil? y)))
(.add data-series (double x) (double y))))
_x _y))
(.addSeries data-set data-series)
(doto data-plot
(.setSeriesRenderingOrder org.jfree.chart.plot.SeriesRenderingOrder/FORWARD)
(.setDatasetRenderingOrder org.jfree.chart.plot.DatasetRenderingOrder/FORWARD)
(.setDataset n data-set)
(.setRenderer n line-renderer))
chart)))
;; doesn't work
(defmethod add-lines* org.jfree.data.statistics.HistogramDataset
([chart x y & options]
(let [opts (when options (apply assoc {} options))
data (:data opts)
_x (if (coll? x) (to-list x) ($ x data))
_y (if (coll? y) (to-list y) ($ y data))
data-plot (.getPlot chart)
n (.getDatasetCount data-plot)
series-lab (or (:series-label opts) (format "%s, %s" 'x 'y))
data-series (XYSeries. series-lab)
points? (true? (:points opts))
line-renderer (XYLineAndShapeRenderer. true points?)
data-set (XYSeriesCollection.)]
(dorun
(map (fn [x y]
(if (and (not (nil? x))
(not (nil? y)))
(.add data-series (double x) (double y))))
_x _y))
(.addSeries data-set data-series)
(doto data-plot
(.setSeriesRenderingOrder org.jfree.chart.plot.SeriesRenderingOrder/FORWARD)
(.setDatasetRenderingOrder org.jfree.chart.plot.DatasetRenderingOrder/FORWARD)
(.setDataset n data-set)
(.setRenderer n line-renderer))
chart)))
(defmacro add-lines
" Plots lines on the given scatter or line plot of the (x,y) points.
Equivalent to R's lines function, returns the modified chart object.
Options:
:series-label (default x expression)
:points (default false)
Examples:
(use '(incanter core stats io datasets charts))
(def cars (to-matrix (get-dataset :cars)))
(def y (sel cars :cols 0))
(def x (sel cars :cols 1))
(def plot1 (scatter-plot x y :legend true))
(view plot1)
;; add regression line to scatter plot
(def lm1 (linear-model y x))
(add-lines plot1 x (:fitted lm1))
;; model the data without an intercept
(def lm2 (linear-model y x :intercept false))
(add-lines plot1 x (:fitted lm2))
;; Clojure's doto macro can be used to build a chart
(doto (histogram (sample-normal 1000) :density true)
(add-lines (range -3 3 0.05) (pdf-normal (range -3 3 0.05)))
view)
(with-data (get-dataset :iris)
(doto (xy-plot :Sepal.Width :Sepal.Length :legend true)
(add-lines :Petal.Width :Petal.Length)
view))
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart x y & options]
`(let [opts# ~(when options (apply assoc {} options))
series-lab# (or (:series-label opts#)
(format "%s, %s" '~x '~y))
args# (concat [~chart ~x ~y] (apply concat (seq (apply assoc opts#
[:series-label series-lab#]))))]
(apply add-lines* args#))))
(defn add-function*
([chart function min-range max-range & options]
(let [opts (when options (apply assoc {} options))
step-size (or (:step-size opts)
(float (/ (- max-range min-range) 500)))
x (range min-range max-range step-size)
series-lab (or (:series-label opts)
(format "%s" 'function))]
(add-lines chart x (map function x) :series-label series-lab))))
(defmacro add-function
" Adds a xy-plot of the given function to the given chart, returning
a modified version of the chart.
Options:
:series-label (default x expression)
:step-size (default (/ (- max-range min-range) 500))
See also:
function-plot, view, save, add-function, add-points, add-lines
Examples:
(use '(incanter core stats charts))
;; plot the sine and cosine functions
(doto (function-plot sin (- Math/PI) Math/PI)
(add-function cos (- Math/PI) Math/PI)
view)
;; plot two normal pdf functions
(doto (function-plot pdf-normal -3 3 :legend true)
(add-function (fn [x] (pdf-normal x :mean 0.5 :sd 0.5)) -3 3)
view)
;; plot a user defined function and its derivative
(use '(incanter core charts optimize))
;; define the function, x^3 + 2x^2 + 2x + 3
(defn cubic [x] (+ (* x x x) (* 2 x x) (* 2 x) 3))
;; use the derivative function to get a function
;; that approximates its derivative
(def deriv-cubic (derivative cubic))
;; plot the cubic function and its derivative
(doto (function-plot cubic -10 10)
(add-function deriv-cubic -10 10)
view)
"
([chart function min-range max-range & options]
`(let [opts# ~(when options (apply assoc {} options))
series-lab# (or (:series-label opts#) (str '~function))
args# (concat [~chart ~function ~min-range ~max-range]
(apply concat (seq (apply assoc opts#
[:series-label series-lab#]))))]
(apply add-function* args#))))
(defn add-points*
([chart x y & options]
(let [opts (when options (apply assoc {} options))
data (:data opts)
_x (if (coll? x) (to-list x) ($ x data))
_y (if (coll? y) (to-list y) ($ y data))
data-plot (.getPlot chart)
n (.getDatasetCount data-plot)
series-lab (or (:series-label opts) (format "%s, %s" 'x 'y))
data-series (XYSeries. series-lab)
line-renderer (XYLineAndShapeRenderer. false true)
data-set (XYSeriesCollection.)]
(dorun
(map (fn [x y]
(if (and (not (nil? x))
(not (nil? y)))
(.add data-series (double x) (double y))))
_x _y))
(.setSeriesRenderingOrder (.getPlot chart) org.jfree.chart.plot.SeriesRenderingOrder/FORWARD)
(.setDatasetRenderingOrder (.getPlot chart) org.jfree.chart.plot.DatasetRenderingOrder/FORWARD)
(.addSeries data-set data-series)
(.setDataset data-plot n data-set)
(.setRenderer data-plot n line-renderer)
chart)))
(defmacro add-points
" Plots points on the given scatter-plot or xy-plot of the (x,y) points.
Equivalent to R's lines function, returns the modified chart object.
Options:
:series-label (default x expression)
Examples:
(use '(incanter core stats io datasets charts))
(def cars (to-matrix (get-dataset :cars)))
(def y (sel cars :cols 0))
(def x (sel cars :cols 1))
;; add regression line to scatter plot
(def lm1 (linear-model y x))
;; model the data without an intercept
(def lm2 (linear-model y x :intercept false))
(doto (xy-plot x (:fitted lm1) :legend true)
view
(add-points x y)
(add-lines x (:fitted lm2)))
(with-data (get-dataset :iris)
(doto (scatter-plot :Sepal.Length :Sepal.Width :data ($where {:Species \"setosa\"}))
(add-points :Sepal.Length :Sepal.Width :data ($where {:Species \"versicolor\"}))
(add-points :Sepal.Length :Sepal.Width :data ($where {:Species \"virginica\"}))
view))
;; of course this chart can be achieved in a single line:
(view (scatter-plot :Sepal.Length :Sepal.Width :group-by :Species :data (get-dataset :iris)))
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart x y & options]
`(let [opts# ~(when options (apply assoc {} options))
series-lab# (or (:series-label opts#) (format "%s, %s" '~x '~y))
args# (concat [~chart ~x ~y] (apply concat (seq (apply assoc opts#
[:series-label series-lab#]))))]
(apply add-points* args#))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn set-alpha
" Sets the alpha level (transparancy) of the plot's foreground
returns the modified chart object.
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart alpha]
(.setForegroundAlpha (.getPlot chart) alpha)
chart))
(defn set-background-alpha
" Sets the alpha level (transparancy) of the plot's background
returns the modified chart object.
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart alpha]
(.setBackgroundAlpha (.getPlot chart) alpha)
chart))
(defn clear-background
" Sets the alpha level (transparancy) of the plot's background to zero
removing the default grid, returns the modified chart object.
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart]
(.setBackgroundAlpha (.getPlot chart) 0.0)
chart))
(defn set-title
" Sets the main title of the plot, returns the modified chart object.
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart title]
(.setTitle chart title)
chart))
(defn set-x-label
" Sets the label of the x-axis, returns the modified chart object.
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart label]
(.setLabel (.getDomainAxis (.getPlot chart)) label)
chart))
(defn set-y-label
" Sets the label of the y-axis, returns the modified chart object.
References:
http://www.jfree.org/jfreechart/api/javadoc/
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html
"
([chart label]
(.setLabel (.getRangeAxis (.getPlot chart)) label)
chart))
(defn set-x-range
" Sets the range of the x-axis on the given chart.
Examples:
(use '(incanter core charts datasets))
(def chart (xy-plot :speed :dist :data (get-dataset :cars)))
(view chart)
(set-x-range chart 10 20)
"
([chart lower upper]
(-> chart
.getPlot
.getDomainAxis
(.setRange lower upper))
chart))
(defn set-y-range
" Sets the range of the y-axis on the given chart.
Examples:
(use '(incanter core charts datasets))
(def chart (xy-plot :speed :dist :data (get-dataset :cars)))
(view chart)
(set-y-range chart 10 60)
"
([chart lower upper]
(-> chart
.getPlot
.getRangeAxis
(.setRange lower upper))
chart))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NEW CHART FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- create-xy-plot
[title x-lab y-lab dataset legend? tooltips? urls?]
(org.jfree.chart.ChartFactory/createXYLineChart
title
x-lab
y-lab
dataset
org.jfree.chart.plot.PlotOrientation/VERTICAL
legend?
tooltips?
urls?))
(defn- create-time-series-plot
[title x-lab y-lab dataset legend? tooltips? urls?]
(org.jfree.chart.ChartFactory/createTimeSeriesChart
title
x-lab
y-lab
dataset
legend?
tooltips?
urls?))
(defn- create-xy-series-plot
([x y create-plot & options]
(let [opts (when options (apply assoc {} options))
data (:data opts)
_x (if (coll? x) (to-list x) ($ x data))
_y (if (coll? y) (to-list y) ($ y data))
_group-by (when (:group-by opts)
(if (coll? (:group-by opts))
(to-list (:group-by opts))
($ (:group-by opts) data)))
x-groups (when _group-by
(map #($ :col-0 %)
(vals ($group-by :col-1 (conj-cols _x _group-by)))))
y-groups (when _group-by
(map #($ :col-0 %)
(vals ($group-by :col-1 (conj-cols _y _group-by)))))
__x (if x-groups (first x-groups) _x)
__y (if y-groups (first y-groups) _y)
title (or (:title opts) "")
x-lab (or (:x-label opts) (str 'x))
y-lab (or (:y-label opts) (str 'y))
series-lab (or (:series-label opts)
(if x-groups
(format "%s, %s (0)" 'x 'y)
(format "%s, %s" 'x 'y)))
theme (or (:theme opts) :default)
legend? (true? (:legend opts))
points? (true? (:points opts))
data-series (XYSeries. series-lab)
dataset (XYSeriesCollection.)
chart (do
(dorun
(map (fn [x y]
(if (and (not (nil? x))
(not (nil? y)))
(.add data-series (double x) (double y))))
__x __y))
(.addSeries dataset data-series)
(create-plot
title
x-lab
y-lab
dataset
legend?
true ; tooltips
false))
_ (when x-groups
(doseq [i (range 1 (count x-groups))]
(add-lines chart (nth x-groups i)
(nth y-groups i)
:series-label (format "%s, %s (%s)" 'x 'y i)
:points points?)))]
(.setRenderer (.getPlot chart) 0 (XYLineAndShapeRenderer. true points?))
(set-theme chart theme)
chart)))
(defn xy-plot* [x y & options]
(apply create-xy-series-plot x y create-xy-plot options))
(defmacro xy-plot
" Returns a JFreeChart object representing a xy-plot of the given data.
Use the 'view' function to display the chart, or the 'save' function
to write it to a file.
Options:
:data (default nil) If the :data option is provided a dataset,
column names can be used instead of sequences
of data as arguments to xy-plot.
:title (default 'XY Plot') main title
:x-label (default x expression)
:y-label (default 'Frequency')
:legend (default false) prints legend