4
$\begingroup$

I produced the following time series ListLinePlot:

enter image description here

using the following Code (thanks to many of the contributors in this forum)

q1 = {{2.0779957`*^7, 2.1606992`*^7, 2.2600774`*^7, 2.3680871`*^7, 
    2.4726689`*^7, 2.5654274`*^7, 2.6433058`*^7, 2.7100542`*^7, 
    2.7722281`*^7, 2.8394806`*^7, 2.9185511`*^7, 3.0117411`*^7, 
    3.1161378`*^7, 3.2269592`*^7, 3.3370804`*^7, 3.4413603`*^7, 
    3.5383028`*^7, 3.6296111`*^7, 3.7171922`*^7, 3.8041757`*^7, 
    3.8928341`*^7}};
q2 = {{2.97505722281038`, 3.90280496415438`, 4.4967187466326`, 
    4.66834379545461`, 4.32155951673842`, 3.68269988149014`, 
    2.9905238360968`, 2.49383039084495`, 2.26827304079294`, 
    2.39697830237036`, 2.74661463795293`, 3.14310555651301`, 
    3.40760003260595`, 3.49459192739625`, 3.35560233082199`, 
    3.07705362914441`, 2.77803478841236`, 2.54783303994568`, 
    2.38430907940751`, 2.31307334941778`, 2.30381209309418`}};

qw1 = ListLinePlot[
   q1,
   PlotLabel -> "Afghanistan",
   PlotStyle -> {Thin, Blue},
   ImagePadding -> 25,
   ImageSize -> 500,
   AspectRatio -> 0.4,
   Frame -> {{True, False}, {True, False}},
   FrameStyle -> {Directive[FontFamily -> "Helvetica", Bold], 
     Directive[FontFamily -> "Helvetica", Bold, Blue], Automatic, 
     Automatic}
   ];
qw2 = ListLinePlot[
   q2,
   PlotLabel -> "Afghanistan",
   PlotStyle -> {Thin, Red},
   ImagePadding -> 25,
   ImageSize -> 500,
   AspectRatio -> 0.4,
   Axes -> False,
   Frame -> {{False, True}, {False, False}},
   FrameTicks -> {{None, All}, {None, None}},
   FrameStyle -> {Automatic, Automatic, Automatic, 
     Directive[FontFamily -> "Helvetica", Bold, Red]}
   ];

Labeled[
 Overlay[{qw1, qw2}],
 Style[#, 14, #2, ShowStringCharacters -> False] & @@@ 
  Transpose[{{Rotate["Population", 90 Degree], 
     Rotate["Population growth rate (%)", 90 Degree], "Years"}, {Blue,
      Red, Black}}], {Left, Right, Bottom}
 ]

My purpose is to color the fluctuating series (population growth) with RED but I cannot find where my mistake is.

$\endgroup$
4
  • 1
    $\begingroup$ try PlotStyle -> Directive@{Thin, Blue} instead of PlotStyle ->{Thin, Blue} (Similarly, PlotStyle -> Directive@{Thin, Red} in the second plot). $\endgroup$
    – kglr
    Nov 8, 2021 at 23:23
  • $\begingroup$ @kglr: When I add date=Range[2000,2020] and change the plot qw1 as qw1 = ListLinePlot[Transpose@{date, q1},...] the plot is not working. Why do you think? $\endgroup$ Nov 8, 2021 at 23:39
  • 2
    $\begingroup$ There is an official "How To". And if you wait just a bit, there is a new feature coming up in V13, called multiple axis plots :-) $\endgroup$
    – Domen
    Nov 9, 2021 at 0:06
  • 1
    $\begingroup$ Similar discussions: 164846 and 627 $\endgroup$
    – Syed
    Nov 9, 2021 at 7:13

3 Answers 3

5
$\begingroup$

You can get the desired result using a single ListLinePlot as follows:

ListLinePlot[Join[q1, Rescale[q2,  {0, 5}, MinMax @ q1]], 
 Frame -> True,
 PlotStyle -> (Directive[Thin, #] & /@ {Blue, Red}),
 FrameTicks -> {{Automatic, Charting`FindTicks[ MinMax@q1, {0, 5}]}, 
    {Automatic, Automatic}}, 
 FrameLabel -> {{"Population", "Population growth rate (%)"}, {"Year", None}},
 FrameStyle -> {{Directive[FontFamily -> "Helvetica", Bold, Blue], 
    Directive[FontFamily -> "Helvetica", Bold, Red]}, {Automatic, Automatic}},
 PlotLabel -> "Afganistan",
 ImageSize -> 500,
 AspectRatio -> 0.4]

enter image description here

$\endgroup$
3
  • $\begingroup$ Using my original code, I created a Manipulate to see for each country the two time series. It works just as expected. However, your Code above is much elegant and short, and wanted to use it in a Manipulate. Therefore, I prepared a Manipulate by simply replacing the corresponding inputs for q1 with grjdataWB[countries, var1] and for q2 with grjdataWB[countries, var2]. The arguments of grjdataWB[..] are controls. It does not work and I get an error saying HeadList and Plus at positions 1 and 2 are to be the same. $\endgroup$ Nov 9, 2021 at 1:05
  • 1
    $\begingroup$ Wouldn't {0,5} be MinMax@q2? For some cases, I have no list plot for the RED axis variable, although there are data attached to it. $\endgroup$ Nov 9, 2021 at 23:28
  • 1
    $\begingroup$ Yes. I picked {0,5} to get a picture similar to the one in your post. $\endgroup$
    – kglr
    Nov 10, 2021 at 1:27
4
$\begingroup$

you can always use ResourceFunction["CombinePlots"] in such a case

ResourceFunction["CombinePlots"][
 ListLinePlot[q1, PlotLabel -> "Afganistan", ImageSize -> 500, 
  AspectRatio -> 0.4, Frame -> True, PlotStyle -> Blue, 
  FrameLabel -> {"Years", "Population", None, None}, 
  FrameStyle -> {Directive[{Blue, FontFamily -> "Helvetica", 12}], 
    Directive[{Black, FontFamily -> "Helvetica", 12}]}], 
 ListLinePlot[q2, PlotLabel -> "Afganistan", ImageSize -> 500, 
  AspectRatio -> 0.4, Frame -> True, PlotStyle -> Red, 
  FrameLabel -> {"Years", "Population growth rate (%)", None, None}, 
  FrameStyle -> Directive[{Red, FontFamily -> "Helvetica", 12}]], 
 "AxesSides" -> "TwoY"]   

enter image description here

$\endgroup$
1
$\begingroup$

I found out the reason and corrected it as follows:

q1 = grjdataWB["Afghanistan", "Population, total"] // Normal // 
   Flatten;
q2 = grjdataWB["Afghanistan", "Population growth (annual %)"] // 
    Normal // Flatten; 
date = Range[2000, 2020];

The reason is that data comes from a Mathematica Dataset and therefore needs to be adjusted by //Normal//Flatten to get the correct plot with RED color. Although the reason is identified, I think it is better to keep this question as it shows that the data extracted from a Mathematica Dataset should be adjusted if one wants to produce the 2-Y-axes plot as shown.

$\endgroup$

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.