[R] Can 'mosaic' be used with a continuous variable?

Achim Zeileis Achim.Zeileis at uibk.ac.at
Thu Sep 15 08:36:47 CEST 2011


On Wed, 14 Sep 2011, Mauricio Cornejo wrote:

> Thanks Z.
> 
> Let me clarify my problem a bit further ... as I don't think I could use
> cut() or spine() to solve it.
> 
> I have a data frame with three columns (A, B, Value).  'A' and 'B' are
> categorical and 'Value' is continuous and non-negative.  I'm looking to make
> a mosaic plot with A on one axis and B on the other.  The size (i.e. area)
> of the tiles would correspond to the proportion of the total Value
> represented by the given combinations of the levels of A and B.

OK, that's easy. Just create the corresponding contingency table which has 
non-integer cell entries and then call mosaic().

Consider this simple artificial example:

## artificial data
set.seed(1)
d <- data.frame(A = rep(1:2, each = 4), B = rep(1:2, 4), Value = runif(8))
d

## corresponding table
tab <- xtabs(Value ~ A + B, data = d)

## mosaic display
mosaic(tab)

hth,
Z

> Thanks again,
> Mauricio
> 
> ____________________________________________________________________________
> From: Achim Zeileis <Achim.Zeileis at uibk.ac.at>
> To: Mauricio Cornejo <mauriciocornejo at yahoo.com>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Wednesday, September 14, 2011 1:45 PM
> Subject: Re: [R] Can 'mosaic' be used with a continuous variable?
> 
> On Wed, 14 Sep 2011, Mauricio Cornejo wrote:
> 
> > Hello,
> >
> > I'm wondering if the 'mosaic' plot of the vcd package (or any other
> function for that matter) can be used with a continuous variable that should
> be represented via various categorical variables.? All the documentation
> I've read lead me to believe that it only works with counts of categories.
> >
> > Alternatively, I've thought of first creating a contingency table where
> the frequencies would really be the values of my continuous variable as
> opposed to counts ... but I don't know how to do that.
> 
> Well, you can always use cut() to create a categorical variable from a
> continuous variable. But you have to do that in advance before calling
> mosaic().
> 
> For a bivariate display with a continuous x and a categorical y, you can use
> spine() which does the categorization for you.
> 
> hth,
> Z
> 
> > Any insight would be appreciated.
> >
> > Thanks,
> > Mauricio
> >
> >     [[alternative HTML version deleted]]
> >
> >
> 
> 
> 
>


More information about the R-help mailing list