4

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.

Tugrul Temel
  • 6,193
  • 3
  • 12
  • 32

3 Answers3

5

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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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. – Tugrul Temel Nov 09 '21 at 01:05
  • 1
    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. – Tugrul Temel Nov 09 '21 at 23:28
  • 1
    Yes. I picked {0,5} to get a picture similar to the one in your post. – kglr Nov 10 '21 at 01:27
4

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

MMA13
  • 4,664
  • 3
  • 15
  • 21
1

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.

Tugrul Temel
  • 6,193
  • 3
  • 12
  • 32