Announcement

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

  • Writing string variable to x-axis while actually plotting numeric variable

    I have this data.

    var1 var2 var3
    apple 1 4
    banana 2 6
    strawberry 3 9


    To draw a graph, I do
    Code:
    twoway line var3 var2
    But I want to write var1 into the x-axis like this. How can I do this? I opened the image editor and tried various things but couldn't find how to do it.
    Click image for larger version

Name:	image example.png
Views:	1
Size:	10.9 KB
ID:	1565375

    Last edited by James Park; 25 Jul 2020, 21:34.

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 var1 float(var2 var3)
    "apple"      1 4
    "banana"     2 6
    "strawberry" 3 9
    end
    
    
    //  CREATE A VALUE LABEL FOR VAR2 FROM THE VALUES OF VAR1
    forvalues i = 1/`=_N' {
     label define lab2   `=var2[`i']'    "`=var1[`i']'", add
    }
    label values var2 lab2
    
    //  NOW GRAPH USING THE LABELS ON THE AXIS
    levelsof var2, local(var2values)
    graph twoway line var3 var2, xlabel(`var2values', valuelabels)
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Tweaking @Clyde Schechter's helpful answer: See also labmask from the Stata Journal, as documented here

      SJ-8-2 gr0034 . . . . . . . . . . Speaking Stata: Between tables and graphs
      (help labmask, seqvar if installed) . . . . . . . . . . . . N. J. Cox
      Q2/08 SJ 8(2):269--289
      outlines techniques for producing table-like graphs




      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str10 var1 float(var2 var3)
      "apple"      1 4
      "banana"     2 6
      "strawberry" 3 9
      end
      
      labmask var2, values(var1)
      
      graph twoway line var3 var2, xlabel(1/3, valuelabels) xsc(r(0.8 3.2))

      Comment


      • #4
        Thank you so much Clyde Schechter and Nick Cox. I will familiarize myself with dataex. It missed my mind.

        Combining the two solutions, this must be a concise and versatile solution.

        Code:
        clear
        input str10 var1 float(var2 var3)
        "apple"      1 4
        "banana"     2 6
        "strawberry" 7 9
        end
        
        labmask var2, values(var1)
        levelsof var2, local(var2values)
        graph twoway line var3 var2, xlabel(`var2values', valuelabels) xsc(r(0.8 8))

        Comment


        • #5
          That solution doesn't qualify as versatile, as it wires in 0.8 and 8. as suggested axis limits. If you wanted to write very generally, you would start with a calculation of minimum and maximum values for the x axis variable and add some fraction of the range either side to accommodate leftmost and rightmost axis labels.

          Comment

          Working...
          X