Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: AW: RE: RE: AW: RE: display all categories on pie chart for categorical variables (with some zero values)


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: AW: RE: RE: AW: RE: display all categories on pie chart for categorical variables (with some zero values)
Date   Fri, 27 Nov 2009 13:58:11 -0000

The difference is this: A pie chart of one variable will be drawn
without any reference to whatever categories exist in any other
variable. If the data are -reshape-d so that several variables are
stored as one, that no longer bites.

The trick that starts with -gen one = 1- is essentially the same trick
as is used by -catplot- (SSC), except that -catplot- adds handles for
percents etc. I haven't tried -catplot- with -allcategories- but I don't
foresee any conflict. 

Nick 
n.j.cox@durham.ac.uk 

Martin Weiss

" I don't quite understand the theory behind it, but it works like a
treat."

I am glad it works for you :-) Nick`s solution exploits -reshape- which
is
at the heart of most elegant solutions for data management in Stata, and
the
-by- mechanism. My solution temporarily created different datasets, drew
the
graph and so on. Later on, I -graph combine-d the graphs into one big
picture. If you are happy with the result of Nick`s procedure, it is
certainly the way to go and easier to reverse engineer in case you want
to
:-)

Adam Collins

Thank you so much Martin and Nick!!!  This is a fantastic solution!  I
don't
quite understand the theory behind it, but it works like a treat.

I notice that now, to display just one of my categorical variables on
it's
own (but with all the categories listed in the legend), it is as simple
as:

graph pie plotvar if _j==1, over(c) allcategories

or

graph pie plotvar if _j==2, over(c) allcategories


Even more excitingly, I see that the same thing can now be applied to
bar
graphs, and that it solves the problem of displaying all the categories
in
the "correct" order on the bar graph - even if some of them containing
zero
occurrences:

gen one=1
graph hbar (sum) one if _j==2, over(c) allcategories

Nick Cox

This is the code to get Martin's graphs from Martin's data, but using
-reshape- instead. 

clear

inp byte(a b)
2 3
4 5
7 2
3 6
9	10
end

gen plotvar=abs(rnormal())

gen id = _n 

rename a c1
rename b c2

reshape long c, i(id)
label define _j 1 "a" 2 "b"
label val _j _j 

graph pie plotvar, over(c) by(_j)

Nick 
n.j.cox@durham.ac.uk 

Martin Weiss

This at least gives you all the categories:

*************
clear*

inp byte(a b)
2 3 
4 5
7 2
3 6
9	10
end

gen plotvar=abs(rnormal())

compress
list, noo

preserve
	tempfile mytemp
 //get unique values of a and b jointly
	stack a b, into(c) clear
	duplicates drop c, force
	drop _stack
	save `mytemp', replace
restore

//for a
preserve
 keep a plotvar
 rename a c
 l
 merge 1:1 c using "`mytemp'"
 replace plotvar=0 if mi(plotvar)
 graph pie plotvar, over(c) missing  /* 
	*/ name(mygra, replace) tit(for a) /* 
  */ nodraw
restore

//for b
preserve
 keep b plotvar
 rename b c
 l
 merge 1:1 c using "`mytemp'"
 replace plotvar=0 if mi(plotvar)
 graph pie plotvar, over(c) missing  /* 
	*/ name(mygrb, replace) tit(for b) /* 
	*/ nodraw
restore

graph combine mygra mygrb
*************

Adam Collins

I expect there is something really simple that I am missing here... but
I am
new to Stata and it is doing my head in that I can't display all
categories
of a categorical variable on a pie chart...  I am really getting no
where
with this problem.  I would really appreciate some help.

I tried exploring ways of storing the "counts" from my categorical
variable
values in another variable, such as a matrix which would allow me to
store a
"zero count" as a "0" value.  In searching for a solution, I came
accross
-fulltab- (from SSC), however it doesn't seem to work (it just displays
the
following "program error:  code follows on the same line as open
brace").  

Anyway, I am not even sure this is the best approach.  Surely there is
something more straight forward?



-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Adam Collins
Sent: mercredi 25 novembre 2009 17:39
To: statalist@hsphsun2.harvard.edu
Subject: st: display all categories on pie chart for categorical
variables
(with some zero values)

Hello, 

I have several categorical variables that each possess the same list of
categories (value labels).  

To enable easy comparison of the pie charts, I would like every category
to
exist on every chart (so that each category retains the same
colour/settings/legend position etc on each chart).

The problem is that some of the variables contain zero occurrences of
certain values (categories) and the standard 'over(varname)' option with
-graph pie- excludes categories with no occurrences.  I have tried the
'allcategories' option, but since it applies to the 'over()' variable,
it
does not help.

Does anyone have any ideas?  (by the way, since this is for a specific
set
of categorical variables, it is fine if the categories are 'hard coded',
then applied to each variable)


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index