Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • graph twoway line: How to create a condition (if for example prices are the same for two lines)?

    Hi everyone,

    I would like to create a graph in which, when the two prices are identical, the green colour must prevail over the red colour (i.e. on the graph, when the two values are identical, the green colour must appear).

    I enclosed the graph. What I want is that green line should the only one to be viewed from 01 July 2021 to 15 June 2022 (where both lines coincide). Again for approximately on the 1 January 2023 onwards.
    In-between, the two lines should be on the graph, as their prices are not the same.

    Original graph:
    Click image for larger version

Name:	twoway_lines.png
Views:	1
Size:	76.3 KB
ID:	1733150









    dataex:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double Date float(CompetitivePrice CompetitivePrice_noInter day_Date month_Date year_Date)
    22462 110.08 110.08  1 7 2021
    22463 110.58 110.58  2 7 2021
    22464 102.26 102.26  3 7 2021
    22465  88.82  88.82  4 7 2021
    22466 106.42 106.42  5 7 2021
    22467 105.88 105.88  6 7 2021
    22468 107.03 107.03  7 7 2021
    22469 101.45 101.45  8 7 2021
    22470 104.71 104.71  9 7 2021
    22471 107.55 107.55 10 7 2021
    end
    format %td Date
    I tried something, but it gave me a weird graph:

    Code:
    gen SamePrice = (CompetitivePrice == CompetitivePrice_noInter)
    # delimit;
    
    twoway (line CompetitivePrice Date if SamePrice, lcolor(green))
           (line CompetitivePrice Date if !SamePrice, lcolor(red)),
           legend(cols(1) label(1 "Factual") label(2 "No Intervention Scenario")
                          label(3 "Long Intervention Scenario") position(12) ring(0))
           xlabel(, angle(45))
           ylabel(0(100)800)
           tlabel(01jul2021 01oct2021 01jan2022 01apr2022 01jul2022 01oct2022 01jan2023 01apr2023 31jul2023)
           xtitle("Date") ytitle("€/MWh")
           plotregion(margin(none))
           ;
    # delimit cr
    Attempted graph with above code:
    Click image for larger version

Name:	attempted_twoway_lines.png
Views:	1
Size:	63.9 KB
ID:	1733152






    Could you please tell me how to do this?
    Thanks in advance.

    Michael
    Last edited by Michael Duarte Goncalves; 08 Nov 2023, 01:29.

  • #2
    If you plot multiple graphs with twoway this first graph will be drawn first, the second on top of that, the third on top of that, etc. So if you want the green curve to be on top if the green and red curve are the same, you just specify it last. No need for if conditions.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Red and green should never be juxtaposed or superimposed in any case, as difficulty in distinguishing red and green is one of the most kinds of so-called colour blindness. See e.g. https://venngage.com/blog/color-blind-friendly-palette/

      Red and blue or orange and blue often work well together.

      Comment


      • #4
        Good morning Maarten Buis,
        Good morning Nick Cox,

        Thank you very much for your answers!

        I tried #2 and it works perfectly well. Thank you!
        #3: Thank you for your feedback! You are right, I will change that.

        Lovely morning to both of you.
        Best,

        Michael

        Comment

        Working...
        X