[R] barplots of several variables with different number of categories

ilai keren at math.montana.edu
Thu Mar 1 17:26:28 CET 2012


Jon,
You could create new variables with the combined levels just for the
purpose of plotting.
Assume I have data.frame bpt
str(bpt)
'data.frame':	12 obs. of  2 variables:
 $ V1: Factor w/ 3 levels "low","med","high": 1 1 1 1 2 2 2 2 3 3 ...
 $ V2: Factor w/ 6 levels "1","2","3","4",..: 1 1 2 2 3 3 4 4 5 5 ...

 bpt$V3 <- factor(bpt$V1,c(levels(bpt$V1),levels(bpt$V2)))
 bpt$V4 <- factor(bpt$V2,c(levels(bpt$V1),levels(bpt$V2)))

 with(bpt,barplot(cbind(table(V3),table(V4))))

Hope this helps

Elai

On Thu, Mar 1, 2012 at 7:19 AM, jon waterhouse <jonwaterhouse at gov.nl.ca> wrote:
> If I have two factors, v1 and v2 and I want to have a stacked bar graph of
> the two variables side by side I could do
>
> barplot(cbind(table(v1),table(v2)))
>
> if v1 and v2 have the same number of categories.
>
> If they don't have the same number of categories this won't work.
>
> I'm sure there's a simple solution?
>
> Thanks,
>
> Jon
>
> --
> View this message in context: http://r.789695.n4.nabble.com/barplots-of-several-variables-with-different-number-of-categories-tp4435092p4435092.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list