[Stata] - String values on axis of graph

#1
Hi there,

I would like to create a graph with the value of a string variable in the x axis.

Right now I can do

line price id

and I get a graph of price against id (which is a numerical variable). However, I would like to have the value of a string variable on the x-axis so that I can have, for example, price against a variable {a b c d e f ...}

Any help please...
 
#4
So you do want to plot price against id but you want to label the x-axis with a label associated to id? If this is what you want to do, then you should:

1/ define the label for id. If the label is in a string variable you can store the different values using "levelsof". Then you just have to use "label define" and "label val"

2/ use the options for xlabel. Smtg like this: "line price id, xlabel(..., valuelabel)

Or maybe I still don't understand what you want to do.
 
#5
Yes, that is what I want to do. Thanks for your help but I still don't understand how this is going to work.

I have about 100 000 observations on the line graph and I need to have some words like "one", "two", ..., "ten" (for example) on the horizontal axis (there are far fewer words than id numbers).

I know I can use xlabel to label every 10 000th tick with a number but I need to have a word instead.
 
#6
Then you can label only some values of your variable id. For instance:

label define example 1 "One" 100 "Two" etc. 100000 "Ten"
label val id example

and then line price id, xlabel(1 100 ... 100000, valuelabel)
 
#8
For future reference, I suggest downloading Nick Cox's command suite -labutil-

The command you need is "labmask" (type "findit labmask" into Stata and it'll find it).

The command assigns the string variable as the label for the numerical variable.

For example, I had a list of towns ('town') as a string variable, and a corresponding rank ('rank') which is numeric. To assign the town names as values, so that say 1=London, I just use

labmask rank, values(town)

Saves me doing lab define town 1 "London"... etc.

Really useful.