3
$\begingroup$

I have two sets of data that live on different scales, but I would like to plot them together to show the distributions of values side-by-side. Is there a way to add a second y-axis on the right that corresponds to (and scales) that respective data properly.

For example:

DistributionChart[{RandomReal[{0, 1.5}, 1000], 
  RandomReal[{0, 0.01}, 1000]}]

generates a squished distribution for the second variable. I'd like a second y axis on the right that goes from say (0,0.01).

$\endgroup$
5
$\begingroup$
SeedRandom[1]
{data1, data2} = {RandomReal[{0, 1.5}, 1000], RandomReal[{0, 0.01}, 1000]};

You can Rescale data2 to have the same scale as data1 and construct the correct frame ticks for the right edge using Charting`FindTicks:

DistributionChart[{data1, Rescale[data2, MinMax @ data2, MinMax @ data1]}, 
  FrameTicks -> {{Automatic, 
     Charting`FindTicks[MinMax @ data1, MinMax @ data2][## & @@ MinMax[data1]]},
    {None, None}}]

enter image description here

To get the correct tooltips for data2, we can extract the tooltip from the DistributionChart with original data2 as input and use it as the tooltip for the rescaled data2:

tooltip2 = Cases[DistributionChart[{data2}], Tooltip[_, t_] :> t, All][[1]];
DistributionChart[{data1, 
    Tooltip[Rescale[data2, MinMax @ data2, MinMax @ data1], tooltip2]},
  FrameTicks -> {{Automatic, 
      Charting`FindTicks[ MinMax@data1, MinMax@data2][## & @@ MinMax[data1]]}, 
    {None, None}}]

enter image description here

Alternatively, you can use logarithmic scaling:

 DistributionChart[{data1,data2}, ScalingFunctions -> "Log"]

enter image description here

$\endgroup$
  • $\begingroup$ Perfect, nice catch with tooltips. I didn't even notice the scaling issue until you edited the change. $\endgroup$ – Gregory May 24 '19 at 15:28

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.