Hello, Suppose I simulate 20 observations from the Poisson distribution with lambda = 4. I can summarize these values using table() and then feed that to barplot() for a graph. Problem: if there are empty categories (e.g. 0) or empty categories within the range of the data (e.g. observations for 6, 7, 9), barplot() does not include the empty cells in the x-axis of the plot. Is there any way to specify table() to have specific categories (in the above example, probably 0:12) so that zeroes are included? Thanks, Jason
On Wed, 2006-04-19 at 09:21 -0400, Owen, Jason wrote:> Hello, > > Suppose I simulate 20 observations from the Poisson distribution > with lambda = 4. I can summarize these values using table() and > then feed that to barplot() for a graph. > > Problem: if there are empty categories (e.g. 0) or empty categories > within the range of the data (e.g. observations for 6, 7, 9), barplot() > does not include the empty cells in the x-axis of the plot. Is there > any way to specify table() to have specific categories (in the above > example, probably 0:12) so that zeroes are included? > > Thanks, > > JasonOne thought comes to mind, which is based upon table()'s internal behavior, where it interprets the vectors passed as a factor, for the purpose of the [cross-]tabulation. Thus:> x <- rpois(20, 4)> x[1] 4 4 3 8 2 4 5 2 3 2 4 5 5 5 6 4 5 8 2 5> table(x)x 2 3 4 5 6 8 4 2 5 6 1 2 # Add the desired factor levels> table(factor(x, levels = 0:12))0 1 2 3 4 5 6 7 8 9 10 11 12 0 0 4 2 5 6 1 0 2 0 0 0 0 For the barplot: barplot(table(factor(x, levels = 0:12))) HTH, Marc Schwartz
Hi tabulate() approximates your desired functionality: > tabulate(c(1,1,1,4,4,4,8,8)) [1] 3 0 0 3 0 0 0 2 > (although this works with integer-valued vectors only; it excludes zero so you might be better to use tabulate(x+1) to catch this) HTH Robin On 19 Apr 2006, at 14:21, Owen, Jason wrote:> Hello, > > Suppose I simulate 20 observations from the Poisson distribution > with lambda = 4. I can summarize these values using table() and > then feed that to barplot() for a graph. > > Problem: if there are empty categories (e.g. 0) or empty categories > within the range of the data (e.g. observations for 6, 7, 9), > barplot() > does not include the empty cells in the x-axis of the plot. Is there > any way to specify table() to have specific categories (in the above > example, probably 0:12) so that zeroes are included? > > Thanks, > > Jason > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting- > guide.html-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
One can use the fact that converting a zoo object to a ts object fills in the holes with NAs. First create some test data, x, and create the table, tab. Then create a zoo object and convert that to a ts object. Now barplot the ts values using the times as names. set.seed(1) x <- rpois(20, 4) # test data library(zoo) tab <- table(x) tt <- as.ts(zoo(as.vector(tab), as.numeric(names(tab)))) barplot(coredata(tt), names = time(tt)) On 4/19/06, Owen, Jason <wowen at richmond.edu> wrote:> Hello, > > Suppose I simulate 20 observations from the Poisson distribution > with lambda = 4. I can summarize these values using table() and > then feed that to barplot() for a graph. > > Problem: if there are empty categories (e.g. 0) or empty categories > within the range of the data (e.g. observations for 6, 7, 9), barplot() > does not include the empty cells in the x-axis of the plot. Is there > any way to specify table() to have specific categories (in the above > example, probably 0:12) so that zeroes are included? > > Thanks, > > Jason > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Le 19.04.2006 15:21, Owen, Jason a ?crit :> Hello, > > Suppose I simulate 20 observations from the Poisson distribution > with lambda = 4. I can summarize these values using table() and > then feed that to barplot() for a graph. > > Problem: if there are empty categories (e.g. 0) or empty categories > within the range of the data (e.g. observations for 6, 7, 9), barplot() > does not include the empty cells in the x-axis of the plot. Is there > any way to specify table() to have specific categories (in the above > example, probably 0:12) so that zeroes are included? > > Thanks, > > Jason >Hi, What about using ?factor with its levels argument : x <- rpois(30, lambda=4) table(factor(x,levels=0:12)) Romain -- visit the R Graph Gallery : http://addictedtor.free.fr/graphiques mixmod 1.7 is released : http://www-math.univ-fcomte.fr/mixmod/index.php +---------------------------------------------------------------+ | Romain FRANCOIS - http://francoisromain.free.fr | | Doctorant INRIA Futurs / EDF | +---------------------------------------------------------------+