Announcement

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

  • change x-axis values

    Hey folks,

    I am quiet new to stata and want to change my x-axis values (encircled). Now I have 0-40, but I want 01/2019-04/2022 since this is the time period of my data sets obervations.

    Click image for larger version

Name:	Bildschirmfoto 2022-10-20 um 08.14.49.png
Views:	1
Size:	167.9 KB
ID:	1686068
    This is my do-file so far
    Code:
    ***ANALYSIS HUBI THESIS***
    
    *Data import*
    
    
    import excel "/Users/nicolasstappen/Desktop/Nico/Studium/Master/5. Semester/Masterarbeit/Daten/all Data in one file nur ab 2019.xlsx", sheet("Trade values") firstrow case(lower)
    
    codebook // entweder nur codebook oder codebook varname
    
    
    
    drop classification // no information and/or variation
    drop perioddesc // no info that is not within Period already
    drop reportercode // no information and/or variation
    drop partnercode // no added information
    drop tradeflowcode // no added information
    
    replace tradeflow="exports" if tradeflow=="Exports"
    replace tradeflow="imports" if tradeflow=="Imports"
    replace tradeflow="reimports" if tradeflow=="Re-imports"
    
    replace reporter="united kingdom" if reporter=="United Kingdom"
    
    replace partner = lower(partner)
    
    gen period_new=.
    
    xtset tradevalueus
    
    replace period_new=1 if period==201901
    replace period_new=2 if period==201902
    replace period_new=3 if period==201903
    replace period_new=4 if period==201904
    replace period_new=5 if period==201905
    replace period_new=6 if period==201906
    replace period_new=7 if period==201907
    replace period_new=8 if period==201908
    replace period_new=9 if period==201909
    replace period_new=10 if period==201910
    replace period_new=11 if period==201911
    replace period_new=12 if period==201912
    replace period_new=13 if period==202001
    replace period_new=14 if period==202002
    replace period_new=15 if period==202003
    replace period_new=16 if period==202004
    replace period_new=17 if period==202005
    replace period_new=18 if period==202006
    replace period_new=19 if period==202007
    replace period_new=20 if period==202008
    replace period_new=21 if period==202009
    replace period_new=22 if period==202010
    replace period_new=23 if period==202011
    replace period_new=24 if period==202012
    replace period_new=25 if period==202101
    replace period_new=26 if period==202102
    replace period_new=27 if period==202103
    replace period_new=28 if period==202104
    replace period_new=29 if period==202105
    replace period_new=30 if period==202106
    replace period_new=31 if period==202107
    replace period_new=32 if period==202108
    replace period_new=33 if period==202109
    replace period_new=34 if period==202110
    replace period_new=35 if period==202111
    replace period_new=36 if period==202112
    replace period_new=37 if period==202201
    replace period_new=38 if period==202202
    replace period_new=39 if period==202203
    replace period_new=40 if period==202204
    
    sort partner
    sort period_new
    
    format %100.0g tradevalueus // um e's wegzubekommen
    
    graph twoway line tradevalueus period_new if tradeflow=="exports" & partner=="belgium", c(L) //Graph
    I think it might be only one command and i tried xscale, but for whatever reason it doesn't work.

    Thanks in advance
    Nico

  • #2
    The harder part of the solution has already been explained at length in your previous thread. https://www.statalist.org/forums/for...he-stata-graph

    You need a variable that is the monthly date in Stata terms. Your period variable is not fit for purpose as it jumps by 1 for 11 months of the year and by 89 in January.

    Code:
    gen mdate = ym(floor(period/100), mod(period, 100)) 
    format mdate %tmNN/CCYY
    Now you can plot in terms of that variable. 40 labels or even 40 ticks are likely to be too busy but xlabel() allows other choices.

    Comment


    • #3
      I'm sorry I dont get it.

      This is my do-file for now:

      Code:
      ***ANALYSIS HUBI THESIS***
      
      *Data import*
      
      
      import excel "/Users/nicolasstappen/Desktop/Nico/Studium/Master/5. Semester/Masterarbeit/Daten/all Data in one file nur ab 2019.xlsx", sheet("Trade values") firstrow case(lower)
      
      codebook // entweder nur codebook oder codebook varname
      
      drop classification // no information and/or variation
      drop perioddesc // no info that is not within Period already
      drop reportercode // no information and/or variation
      drop partnercode // no added information
      drop tradeflowcode // no added information
      
      replace tradeflow="exports" if tradeflow=="Exports"
      replace tradeflow="imports" if tradeflow=="Imports"
      replace tradeflow="reimports" if tradeflow=="Re-imports"
      
      replace reporter="united kingdom" if reporter=="United Kingdom"
      
      replace partner = lower(partner)
      
      gen period_new=.
      
      xtset tradevalueus
      
      gen mdate = ym(floor(period_new/100), mod(period_new, 100)) 
      format mdate %tmNN/CCYY
      
      sort partner
      sort period_new
      
      format %100.0g tradevalueus // um e's wegzubekommen
      
      graph twoway line tradevalueus period_new if tradeflow=="exports" & partner=="belgium", c(L) //Graph
      then the graph is empty

      Iguess I need to adjust this command
      Code:
      gen mdate = ym(floor(period_new/100), mod(period_new, 100)) 
      format mdate %tmNN/CCYY
      to my dataset. I've replaced period with period_new, but the second command I need to adjust, don't I?

      Thanks again for your help
      Click image for larger version

Name:	Bildschirmfoto 2022-10-20 um 15.50.46.png
Views:	1
Size:	433.3 KB
ID:	1686078

      Comment


      • #4
        You just need to specify mdate as the x axis variable for your plots.

        Creating
        mdate as a new variable has absolutely no effect on the existing variable period_new.

        You seem very confused about basic principles. That is understandable in a new user, but evidently you have been struggling with this for some weeks. I suggest trying to find somebody who can help locally, for example someone at your university if you are a student or an academic. It may also help to work through a basic introduction to Stata. Sometimes, and perhaps it is true here, someone has a lot of code but it is mostly copied from somewhere else and you are guessing at what is needed for your own project without understanding anything much. That is not a good strategy.


        Last edited by Nick Cox; 20 Oct 2022, 08:45.

        Comment


        • #5
          Dear all,

          Apologies for reviving this thread.

          In #1, with the exact same data as Nico Stappen, suppose one wants to replace the value labels of "period_new" on the x-axis (0,10,20,30,40) with say string names, which would act as value labels (e.g. just to illustrate 0 corresponds to e.g. "COVID19 1st wave", 10 to "COVID19 second wave", etc.), how would one go about doing this?

          If another thread discusses this question already, I'm very sorry! I haven't found any so far.

          Comment


          • #6

            Code:
            xla(0 "whatever" 10 "you want" 20 "but don't try very long strings as that won't work well")
            If your example is close to what you really want then I would do this by added text, not axis labels.

            Comment


            • #7
              Worked a wonder, thank you!

              Comment

              Working...
              X