Announcement

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

  • When plotting a graph in stata, how do I configure the x-axis to have two points?

    Click image for larger version

Name:	stata_result.png
Views:	3
Size:	106.1 KB
ID:	1714051 Click image for larger version

Name:	stata_result.png
Views:	3
Size:	106.1 KB
ID:	1714052 Hello

    I'm studying STATA and I'm asking for help with something that I haven't personally been able to solve.

    The structure of my data is as follows.


    con_ym con_y con_m TM local_2 trade_volume
    202204 2022 4 2022/04 seoul 1828
    202205 2022 5 2022/05 seoul 1831
    202206 2022 6 2022/06 seoul 1128
    202207 2022 7 2022/07 seoul 676
    202208 2022 8 2022/08 seoul 705
    202209 2022 9 2022/09 seoul 641
    202210 2022 10 2022/10 seoul 566
    202211 2022 11 2022/11 seoul 743
    202212 2022 12 2022/12 seoul 777
    202301 2023 1 2023/01 seoul 1453
    202302 2023 2 2023/02 seoul 2511
    202303 2023 3 2023/03 seoul 3045
    202304 2023 4 2023/04 seoul 2990

    I'm trying to make a graph like the one attached below in Excel using this data.

    Click image for larger version

Name:	Excel_result.png
Views:	1
Size:	10.4 KB
ID:	1714049



    Currently, the best I can do is to use the command below for the above data.

    I am using the 2nd attached graph result, but it is not visually appealing and I would like to improve it.

    Currently, I'm using the following command against the above data
    I am using the 2nd attached graph result.

    gen TM2 = ym(con_y,con_m)
    format %tmYY-NN TM2
    gen temp = string(TM2, "%tmYY/NN")
    labmask TM2, values(temp)
    graph bar v_tr_pos v_tr_neg v_tr_null, over(TM2), if local_2==2 & v_trade_rate !=., stack blabel(total, format(%9.1f))legend(off) ylabel(, angle(0))

    Click image for larger version

Name:	stata_result.png
Views:	3
Size:	106.1 KB
ID:	1714050



    I tried to find a way to make it look like the graph I made in excel in stata,
    I couldn't figure it out, so I'm asking a question.

    Please share your ideas on how to make the x-axis look like an excel graph.

    Thank you in advance for your help.

  • #2
    Your code and data example don't fully hang together. but I think what you want to do is reasonably clear.

    Conversely, I know very little about MS Excel and I don't endorse all the design choices in your example, whether they are yours or the software defaults. The bars are too thin for my taste, for one.

    I have reason to respect labmask (from the Stata Journal) but you don't need it here. What you want would I think be harder to get with graph bar. I recommend doing this with twoway. See also https://www.stata-journal.com/articl...article=gr0030

    Here's some technique.

    Code:
    clear
    input int con_y byte con_m int trade_volume
    2022  4 1828
    2022  5 1831
    2022  6 1128
    2022  7  676
    2022  8  705
    2022  9  641
    2022 10  566
    2022 11  743
    2022 12  777
    2023  1 1453
    2023  2 2511
    2023  3 3045
    2023  4 2990
    end
    
    gen mdate = ym(con_y,con_m)
    
    su trade_volume, meanonly 
    gen toshow = trade_volume - r(mean)
    
    rename con_y year 
    
    su mdate if year == 2022, meanonly 
    local m1 = r(mean)
    
    su mdate if year == 2023, meanonly 
    local m2 = r(mean)
    
    levelsof mdate, local(labels)
    
    gen where = -1000 
    gen what = string(toshow, "%4.1f")
    
    twoway scatter where mdate, ms(none) mla(what) mlabpos(0) mlabc(black) ///
    || bar toshow mdate if toshow >= 0, barw(0.8) color(blue) ///
    || bar toshow mdate if toshow <  0,  barw(0.8) color(red) /// 
    xla(`labels', noticks format(%tmNN)) yla(-500(500)1500) /// 
    xmla(`m1' "2022" `m2' "2023", tlength(*6) tlc(none) labsize(medium)) ///
    legend(off) ytitle(Trade volume MINUS mean) xtitle("") xli(`=ym(2022, 12) + 0.5', lp(solid) lw(thin))
    Click image for larger version

Name:	barsanddates.png
Views:	1
Size:	37.5 KB
ID:	1714057

    Comment


    • #3
      I spent quite a bit of time thinking about this on my own.
      Thank you for giving me a clue that allowed me to solve it much faster than I expected.

      Thanks to Nick Cox, I learned a great way to do it.

      I'm going to try it on my own and will definitely give you a good review again.

      Thanks again.

      Comment


      • #4
        hi

        I'm reaching out again because I'm trying to figure out what I was trying to do based on the code you answered earlier, and I have a question.

        First, I'd like to more accurately describe what I have and then rephrase my question.

        ym year mon TM local_2 trade_volume v_trade_rate
        202204 2022 4 2022/04 Seoul 1828 22.5
        202205 2022 5 2022/05 Seoul 1831 0.2
        202206 2022 6 2022/06 Seoul 1128 -38.4
        202207 2022 7 2022/07 Seoul 676 -40.1
        202208 2022 8 2022/08 Seoul 705 4.3
        202209 2022 9 2022/09 Seoul 641 -9.1
        202210 2022 10 2022/10 Seoul 566 -11.7
        202211 2022 11 2022/11 Seoul 743 31.3
        202212 2022 12 2022/12 Seoul 777 4.6
        202301 2023 1 2023/01 Seoul 1453 87.0
        202302 2023 2 2023/02 Seoul 2511 72.8
        202303 2023 3 2023/03 Seoul 3045 21.3
        202304 2023 4 2023/04 Seoul 2990 -1.8


        First of all, I am trying to find the percentage increase and decrease from the previous month based on the trade_volume for each month, and for this purpose, I am modifying some of the code to create a graph similar to the graph in Excel in the previous question, excluding the contents of the code you provided as unnecessary.

        Below is the code I am currently writing.

        gen trade_volume = 1
        collapse (count) trade_volume if local_2 =="seoul", by(local_2 TM con_ym con_y con_m)
        sort local_2 con_y con_m
        gen v_trade_rate = ((trade_volume[_n] - trade_volume[_n-1]) / trade_volume[_n-1]) * 100 if local_2[_n] == local_2[_n-1]

        capture generate v_tr_pos = cond(v_trade_rate>=0,v_trade_rate,.)
        capture generate v_tr_neg = cond(v_trade_rate<0,v_trade_rate,.)
        capture generate v_tr_null = .

        keep if tm(2022/04)<=TM & TM <=tm(2023/04)
        gen TM2 = ym(con_y,con_m)
        format %tmYY-NN TM2
        gen temp = string(TM2, "%tmYY/NN")
        labmask TM2, values(temp)

        gen mdate = ym(con_y,con_m)

        su mdate if con_y == 2022, meanonly
        local m1 = r(mean)

        su mdate if con_y == 2023, meanonly
        local m2 = r(mean)

        levelsof mdate, local(labels)

        format %9.1f v_trade_rate

        twoway scatter v_trade_rate mdate, ms(none) mla(v_trade_rate) mlabposition(12) mlabc(black)
        || bar v_trade_rate mdate if v_trade_rate > 0, barw(0.8) color(blue) ///
        || bar v_trade_rate mdate if v_trade_rate < 0, barw(0.8) color(red) ///
        xla(`labels', noticks format(%tmNN)) ///
        xmla(`m1' "2022" `m2' "2023", tlength(*8) tlc(none) labsize(medium)) ///
        legend(off) ytitle(" ") xtitle("") title("volume compared to the previous month (%)", margin(b=3)) xli(`=ym(2022, 12) + 0.5', lp(solid) lw(thin))

        When you run the above code, you'll notice that the negative values for June, July, September, and October 2022 are not displayed correctly.

        I tried to change the placement by entering the number corresponding to the clock value through the mlabpostion option, but I want to display it at the top of the graph if it is positive and at the bottom of the graph if it is negative, but I am not able to implement it properly as shown in the photo attached below.



        If you have any idea on how to solve this, I would appreciate your help again.

        Attached Files
        Last edited by YEONOH HAN; 22 May 2023, 00:52.

        Comment


        • #5
          I am using Stata 18 and scheme stcolor. It looks as if you are using some earlier version and scheme s2color. That shouldn't matter much, although many people dislike s2color. For versions < 18, I used to start with s1color. More importantly, you can change the line color to whatever you want. See the FAQ Advice for the position on which version people are using; if you are using an older version than current, you are asked to say what that is.

          All the marker labels are plotted at position 12, so above the value shown. When the bar shows a negative value, the marker label cannot usually be seen. The bar usually occludes the marker label. The value -1.8 is not an exception; it is just that the bar is so short that you can see the label.

          Run this script to see the problem and a solution.

          Code:
          clear 
          set obs 6 
          gen y = 3 - _n 
          gen t = _n 
          twoway scatter y t, ms(none) mla(y) mlabpos(12) || bar y t 
          
          gen pos = cond(y < 0, 6, 12)
          twoway scatter y t, ms(none) mla(y) mlabvpos(pos) || bar y t

          Comment


          • #6
            I utilized your code to create the desired result.

            Nick Cox, thank you so much for being so helpful.

            Thanks again for sharing your great knowledge for STATA beginners.

            I wish you all the best and happy days ahead.

            Click image for larger version

Name:	result.png
Views:	1
Size:	64.2 KB
ID:	1714527

            Comment

            Working...
            X