Announcement

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

  • Graph: x-axis labels -> change to desired months

    Hi everyone

    I am trying to generate a graph of an index. My data (year,month) ranges from 199901 to 201812. However, the graph displays on the x-axis the values from 200001 to 202001

    The data is the following (sorry, it's a bit long):

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(Jahr_Monat SENT SENT_ORT)
    468 . .
    469 -.9883735 .17571376
    470 -.6953934 .3340038
    471 -.4804287 .08225542
    472 -.2135436 -.1733986
    473 -.40797755 -.6598344
    474 -.20301445 -.33490825
    475 -.087966 -.24500842
    476 .40384895 -.1362675
    477 .6014041 .4293425
    478 .50686157 .5251744
    479 .4979403 1.0525099
    480 .54874825 1.7644436
    481 .6212594 1.5954952
    482 .7128802 1.928487
    483 .7200878 1.0643696
    484 .6987402 1.478783
    485 1.0160284 1.3999777
    486 1.122921 1.5570377
    487 .8487988 1.3295542
    488 1.0435077 1.332692
    489 .7587039 1.1980356
    490 .7933701 .9344496
    491 .7376158 .8985572
    492 .8905743 1.2440665
    493 .7886781 1.5811487
    494 .50602764 1.449081
    495 .4108059 .4331162
    496 .15761256 .3449513
    497 -.07436032 .42745325
    498 -.48415455 .02644596
    499 -.465555 .13633555
    500 -1.531624 .8986828
    501 -2.3136506 -.48848554
    502 -2.388872 -1.2838695
    503 -2.0963929 -2.1475174
    504 -.52402335 -1.2027028
    505 -.473429 -.013739068
    506 -.3525708 .3096239
    507 -.70109 -.14804712
    508 -.26204827 -.05046533
    509 -.9030384 .36876205
    510 -2.547417 .14716044
    511 -2.2635396 -.10656934
    512 -2.3641849 -.4875911
    513 -2.3782527 -.9783503
    514 -1.9338255 -1.6837498
    515 -1.5755464 -1.3207465
    516 -1.3992268 -1.2115955
    517 -1.8999293 .7026442
    518 -2.4891555 .8523473
    519 -2.3744946 -.38552395
    520 -2.2098827 -.6340695
    521 -2.4586594 -1.2069182
    522 -1.8027 -1.4833723
    523 -.9671379 -1.3858428
    524 -.8685479 -1.400233
    525 -.7697501 -.604431
    526 -1.0154068 -.6951205
    527 -.15059333 -1.3649715
    528 -.14633152 -.0792993
    529 .01277743 .56845385
    530 -.3973399 .5889692
    531 .2277742 .0040735425
    532 -.07434668 .28324583
    533 -.026934003 .555994
    534 .2985708 .034490287
    535 -.02677937 -.24436995
    536 .27823198 -.3821239
    537 .05901428 -.5832725
    538 .14441074 -.7316566
    539 .3231294 -.59394413
    540 .3038368 -.072538145
    541 -.08542635 -.24338
    542 -.18277746 -.3218683
    543 -.4696557 -.1813648
    544 -.3480627 -.7211398
    545 .0018222064 -.8706591
    546 .12823159 -1.5658035
    547 -.18932045 -1.4131793
    548 -.25371146 -1.587296
    549 -.28956395 -.543177
    550 -.3027096 -.6195347
    551 .18192355 -1.3518226
    552 .5866581 -.20997605
    553 .7776828 .4266921
    554 .4374009 1.2580547
    555 .3468046 .6952513
    556 .13170543 .8024296
    557 .4168928 .7450286
    558 .7971303 .29398882
    559 1.215296 .16710547
    560 1.3439866 .3271948
    561 .9520065 .34591845
    562 1.0260141 .2320076
    563 1.239202 .7131925
    564 .9691528 1.1234146
    565 .9287351 1.0530885
    566 .7419706 1.0005457
    567 1.3036896 1.568058
    end
    format %tmCYN Jahr_Monat
    [/CODE]

    my code for to generate the graph:

    twoway line SENT SENT_ORT Jahr_Monat, ytitle("XX") xtitle("") title("Test Index")

    Click image for larger version

Name:	test.png
Views:	1
Size:	129.4 KB
ID:	1532932


    I have tried the command:
    xmlabel(199812 200312 200812 201312 201812)

    but nothing in the graph has changed.

    What needs to be changed to achieve the desired outcome (on the x-axis)?

    Thanks in advance!

  • #2
    You got what you asked for: the format assigned to your date variable is what is being used for the graph. Your new option specifies monthly dates many years into the future, which I guess is why it’s being ignored.

    What do you want to see?

    Type

    Code:
    display ym(1999, 1)
    to get the monthly date for January 1999.
    Last edited by Nick Cox; 21 Jan 2020, 14:41.

    Comment


    • #3
      First of all, thank you very much for replying!

      However, I don't understand why in the graph the labels of the x-axis are randomly chosen, e.g. my data ends in 2018, and does not go into 2020? I imagined to label the x-axis with my desired dates.


      Comment


      • #4
        Nothing is randomly chosen. The intervals are 60 months apart and chosen to be nice so far as possible, meaning simply defined and equal. You can chose different label positions to override the default choice.

        Comment


        • #5
          Here's another way to do it:
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(Jahr_Monat SENT SENT_ORT)
          468 . .
          469 -.9883735 .17571376
          470 -.6953934 .3340038
          471 -.4804287 .08225542
          472 -.2135436 -.1733986
          473 -.40797755 -.6598344
          474 -.20301445 -.33490825
          475 -.087966 -.24500842
          476 .40384895 -.1362675
          477 .6014041 .4293425
          478 .50686157 .5251744
          479 .4979403 1.0525099
          480 .54874825 1.7644436
          481 .6212594 1.5954952
          482 .7128802 1.928487
          483 .7200878 1.0643696
          484 .6987402 1.478783
          485 1.0160284 1.3999777
          486 1.122921 1.5570377
          487 .8487988 1.3295542
          488 1.0435077 1.332692
          489 .7587039 1.1980356
          490 .7933701 .9344496
          491 .7376158 .8985572
          492 .8905743 1.2440665
          493 .7886781 1.5811487
          494 .50602764 1.449081
          495 .4108059 .4331162
          496 .15761256 .3449513
          497 -.07436032 .42745325
          498 -.48415455 .02644596
          499 -.465555 .13633555
          500 -1.531624 .8986828
          501 -2.3136506 -.48848554
          502 -2.388872 -1.2838695
          503 -2.0963929 -2.1475174
          504 -.52402335 -1.2027028
          505 -.473429 -.013739068
          506 -.3525708 .3096239
          507 -.70109 -.14804712
          508 -.26204827 -.05046533
          509 -.9030384 .36876205
          510 -2.547417 .14716044
          511 -2.2635396 -.10656934
          512 -2.3641849 -.4875911
          513 -2.3782527 -.9783503
          514 -1.9338255 -1.6837498
          515 -1.5755464 -1.3207465
          516 -1.3992268 -1.2115955
          517 -1.8999293 .7026442
          518 -2.4891555 .8523473
          519 -2.3744946 -.38552395
          520 -2.2098827 -.6340695
          521 -2.4586594 -1.2069182
          522 -1.8027 -1.4833723
          523 -.9671379 -1.3858428
          524 -.8685479 -1.400233
          525 -.7697501 -.604431
          526 -1.0154068 -.6951205
          527 -.15059333 -1.3649715
          528 -.14633152 -.0792993
          529 .01277743 .56845385
          530 -.3973399 .5889692
          531 .2277742 .0040735425
          532 -.07434668 .28324583
          533 -.026934003 .555994
          534 .2985708 .034490287
          535 -.02677937 -.24436995
          536 .27823198 -.3821239
          537 .05901428 -.5832725
          538 .14441074 -.7316566
          539 .3231294 -.59394413
          540 .3038368 -.072538145
          541 -.08542635 -.24338
          542 -.18277746 -.3218683
          543 -.4696557 -.1813648
          544 -.3480627 -.7211398
          545 .0018222064 -.8706591
          546 .12823159 -1.5658035
          547 -.18932045 -1.4131793
          548 -.25371146 -1.587296
          549 -.28956395 -.543177
          550 -.3027096 -.6195347
          551 .18192355 -1.3518226
          552 .5866581 -.20997605
          553 .7776828 .4266921
          554 .4374009 1.2580547
          555 .3468046 .6952513
          556 .13170543 .8024296
          557 .4168928 .7450286
          558 .7971303 .29398882
          559 1.215296 .16710547
          560 1.3439866 .3271948
          561 .9520065 .34591845
          562 1.0260141 .2320076
          563 1.239202 .7131925
          564 .9691528 1.1234146
          565 .9287351 1.0530885
          566 .7419706 1.0005457
          567 1.3036896 1.568058
          end
          format %tmCYN Jahr_Monat
          
          twoway line SENT SENT_ORT Jahr_Monat, ytitle("XX") xtitle("") title("Test Index")
          
          local start1 = ym(1999,1)  
          local end1 = ym(2008,1)  
          local start2 = ym(1999,7)  
          local end2 = ym(2007,7) 
          mac li 
          
          twoway line SENT SENT_ORT Jahr_Monat, ytitle("XX") xtitle("") title("Test Index") ///
          xticks(`start1'(12)`end1', tlength(*3)) xla(`start2'(12)`end2', noticks format(%tmCY))

          Click image for larger version

Name:	betterlabels.png
Views:	1
Size:	54.6 KB
ID:	1532987


          See also https://www.stata-journal.com/articl...article=gr0030

          Comment


          • #6
            Thank you very much, with the help of that code I managed to make it look the way I wanted

            Comment

            Working...
            X