Re: [R] juxtaposed and stacked bars in one barplot?

2008-08-28 Thread hadley wickham
On Thu, Aug 28, 2008 at 9:31 AM, Stefan Uhmann
<[EMAIL PROTECTED]> wrote:
> hadley wickham schrieb:
>>
>> Hi Stefan,
>>
>> Could you be a bit more explicit?  Do you have an example dataset that
>> you are trying to visualise?
>>
>
> Right, thanks for pointing out the obvious.
>
> So here's my code:
>
>>>
> library(gplots)
>
> quarter <- as.factor(sample(c("Q1", "Q2", "Q3", "Q4"),
>100, replace = TRUE))
> year <- as.factor(sample(c(seq(from=2000, to=2008)), 100, replace = TRUE))
> category <- as.factor(sample(c(seq(from=1, to=4)), 100, replace = TRUE))
> test <- data.frame(quarter, year, category)
> table(test$category, test$quarter, test$year)
> barplot2(table(test$quarter, test$year),
>beside=T, ylim=c(0,10), main="how to include dim3?")
> # inclusion of 3rd dimension does not work:
> barplot2(table(test$quarter, test$year, test$category),
>beside=T, ylim=c(0,10), main="how to include dim3?")
> <<
>
> I want the barplot to be exactly the same but with the bars stacked (by
> 'category'). I got the message from Gaspar, but have not yet tried to fit
> his example to my data.

Hi Stefan,

For your data, I'd suggest you consider using lines instead of bars.
http://peltiertech.com/WordPress/2008/08/27/stacked-vs-clustered/ has
some good reasons why. - but basically stacking makes it difficult to
see how each group changes over time.  It's pretty easy to play around
different variations with ggplot2, so I've included a few you might
want to look at.  (Including your original request right at the
bottom)

test <- data.frame(quarter, year, category)
tabdf <- as.data.frame(with(test, table(category, quarter, year)))

install.packages("ggplot2")
qplot(year, Freq, data=tabdf, geom="line", colour = category, facets =
 quarter ~ . , group = interaction(category, quarter))
# OR
qplot(year, Freq, data=tabdf, geom="line", colour = quarter, facets =
category ~ . , group = interaction(category, quarter))

# If you _really_ want stacking:

qplot(year, Freq, data=tabdf, geom="area", fill = quarter, facets =
category ~ . , group = interaction(category, quarter))

# OR

qplot(year, Freq, data=tabdf, geom="bar", stat="identity", fill =
quarter, facets =  category ~ .)

# OR finally, like what you originally asked for

qplot(quarter, Freq, data=tabdf, geom="bar", stat="identity", fill =
category, facets = . ~ year)


Regards,

Hadley

-- 
http://had.co.nz/

__
R-help@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.


Re: [R] juxtaposed and stacked bars in one barplot?

2008-08-28 Thread Stefan Uhmann

hadley wickham schrieb:

Hi Stefan,

Could you be a bit more explicit?  Do you have an example dataset that
you are trying to visualise?



Right, thanks for pointing out the obvious.

So here's my code:

>>
library(gplots)

quarter <- as.factor(sample(c("Q1", "Q2", "Q3", "Q4"),
100, replace = TRUE))
year <- as.factor(sample(c(seq(from=2000, to=2008)), 100, replace = TRUE))
category <- as.factor(sample(c(seq(from=1, to=4)), 100, replace = TRUE))
test <- data.frame(quarter, year, category)
table(test$category, test$quarter, test$year)
barplot2(table(test$quarter, test$year),
beside=T, ylim=c(0,10), main="how to include dim3?")
# inclusion of 3rd dimension does not work:
barplot2(table(test$quarter, test$year, test$category),
beside=T, ylim=c(0,10), main="how to include dim3?")
<<

I want the barplot to be exactly the same but with the bars stacked (by 
'category'). I got the message from Gaspar, but have not yet tried to 
fit his example to my data.


/Stefan

__
R-help@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.


Re: [R] juxtaposed and stacked bars in one barplot?

2008-08-27 Thread Gasper Cankar
You can simply do

> a <- c(2,4,3,4,5)
> b <- c(4,3,4,5,2)

> barplot(cbind(a,b))

or you can put a and b in matrix as help suggests.

Gasper Cankar

-Original Message-
From: Stefan Uhmann [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 27, 2008 11:39 AM
To: r-help@r-project.org
Subject: [R] juxtaposed and stacked bars in one barplot?

Hi,

anybody any hints how to get a barplot with both juxtaposed and stacked
bars?

/Stefan

__
R-help@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.

__
R-help@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.


Re: [R] juxtaposed and stacked bars in one barplot?

2008-08-27 Thread hadley wickham
Hi Stefan,

Could you be a bit more explicit?  Do you have an example dataset that
you are trying to visualise?

Hadley

On Wed, Aug 27, 2008 at 4:38 AM, Stefan Uhmann
<[EMAIL PROTECTED]> wrote:
> Hi,
>
> anybody any hints how to get a barplot with both juxtaposed and stacked
> bars?
>
> /Stefan
>
> __
> R-help@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.
>



-- 
http://had.co.nz/

__
R-help@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.