Histogram with xline in Stata

1k Views Asked by At

I want to create a histogram with the option xline but I want to tell Stata which xline value should be taken according to the year variable in my data set.

For example, in the data set below, I want the value 100 to be taken if I'm doing the histogram for the year 1990. (I'm doing lots of histograms like this so it is very inefficient to write the xline value manually.)

id    year     xline_value    
1     1990     100            
1     1991     200            
1     1992     300
1     1993     5000
1     1994     600
2     1990     100
2     1991     200
2     1992     300
3     1991     200
3     1992     300
3     1993     5000

forvalues t = 1950/2005{    
hist A if year==`t' , xline(?) 
graph export "A_`t'.jpg", replace
}

1

There are 1 best solutions below

1
Nick Cox On BEST ANSWER
forvalues t = 1950/2005 {    
    su xline_value if year == `t', meanonly 
    hist A if year==`t', xline(`r(min)') 
    graph export "A_`t'.jpg", replace
}

For other values, you would need something more complicated, say

forvalues t = 1950/2005 {    
    su xline_value if year == `t', meanonly 
    local show1 = r(min) 
    su xline_value2 if year == `t', meanonly 
    hist A if year==`t', xline(`show1' `r(min)') 
    graph export "A_`t'.jpg", replace
}