3
$\begingroup$

What is the easiest way to plot something like

enter image description here

where either x-y axes pair is independent of the other. I tried combining BarChart and ListPlot, but with no success, as the scaling really messes everything up. Any ideas? I have heard about MultiaxisArrangement, but I am not too sure how that would work with a bar chart.

Here is a working example:

plot1 = BarChart[{1, 2, 3, 4}, 
  ChartLabels -> 
   Placed[{"data1", "data2", "data3", "data4"}, Below, 
    Rotate[#, Pi/2] &], ChartStyle -> "Pastel", 
  AxesLabel -> {None, None}, BarSpacing -> 0.2, AspectRatio -> .5, 
  ImageSize -> 300, Frame -> True, FrameLabel -> "Number of data", 
  GridLines -> Automatic, GridLinesStyle -> Directive[LightGray]] 

dataPoints = {{1997, 9}, {1998, 14}, {1999, 12}, {2000, 17}, {2001, 
    17}, {2002, 28}, {2003, 21}, {2004, 16}, {2005, 30}, {2006, 
    29}, {2007, 31}, {2008, 37}, {2009, 42}, {2010, 62}, {2011, 
    56}, {2012, 61}, {2013, 54}, {2014, 82}, {2015, 72}, {2016, 
    76}, {2017, 79}, {2018, 75}, {2019, 58}, {2020, 79}, {2021, 
    78}, {2022, 80}, {2023, 60}};

plot2 = ListPlot[dataPoints, PlotStyle -> {Black}, 
  PlotMarkers -> {"\[FilledSquare]", 10}, Axes -> False, 
  Frame -> True, 
  FrameLabel -> {{None, "Number of events"}, {None, "Year"}}, 
  FrameTicks -> {{None, All}, {None, All}}, AspectRatio -> .5]

enter image description here

$\endgroup$
1
  • $\begingroup$ Can it be deduced that the number of papers (shown in red) is declining every year? Or that there are many events happening in toxicology compared to physics? $\endgroup$
    – Syed
    Feb 3 at 19:05

2 Answers 2

6
$\begingroup$

We can use ResourceFunction["CombinePlots"]

combinePlots = ResourceFunction["CombinePlots"];

bc = BarChart[{1, 2, 3, 4}, 
   ChartLabels ->  
     Placed[{"data1", "data2", "data3", "data4"}, Below, Rotate[#, Pi/2] &], 
   ChartStyle -> "Pastel", 
   BarSpacing -> 0.2, 
   AspectRatio -> .5, 
   Frame -> True, 
   FrameLabel -> {None, "Number of Data"}, 
   GridLines -> Automatic, 
   GridLinesStyle -> Directive[LightGray], 
   ImageSize -> 800];

lp = ListPlot[dataPoints, 
   PlotStyle -> Black, 
   PlotMarkers -> {"■", 10}, 
   Frame -> True, 
   FrameLabel -> {"Year", "Number of Events"}, 
   FrameTicks -> All, 
   AspectRatio -> .5];


combinePlots[bc, lp, "AxesSides" -> "TwoXY"]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you, kglr, I updated my answer $\endgroup$
    – eldo
    Feb 4 at 7:26
6
$\begingroup$
plot1 =
 BarChart[{1, 2, 3, 4}, 
  ChartLabels -> 
   Placed[{"data1", "data2", "data3", "data4"}, Below, Rotate[#, Pi/2] &], 
  ChartStyle -> "Pastel", 
  AxesLabel -> {None, None}, 
  BarSpacing -> 0.2, 
  AspectRatio -> .5, 
  Frame -> True, 
  FrameLabel -> "Number of data", 
  GridLines -> Automatic, 
  GridLinesStyle -> Directive[LightGray], 
  ImagePadding -> 50,
  ImageSize -> 800];

plot2 = 
 ListPlot[dataPoints, 
  PlotStyle -> {Black}, 
  PlotMarkers -> {"\[FilledSquare]", 10}, 
  Axes -> False, 
  Frame -> True, 
  FrameLabel -> {{None, "Number of events"}, {None, "Year"}}, 
  FrameTicks -> {{None, All}, {None, All}}, AspectRatio -> .5, 
  ImagePadding -> 50,
  ImageSize -> 800];

Overlay[{plot1,plot2}]

enter image description here

Addendum

As kglr commented you can use Overlay[{plot1, plot2}, All, 2] to get tooltips for the black data points.

$\endgroup$
5
  • $\begingroup$ Interesting, but it seems I need to manually adjust the padding value, right? Is there a way of making this automatic? $\endgroup$
    – sam wolfe
    Feb 3 at 18:47
  • $\begingroup$ I don't think so. I always do it manually with these Overlay - plots. It doesn't take too long. $\endgroup$
    – eldo
    Feb 3 at 18:50
  • $\begingroup$ Just one more question, is it possible to set the padding of the final object to be automatically tight? $\endgroup$
    – sam wolfe
    Feb 3 at 19:18
  • $\begingroup$ The tightest would be ImagePadding -> 31. But you have to set it manually for both plots. I don't know an automatic way to find the 31. $\endgroup$
    – eldo
    Feb 4 at 0:02
  • $\begingroup$ (+1) You can retain interactive highlighting of plot2 using Overlay[{plot1, plot2}, All, 2]. $\endgroup$
    – kglr
    Feb 4 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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