The Dataset documentation doesn't indicate how to label a Histogram with the key.

titanic = 
  ExampleData[{"Dataset", "Titanic"}][All, {"survived" ->(# /. {True -> 1, False -> 0} &)}]

enter image description here

titanic[Histogram, #] & /@ {"age", "survived"}


enter image description here

(Note, unlike functions such as Counts, which can operate on tuples of keys (ie, counts the products), it doesn't seem possible to use the syntax: titanic[Histogram, {"age", "survived"}]).

How can a Key be passed programmatically as argument to Histogram (or other operators) in order to label it, eg "age", "survived" ?

Of course it's possible to project individual columns with KeyTake and then histogram but that's not the same as operating on the data.

  • $\begingroup$ Perhaps titanic[Composition[Histogram[#, ChartLabels -> Automatic] &, Pivot[#, 2] & ], {"age"}]. (but Pivot is probably going away soon) $\endgroup$ – Rojo Jul 24 '14 at 0:50
  • $\begingroup$ @Rojo, Pivot isn't documented and its definition is unreadably full of macros & `PackagePrivate parts. Where did you read about it? $\endgroup$ – alancalvitti Jul 30 '14 at 18:11
  • $\begingroup$ It used to be documented before the final release. And it will go away, but I am hoping as it goes away, some other function will take its job because I find it pretty useful $\endgroup$ – Rojo Jul 30 '14 at 18:32
  • $\begingroup$ Do you mean something like titanic[Histogram[#[[All, 1]], PlotLabel -> First@Keys@#[[1]]] &, {#}] & /@ {"age", "survived"} ? $\endgroup$ – Ronald Monson Jul 31 '14 at 22:32

You don't have to modify what you already tried by very much.

  With[{key = #}, 
    titanic[Histogram[#, PlotLabel -> key] &, key]] & /@ {"age", "survived"}]



The comment by the OP is not clear to me. I offer up the following as my best interpretation of what he is asking for. I hope I'm not being obtuse.

  With[{val = #[[1]], lbl = #[[2]]}, 
      titanic[Select[#survived == val &]]
        [Histogram[#, {0, 80, 5}, PlotLabel -> lbl]&, "age"]]& 
      {{1, "Survivors by Age"}, {0, "Non-survivors by Age"}}]


  • $\begingroup$ +1, yet this method also is not a pure operator, since it partly straddles Dataset. $\endgroup$ – alancalvitti Jul 21 '14 at 19:06
  • $\begingroup$ can you redefine this method as a Query operator? $\endgroup$ – alancalvitti Jul 29 '14 at 19:09

Hopefully I don't misunderstand the question. It's not fancy or clever but basic programming still works:

f[x_] := Labeled[Histogram @ #, x] &;

titanic[f[#], #] & /@ {"age", "survived"}

enter image description here

The same thing as a Table:

  titanic[Histogram[#] ~Labeled~ x &, x],
  {x, {"age", "survived"}}
  • $\begingroup$ @m_goldberg Weird! There are no quote marks on screen but they appear when I export (Save Selection As...) to PNG. I didn't even notice that before. $\endgroup$ – Mr.Wizard Jul 21 '14 at 7:39
  • $\begingroup$ Is it not possible to redefine your solution inline? $\endgroup$ – alancalvitti Jul 21 '14 at 19:09
  • $\begingroup$ @alancalvitti What do you mean by "inline" in this case? Would you prefer titanic[(x \[Function] Labeled[Histogram@#, x] &)[#], #] & /@ {"age", "survived"} ? $\endgroup$ – Mr.Wizard Jul 22 '14 at 1:56
  • $\begingroup$ by inline I mean as Query operators. $\endgroup$ – alancalvitti Jul 29 '14 at 19:08

How about

Histogram[titanic[[All, #]], ChartLabels -> Placed[#, Above], 
    ImageSize -> 200] & /@ {"age", "survived"} // Row

Gives this

enter image description here

  • $\begingroup$ titanic[[All,#]] is a projection like I mention in the last paragraph of my Q, it's not an operator applied to Dataset. $\endgroup$ – alancalvitti Jul 21 '14 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.