如何根据一系列值创建Incanter系列

时间:2012-04-23 23:48:56

标签: clojure incanter

我有一个包含3列的Incanter数据集:日期/时间戳,响应时间和消息大小。我想要做的是创建一个散点图,其中x轴上的日期/时间戳和响应时间为y轴。

这很容易,但我想基于邮件大小列生成单独的一系列数据。 Incanter的scatter-plot函数采用:group-by选项,但它似乎只处理离散值。我希望通过将一些函数应用于消息大小列来生成系列。一些功能如:

(fn [n]
  (cond
    (< n 5000)                    "small"
    (and (>= n 5000) (< n 20000)) "medium"
    (>= n 20000)                  "large"))

这可能还是有更好的方法来完成同样的事情?

1 个答案:

答案 0 :(得分:2)

你可以用一个新的列合成一个数据集,其中使用你的函数计算离散值,就像这样......

(def dataset1 (dataset 
               [:x :y] 
               (for [x (range 10) y (range 10)] [x y])))
;=> #'user/dataset1

dataset1
[:x :y]
[0 0]
[0 1]
...
[9 8]
[9 9]

(def dataset2 (with-data dataset1 
  (conj-cols $data 
     (dataset [:size] ($map #(cond
                              (< % 3)   "small"
                              (<= 3 % 6) "medium"
                              (< 6 %)   "large") :x)))))
;=> #'user/dataset2

dataset2
[:x :y :size]
[0 0 "small"]
[0 1 "small"]
...
[9 8 "large"]
[9 9 "large"]

添加,然后对您生成的离散值使用:group-by ...

(with-data dataset2 
   (view 
      (scatter-plot 
       :x 
       :y 
       :group-by :size )))

给出类似的东西:

incanter plot

从两列生成分组的变体:

 (def dataset3 
  (with-data dataset1  
    (conj-cols 
      $data 
      (dataset [:size] ($map #(let [sum (+ % %2)] 
                               (cond
                                 (< sum 4 )    "small"
                                 (<= 4 sum 12) "medium"
                                 (> 12 sum )   "large")) [:x :y])))))

这样的情节:

sum plot

相关问题