Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Making a bar graph from the row of a matrix

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: Making a bar graph from the row of a matrix Date Mon, 8 Apr 2013 18:56:58 +0100

```Your runnable code made it easier to see what you are doing; thanks.
Presumably this is just an example we can all play with.

-svmat- is a rather ugly command; the code here avoids it and adds a
couple of small tricks.

More importantly, I suggest considering a -graph dot- command. With
more than a few predictors, which you may have in your real
application(s), it is likely to be easier to read the resulting graph.

clear all
sysuse auto.dta
qui reg price mpg trunk weight
matrix test = e(V)
matrix rowtest = test["weight", 1...]
/* rowtest is the row I want to graph     */
gen var_colnames = ""
gen trans_rowtest = .
tokenize "`: colnames rowtest'"
qui forval i = 1/`= colsof(rowtest)' {
replace trans_rowtest = rowtest[1, `i'] in `i'
replace var_colnames = "``i''" in `i'
}
graph bar (asis) trans_rowtest, over(var_colnames)
graph dot (asis)trans_rowtest, over(var_colnames) ///
yli(0, lc(gs12) lw(vthin)) ytitle("coefficient")

Nick
njcoxstata@gmail.com

On 8 April 2013 16:49, Mauricio Esteban Cuak <cuak2000@gmail.com> wrote:
> Hi Nick, thanks for the answer.
> The following code plots the graph I wanted and may be useful for other people.
> It doesn't look very nice however.
> As suggested by Nick, I moved the names into a variable (slightly
> tricky) and the values into another.
>
>
> /* Begin CODE */
>
> clear all
>
> sysuse auto.dta
>
> qui reg price mpg trunk weight
>
> matrix test = e(V)
>
>
> matrix rowtest = test["weight", 1...]
>
> /* rowtest is the row I want to graph     */
>
>
> local names : colnames rowtest
>
> local n_names : word count `names'
>
>
> gen var_colnames = ""
>
> forval i=1/`n_names' {
>
> local this_name = "`:word `i' of `names''"
>
> replace var_colnames = "`this_name'" if _n == `i'
>
> }
>
> matrix trans_rowtest = rowtest'
>
> svmat trans_rowtest
>
> graph bar trans_rowtest1, over(var_colnames)
>
>
> **************************************
>
>
>
> 2013/4/7 Mauricio Esteban Cuak <cuak2000@gmail.com>
>>
>> Hi Nick, thanks for the answer.
>> The following code plots the graph I wanted and may be useful for other people.
>> It doesn't look very nice however.
>> As suggested by Nick, I moved the names into a variable (slightly tricky) and the values into another.
>>
>>
>> **************BEGIN CODE********************
>>
>> clear all
>>
>> sysuse auto.dta
>>
>> qui reg price mpg trunk weight
>>
>> matrix test = e(V)
>>
>>
>> matrix rowtest = test["weight", 1...]
>>
>> /* rowtest is the row I want to graph     */
>>
>>
>> local names : colnames rowtest
>>
>> local n_names : word count `names'
>>
>>
>> gen var_colnames = ""
>>
>> forval i=1/`n_names' {
>>
>> local this_name = "`:word `i' of `names''"
>>
>> replace var_colnames = "`this_name'" if _n == `i'
>>
>> }
>>
>> matrix trans_rowtest = rowtest'
>>
>> svmat trans_rowtest
>>
>> graph bar trans_rowtest1, over(var_colnames)
>>
>>
>>
>>
>>
>>
>> 2013/4/3 Nick Cox <njcoxstata@gmail.com>
>>>
>>> The easiest way to do this is probably to copy values from your matrix
>>> to a variable and then call up -twoway bar-.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```