[R] Stacked Bar Plot With Two Dependent Variables

2013-09-18 Thread Carabiniero
Hi All, 

I need to construct a stacked bar plot with two independent (x) variables,
where the stacking is x1 and the x-axis label is x2. Can someone help out
with the code for this or provide a reference/example?

Thank you,
J






--
View this message in context: 
http://r.789695.n4.nabble.com/Stacked-Bar-Plot-With-Two-Dependent-Variables-tp4676402.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Stacked Bar Plot With Two Dependent Variables

2013-09-18 Thread Achim Zeileis

On Tue, 17 Sep 2013, Carabiniero wrote:


Hi All,

I need to construct a stacked bar plot with two independent (x) variables,
where the stacking is x1 and the x-axis label is x2. Can someone help out
with the code for this or provide a reference/example?


I'm not completely sure what exactly you are looking for but you might 
want to explore mosaic displays that generalize stacked barplot. For 
example you can do the following using the UCBAdmissions data. I treat 
Admit as the dependent and Gender and Dept as the explanatory 
variables:


## data and colors
ucb - aperm(UCBAdmissions, 3:1)
gr - gray.colors(2)[2:1]

## mosaic with alternating split direction
mosaicplot(ucb, col = gr, off = c(5, 3, 0))

## doubledecker style
mosaicplot(ucb, col = gr, dir = c(v, v, h), off = c(6, 4, 0))

## or using vcd package
library(vcd)
mosaic(~ Dept + Gender + Admit, data = UCBAdmissions,
  gp = gpar(fill = gr), spacing = spacing_highlighting)
doubledecker(Admit ~ Dept + Gender, data = UCBAdmissions)


Thank you,
J






--
View this message in context: 
http://r.789695.n4.nabble.com/Stacked-Bar-Plot-With-Two-Dependent-Variables-tp4676402.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


[R] Stacked Bar Plot in ggplot2

2011-07-19 Thread Abraham Mathew
I'm trying to develop a stacked bar plot in R with ggplot2.

My data:

conv = c(10, 4.76, 17.14, 25, 26.47, 37.5, 20.83, 25.53, 32.5, 16.7, 27.33)
click = c(20, 42, 35, 28, 34, 48, 48, 47, 40, 30, 30)
date = c(July 7, July 8, July 9, July 10, July 11, July 12,
July 13,
July 14, July 15, July 16, July 17)

dat - data.frame(date=c(date), click=c(click), conv=c(conv),
stringsAsFactors = FALSE)
dat


I'm trying to create a stacked bar plot with the values for Clicks in the
background and the values
for conversions in the forefront. I tried the following, but because the
values aren't factors,
it's doesn't produce the right result.

p3 = ggplot(dat, aes(as.character(date))) +
geom_bar(aes(fill=as.factor(conv))) + ylim(c(0,70)) +
geom_bar(aes(fill = conv), position = 'fill')
p3

Help!

[[alternative HTML version deleted]]

__
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] Stacked Bar Plot in ggplot2

2011-07-19 Thread Justin
Abraham Mathew abraham at thisorthat.com writes:

 
 I'm trying to develop a stacked bar plot in R with ggplot2.
 
 My data:
 
 conv = c(10, 4.76, 17.14, 25, 26.47, 37.5, 20.83, 25.53, 32.5, 16.7, 27.33)
 click = c(20, 42, 35, 28, 34, 48, 48, 47, 40, 30, 30)
 date = c(July 7, July 8, July 9, July 10, July 11, July 12,
 July 13,
 July 14, July 15, July 16, July 17)
 
 dat - data.frame(date=c(date), click=c(click), conv=c(conv),
 stringsAsFactors = FALSE)


Is: 
 
ggplot(dat,aes(x=date))+geom_bar(aes(y=click),fill='red')+geom_bar(aes(y=conv),fill='blue')

what you're looking for?

or 

dat.melt-melt(dat,'date')
ggplot(dat.melt,aes(x=date,y=value,fill=variable))+geom_bar()


Justin

__
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] Stacked bar plot of frequency vs time

2011-07-15 Thread marcel
Thank you for the solutions! 

I have the first one working and it does exactly what I am looking for.
Unfortunately I have to put the plot in a common figure alongside other
plots made in the basic environment (challenging!). With the second method,
I was unable to make the stacked bars locate to the appropriate positions
along the X axis (ie the appropriate time), which, though unconventional is
required for my figure. So I am still looking for a complete solution in the
basic plotting environment. 

I have boiled my problem down to this minimal example: 

# Made-up data
tC - textConnection(
Time Type1 Type2 Type3
1.3 .50 .25 .25
4.5 .55 .25 .20
5.2 .65 .20 .15
)

data1 - read.table(header=TRUE, tC)
data2 - data.frame(Time=rep(data1$Time, 3), stack(data1[,2:4]))
close.connection(tC)

# PLOT1 Scatterplot
attach(data1)
par(mar=c(1,1,1,1))
plot(Time, Type1, frame=T, ylab=Divergence,
col=rgb(0,100,0,50,maxColorValue=255), main=plot 1, xlim= c(0,6), ylim=
c(0, 1), axes=FALSE, xlab= )
detach(data1)

# PLOT2 barplot
require(lattice)
attach(data2)
barchart(values ~ Time, group=ind, data=data2, stack=TRUE, horizontal=FALSE,
main=not there yet)
plot2 - xyplot(values ~ Time, group=ind, data=data2, stack=TRUE,
horizontal=FALSE, panel=panel.barchart, ylim=c(-0.05,1.05), xlim=c(0,6),
main=Plot 2- how can I plot below plot1?)
print(plot2)
detach(data2)

The only thing left is to get both plots to be vertically aligned, one above
the other on the same figure. Is this possible? Thanks for all of your
thoughts.

Marcel

Marcel

--
View this message in context: 
http://r.789695.n4.nabble.com/Stacked-bar-plot-of-frequency-vs-time-tp3659715p3669311.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Stacked bar plot of frequency vs time

2011-07-11 Thread marcel
Hi All,
New to R, but committed. I looked in a number of places but can't figure out
my current problem. I have date of the type:

Time Type1  Type2  Type3
1.50   .25 .25
4.55   .25 .20
5.65   .20 .15
etc

which describe the frequency of types 1, 2 and 3 (adding up to 100%) over
time. I would like to create a stacked bar chart showing these frequencies,
each bar of height = 1, subsections of bars proportional to the frequency,
and each bar located at the correct X (time) position. One difficulty is
that the desired spacing of bar locations on the x-axis is irregular. Is
this possible in R?

Many thanks,
Marcel

--
View this message in context: 
http://r.789695.n4.nabble.com/Stacked-bar-plot-of-frequency-vs-time-tp3659715p3659715.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Stacked bar plot of frequency vs time

2011-07-11 Thread Richard M. Heiberger
Marcel,

tC - textConnection(
Time Type1  Type2  Type3
1.50   .25 .25
4.55   .25 .20
5.65   .20 .15
)
tmp - read.table(header=TRUE, tC)
close.connection(tC)

require(lattice)

tmpdf - data.frame(Time=rep(tmp$Time, 3), stack(tmp[,2:4]))
tmpdf
barchart(values ~ Time, group=ind, data=tmpdf, stack=TRUE, horizontal=FALSE,
 main=not there yet)
xyplot(values ~ Time, group=ind, data=tmpdf, stack=TRUE, horizontal=FALSE,
   panel=panel.barchart, ylim=c(-0.05,1.05), xlim=c(0,6),
   main=this does what you want)


Rich
On Mon, Jul 11, 2011 at 10:36 AM, marcel marcelcur...@gmail.com wrote:

 Hi All,
 New to R, but committed. I looked in a number of places but can't figure
 out
 my current problem. I have date of the type:

 Time Type1  Type2  Type3
 1.50   .25 .25
 4.55   .25 .20
 5.65   .20 .15
 etc

 which describe the frequency of types 1, 2 and 3 (adding up to 100%) over
 time. I would like to create a stacked bar chart showing these frequencies,
 each bar of height = 1, subsections of bars proportional to the frequency,
 and each bar located at the correct X (time) position. One difficulty is
 that the desired spacing of bar locations on the x-axis is irregular. Is
 this possible in R?

 Many thanks,
 Marcel

 --
 View this message in context:
 http://r.789695.n4.nabble.com/Stacked-bar-plot-of-frequency-vs-time-tp3659715p3659715.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.htmlhttp://www.r-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
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] Stacked bar plot of frequency vs time

2011-07-11 Thread Marc Schwartz
On Jul 11, 2011, at 9:36 AM, marcel wrote:

 Hi All,
 New to R, but committed. I looked in a number of places but can't figure out
 my current problem. I have date of the type:
 
 Time Type1  Type2  Type3
 1.50   .25 .25
 4.55   .25 .20
 5.65   .20 .15
 etc
 
 which describe the frequency of types 1, 2 and 3 (adding up to 100%) over
 time. I would like to create a stacked bar chart showing these frequencies,
 each bar of height = 1, subsections of bars proportional to the frequency,
 and each bar located at the correct X (time) position. One difficulty is
 that the desired spacing of bar locations on the x-axis is irregular. Is
 this possible in R?
 
 Many thanks,
 Marcel


In addition to Rich's solution using lattice, here is one possible approach 
using base graphics:


 dput(DF)
structure(list(Time = c(1L, 4L, 5L), Type1 = c(0.5, 0.55, 0.65
), Type2 = c(0.25, 0.25, 0.2), Type3 = c(0.25, 0.2, 0.15)), .Names = c(Time, 
Type1, Type2, Type3), class = data.frame, row.names = c(NA, 
-3L))


 DF
  Time Type1 Type2 Type3
11  0.50  0.25  0.25
24  0.55  0.25  0.20
35  0.65  0.20  0.15


# Create a newdata frame with a column with the full Time sequence
TimeFill - data.frame(Time = with(DF, min(Time):max(Time)))

 TimeFill
  Time
11
22
33
44
55


# merge the full sequence and the original DF together. See ?merge
DF2 - merge(DF, TimeFill, all = TRUE)


 DF2
  Time Type1 Type2 Type3
11  0.50  0.25  0.25
22NANANA
33NANANA
44  0.55  0.25  0.20
55  0.65  0.20  0.15


# Now transpose the columns in DF to the matrix required for the plot
barplot(t(DF2[, -1]), names.arg = DF2$Time)


HTH,

Marc Schwartz

__
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] stacked bar plot

2011-03-23 Thread Chandra Salgado Kent
Many thanks!! That's a million times easier!! :-)
 
All the best,
 
Chandra



From: istaz...@gmail.com on behalf of Ista Zahn
Sent: Wed 3/23/2011 12:06 PM
To: Chandra Salgado Kent
Cc: r-help@r-project.org
Subject: Re: [R] stacked bar plot



FWIW, the ggplot option I suggested works fine with sums instead of means...

library(ggplot2)
.Table-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3),
Group_size=c(1,1,2,2,2))
ggplot(.Table, aes(Group_size, Number, fill=Sex)) +
 geom_bar(stat=summary, fun.y=sum)


Best,
Ista

On Wed, Mar 23, 2011 at 3:21 AM, Chandra Salgado Kent
c.salg...@cmst.curtin.edu.au wrote:
 Hello,

 Many thanks for your responses! They were very helpful.
 FYI, ggplot didn't work for me because I needed the sum of the values.

 The fudged option of barplot was very helpful. Since my matrix is extremely 
 large (the example is a subset), and I would need to take a lot of time to 
 insert NAs everywhere as you did, I used the main idea you sent but instead 
 did summed over group sizes. I'm sure this is far from the most efficient way 
 of doing this, but it was the only way I found for my very large matrix.

 Thanks again!!

 Here is my solution:

 #-
 .Table-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
 Group_size=c(1,1,2,2,2))

 #I separated the females first, and ordered them by group size
 Females-subset(.Table, Sex==F)
 .Order-order(Females$Group_size)
 FemalesF-rbind(Females$Group_size, Females$Number)[,.Order]
 FemalesF-t(FemalesF)
 #I then deleted any NAs which I had in my database, then summed Number for 
 each Group_size and converted it to a matrix
 Females1 - FemalesF[complete.cases(FemalesF[,2]),]
 Females2-by(FemalesF,FemalesF[,1], FUN = function(x){
  sum(x[,2]) })
 Females3-matrix(Females2)

 #I then did the same for the males
 Males-subset(.Table, Sex==M)
 .Order-order(Males$Group_size)
 MalesF-rbind(Males$Group_size, Males$Number)[,.Order]
 MalesF-t(MalesF)
 Males1 - MalesF[complete.cases(MalesF[,2]),]
 Males2-by(MalesF,MalesF[,1], FUN = function(x){
  sum(x[,2]) })
 Males3-matrix((Males2))

 #I then followed your example in forming a matrix of males and females 
 suitable for barplot and plotted the data
 .Matrix-matrix(c(Females3,Males3),ncol=2)
 .Matrix-t(.Matrix)
 barplot(.Matrix,col=c(pink,lightblue),
  names.arg=c(1:3),xlab=Group size,ylab=Number,main=Group Sex)
 legend(10,60,c(Male,Female),fill=c(lightblue,pink))
 #

 Chandra


 

 From: Jim Lemon [mailto:j...@bitwrit.com.au]
 Sent: Tue 3/22/2011 5:55 PM
 To: Chandra Salgado Kent
 Cc: r-help@r-project.org
 Subject: Re: [R] stacked bar plot



 On 03/22/2011 06:30 PM, Chandra Salgado Kent wrote:
 Hello,



 I'm wondering if someone may be able to help me, and do apologize if there 
 is a simple and obvious solution for this. I am somewhat new to R, and have 
 been searching for a simple solution for a couple of days.



 I am interested in finding a tool that allows me to plot a stacked bar plot.



 My data set is in the following format:

 data-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
 Group_size=c(1,1,2,2,2))



 I would like to have the factor Sex stacked, Group size as a Factor on 
 the X axis, and Number on the Y axis (summed so that there is only one 
 value for each Sex by Group_size combination).

 Hi Chandra,
 It's a bit hard to work out exactly what you want, but try this:
  barplot(matrix(c(10,3,NA,1,2,3),ncol=2),col=c(lightblue,pink,pink),
  names.arg=1:2,xlab=Group size,ylab=Number,main=Group Sex)
 legend(1.6,8,c(Male,Female),fill=c(lightblue,pink))

 now I have fudged a bit by just making the matrix contain the values in
 the right order, but if the barplot is what you want, it could get you
 started.

 Jim



[[alternative HTML version deleted]]

 __
 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.




--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org http://yourpsyche.org/ 



[[alternative HTML version deleted]]

__
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] stacked bar plot

2011-03-23 Thread Greg Snow
You can use the tapply function to sum within combinations, then pass the 
results to barplot (possibly doing a reshape first).

Also look at the ggplot2 package, it may do the summing as part of the plot 
call and probably does not need the reshape step.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Chandra Salgado Kent
 Sent: Tuesday, March 22, 2011 1:30 AM
 To: r-help@r-project.org
 Subject: [R] stacked bar plot
 
 Hello,
 
 
 
 I'm wondering if someone may be able to help me, and do apologize if
 there is a simple and obvious solution for this. I am somewhat new to
 R, and have been searching for a simple solution for a couple of days.
 
 
 
 I am interested in finding a tool that allows me to plot a stacked bar
 plot.
 
 
 
 My data set is in the following format:
 
 data-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3),
 Group_size=c(1,1,2,2,2))
 
 
 
 I would like to have the factor Sex stacked, Group size as a Factor
 on the X axis, and Number on the Y axis (summed so that there is only
 one value for each Sex by Group_size combination).
 
 
 
 Many, many thanks for any help you may be able to offer!
 
 
 
 Chandra
 
 
   [[alternative HTML version deleted]]
 
 __
 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] stacked bar plot

2011-03-23 Thread John Kane


--- On Wed, 3/23/11, Greg Snow greg.s...@imail.org wrote:

 From: Greg Snow greg.s...@imail.org
 Subject: Re: [R] stacked bar plot
 To: Chandra Salgado Kent c.salg...@cmst.curtin.edu.au, 
 r-help@r-project.org r-help@r-project.org



 Also look at the ggplot2 package, it may do the summing as
 part of the plot call and probably does not need the reshape
 step.

If I'm reading Hadley's book correctly that is what it does if you have 
summarized data you need to explictly disable the reshape


 -- 
 Gregory (Greg) L. Snow Ph.D.
 Statistical Data Center
 Intermountain Healthcare
 greg.s...@imail.org
 801.408.8111
 
 
  -Original Message-
  From: r-help-boun...@r-project.org
 [mailto:r-help-bounces@r-
  project.org] On Behalf Of Chandra Salgado Kent
  Sent: Tuesday, March 22, 2011 1:30 AM
  To: r-help@r-project.org
  Subject: [R] stacked bar plot
  
  Hello,
  
  
  
  I'm wondering if someone may be able to help me, and
 do apologize if
  there is a simple and obvious solution for this. I am
 somewhat new to
  R, and have been searching for a simple solution for a
 couple of days.
  
  
  
  I am interested in finding a tool that allows me to
 plot a stacked bar
  plot.
  
  
  
  My data set is in the following format:
  
  data-data.frame(Sex=c(M,F,M,F,F),
 Number=c(10,3,1,2,3),
  Group_size=c(1,1,2,2,2))
  
  
  
  I would like to have the factor Sex stacked, Group
 size as a Factor
  on the X axis, and Number on the Y axis (summed so
 that there is only
  one value for each Sex by Group_size combination).
  
  
  
  Many, many thanks for any help you may be able to
 offer!
  
  
  
  Chandra
  
  
      [[alternative HTML version
 deleted]]
  
  __
  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.
 



__
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] stacked bar plot

2011-03-22 Thread Chandra Salgado Kent
Hello,

 

I'm wondering if someone may be able to help me, and do apologize if there is a 
simple and obvious solution for this. I am somewhat new to R, and have been 
searching for a simple solution for a couple of days.

 

I am interested in finding a tool that allows me to plot a stacked bar plot.

 

My data set is in the following format:

data-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
Group_size=c(1,1,2,2,2))

 

I would like to have the factor Sex stacked, Group size as a Factor on the 
X axis, and Number on the Y axis (summed so that there is only one value for 
each Sex by Group_size combination). 

 

Many, many thanks for any help you may be able to offer!

 

Chandra


[[alternative HTML version deleted]]

__
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] stacked bar plot

2011-03-22 Thread Jim Lemon

On 03/22/2011 06:30 PM, Chandra Salgado Kent wrote:

Hello,



I'm wondering if someone may be able to help me, and do apologize if there is a 
simple and obvious solution for this. I am somewhat new to R, and have been 
searching for a simple solution for a couple of days.



I am interested in finding a tool that allows me to plot a stacked bar plot.



My data set is in the following format:

data-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
Group_size=c(1,1,2,2,2))



I would like to have the factor Sex stacked, Group size as a Factor on the X axis, 
and Number on the Y axis (summed so that there is only one value for each Sex by Group_size 
combination).


Hi Chandra,
It's a bit hard to work out exactly what you want, but try this:
 barplot(matrix(c(10,3,NA,1,2,3),ncol=2),col=c(lightblue,pink,pink),
 names.arg=1:2,xlab=Group size,ylab=Number,main=Group Sex)
legend(1.6,8,c(Male,Female),fill=c(lightblue,pink))

now I have fudged a bit by just making the matrix contain the values in 
the right order, but if the barplot is what you want, it could get you 
started.


Jim

__
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] stacked bar plot

2011-03-22 Thread Ista Zahn
Hi Chandra,
You could use ggplot2:

library(ggplot2)
ggplot(dat, aes(Group_size, Number, fill=Sex)) +
  geom_bar(stat=summary, fun.y=mean)

Best,
Ista

On Tue, Mar 22, 2011 at 7:30 AM, Chandra Salgado Kent
c.salg...@cmst.curtin.edu.au wrote:
 Hello,



 I'm wondering if someone may be able to help me, and do apologize if there is 
 a simple and obvious solution for this. I am somewhat new to R, and have been 
 searching for a simple solution for a couple of days.



 I am interested in finding a tool that allows me to plot a stacked bar plot.



 My data set is in the following format:

 data-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
 Group_size=c(1,1,2,2,2))



 I would like to have the factor Sex stacked, Group size as a Factor on 
 the X axis, and Number on the Y axis (summed so that there is only one 
 value for each Sex by Group_size combination).



 Many, many thanks for any help you may be able to offer!



 Chandra


        [[alternative HTML version deleted]]

 __
 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.




-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

__
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] stacked bar plot

2011-03-22 Thread Chandra Salgado Kent
Hello,
 
Many thanks for your responses! They were very helpful. 
FYI, ggplot didn't work for me because I needed the sum of the values. 
 
The fudged option of barplot was very helpful. Since my matrix is extremely 
large (the example is a subset), and I would need to take a lot of time to 
insert NAs everywhere as you did, I used the main idea you sent but instead did 
summed over group sizes. I'm sure this is far from the most efficient way of 
doing this, but it was the only way I found for my very large matrix. 
 
Thanks again!! 
 
Here is my solution:
 
#-
.Table-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
Group_size=c(1,1,2,2,2))

#I separated the females first, and ordered them by group size
Females-subset(.Table, Sex==F)
.Order-order(Females$Group_size)
FemalesF-rbind(Females$Group_size, Females$Number)[,.Order]
FemalesF-t(FemalesF) 
#I then deleted any NAs which I had in my database, then summed Number for each 
Group_size and converted it to a matrix
Females1 - FemalesF[complete.cases(FemalesF[,2]),]
Females2-by(FemalesF,FemalesF[,1], FUN = function(x){
 sum(x[,2]) })
Females3-matrix(Females2)

#I then did the same for the males
Males-subset(.Table, Sex==M)
.Order-order(Males$Group_size)
MalesF-rbind(Males$Group_size, Males$Number)[,.Order]
MalesF-t(MalesF) 
Males1 - MalesF[complete.cases(MalesF[,2]),]
Males2-by(MalesF,MalesF[,1], FUN = function(x){
 sum(x[,2]) })
Males3-matrix((Males2))

#I then followed your example in forming a matrix of males and females suitable 
for barplot and plotted the data
.Matrix-matrix(c(Females3,Males3),ncol=2)
.Matrix-t(.Matrix)
barplot(.Matrix,col=c(pink,lightblue),
  names.arg=c(1:3),xlab=Group size,ylab=Number,main=Group Sex)
legend(10,60,c(Male,Female),fill=c(lightblue,pink))
#

Chandra




From: Jim Lemon [mailto:j...@bitwrit.com.au]
Sent: Tue 3/22/2011 5:55 PM
To: Chandra Salgado Kent
Cc: r-help@r-project.org
Subject: Re: [R] stacked bar plot



On 03/22/2011 06:30 PM, Chandra Salgado Kent wrote:
 Hello,



 I'm wondering if someone may be able to help me, and do apologize if there is 
 a simple and obvious solution for this. I am somewhat new to R, and have been 
 searching for a simple solution for a couple of days.



 I am interested in finding a tool that allows me to plot a stacked bar plot.



 My data set is in the following format:

 data-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
 Group_size=c(1,1,2,2,2))



 I would like to have the factor Sex stacked, Group size as a Factor on 
 the X axis, and Number on the Y axis (summed so that there is only one 
 value for each Sex by Group_size combination).

Hi Chandra,
It's a bit hard to work out exactly what you want, but try this:
  barplot(matrix(c(10,3,NA,1,2,3),ncol=2),col=c(lightblue,pink,pink),
  names.arg=1:2,xlab=Group size,ylab=Number,main=Group Sex)
legend(1.6,8,c(Male,Female),fill=c(lightblue,pink))

now I have fudged a bit by just making the matrix contain the values in
the right order, but if the barplot is what you want, it could get you
started.

Jim



[[alternative HTML version deleted]]

__
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] stacked bar plot

2011-03-22 Thread Ista Zahn
FWIW, the ggplot option I suggested works fine with sums instead of means...

library(ggplot2)
.Table-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3),
Group_size=c(1,1,2,2,2))
ggplot(.Table, aes(Group_size, Number, fill=Sex)) +
 geom_bar(stat=summary, fun.y=sum)


Best,
Ista

On Wed, Mar 23, 2011 at 3:21 AM, Chandra Salgado Kent
c.salg...@cmst.curtin.edu.au wrote:
 Hello,

 Many thanks for your responses! They were very helpful.
 FYI, ggplot didn't work for me because I needed the sum of the values.

 The fudged option of barplot was very helpful. Since my matrix is extremely 
 large (the example is a subset), and I would need to take a lot of time to 
 insert NAs everywhere as you did, I used the main idea you sent but instead 
 did summed over group sizes. I'm sure this is far from the most efficient way 
 of doing this, but it was the only way I found for my very large matrix.

 Thanks again!!

 Here is my solution:

 #-
 .Table-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
 Group_size=c(1,1,2,2,2))

 #I separated the females first, and ordered them by group size
 Females-subset(.Table, Sex==F)
 .Order-order(Females$Group_size)
 FemalesF-rbind(Females$Group_size, Females$Number)[,.Order]
 FemalesF-t(FemalesF)
 #I then deleted any NAs which I had in my database, then summed Number for 
 each Group_size and converted it to a matrix
 Females1 - FemalesF[complete.cases(FemalesF[,2]),]
 Females2-by(FemalesF,FemalesF[,1], FUN = function(x){
  sum(x[,2]) })
 Females3-matrix(Females2)

 #I then did the same for the males
 Males-subset(.Table, Sex==M)
 .Order-order(Males$Group_size)
 MalesF-rbind(Males$Group_size, Males$Number)[,.Order]
 MalesF-t(MalesF)
 Males1 - MalesF[complete.cases(MalesF[,2]),]
 Males2-by(MalesF,MalesF[,1], FUN = function(x){
  sum(x[,2]) })
 Males3-matrix((Males2))

 #I then followed your example in forming a matrix of males and females 
 suitable for barplot and plotted the data
 .Matrix-matrix(c(Females3,Males3),ncol=2)
 .Matrix-t(.Matrix)
 barplot(.Matrix,col=c(pink,lightblue),
  names.arg=c(1:3),xlab=Group size,ylab=Number,main=Group Sex)
 legend(10,60,c(Male,Female),fill=c(lightblue,pink))
 #

 Chandra


 

 From: Jim Lemon [mailto:j...@bitwrit.com.au]
 Sent: Tue 3/22/2011 5:55 PM
 To: Chandra Salgado Kent
 Cc: r-help@r-project.org
 Subject: Re: [R] stacked bar plot



 On 03/22/2011 06:30 PM, Chandra Salgado Kent wrote:
 Hello,



 I'm wondering if someone may be able to help me, and do apologize if there 
 is a simple and obvious solution for this. I am somewhat new to R, and have 
 been searching for a simple solution for a couple of days.



 I am interested in finding a tool that allows me to plot a stacked bar plot.



 My data set is in the following format:

 data-data.frame(Sex=c(M,F,M,F,F), Number=c(10,3,1,2,3), 
 Group_size=c(1,1,2,2,2))



 I would like to have the factor Sex stacked, Group size as a Factor on 
 the X axis, and Number on the Y axis (summed so that there is only one 
 value for each Sex by Group_size combination).

 Hi Chandra,
 It's a bit hard to work out exactly what you want, but try this:
  barplot(matrix(c(10,3,NA,1,2,3),ncol=2),col=c(lightblue,pink,pink),
  names.arg=1:2,xlab=Group size,ylab=Number,main=Group Sex)
 legend(1.6,8,c(Male,Female),fill=c(lightblue,pink))

 now I have fudged a bit by just making the matrix contain the values in
 the right order, but if the barplot is what you want, it could get you
 started.

 Jim



        [[alternative HTML version deleted]]

 __
 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.




-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

__
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] Stacked Bar Plot

2009-11-20 Thread Gary
Hi R Folks,

I need to plot a stacked bar plot with row labels as A,B,C,... and each bar
divided by x,y, Here is the data:

  x y
A 1 .5
B -.2 .6
C .3 -.7
D .4 .8
(for e.g. x,y,... could be air pollutants and A,B,C,... could be months)

Issue 1) It has negative values.
Issue 2) It contain two categorical variables.

Can anyone help me?

Thanks,
Gary

[[alternative HTML version deleted]]

__
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] Stacked Bar Plot

2009-11-20 Thread David Winsemius


On Nov 20, 2009, at 6:54 PM, Gary wrote:


Hi R Folks,

I need to plot a stacked bar plot with row labels as A,B,C,... and  
each bar

divided by x,y, Here is the data:

 x y
A 1 .5
B -.2 .6
C .3 -.7
D .4 .8
(for e.g. x,y,... could be air pollutants and A,B,C,... could be  
months)


At the moment the letters appear to be labels.



Issue 1) It has negative values.
Issue 2) It contain two categorical variables.


It??? (In my opinion, the indefinite pronoun should be severely  
deprecated in technical discourse.)




Can anyone help me?


RSiteSearch(stacked barplot)  # 44 hits




Thanks,
Gary


and provide commented, minimal, self-contained, reproducible code.


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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] Stacked Bar Plot

2009-11-20 Thread RICHARD M. HEIBERGER
tmp - cbind(x=c(1,-.2,.3,.4),
 y=c(.5,.6,-.7,.8))
row.names(tmp) - letters[1:4]
barchart(tmp,
 horizontal=FALSE,
 stack=TRUE,
 auto.key=list(
   title=pollutant,
   border=TRUE),
 xlab=Month,
 main=Interesting Plot)

barchart(tmp,
 horizontal=FALSE,
 stack=TRUE,
 auto.key=list(
   title=pollutant,
   cex.title=1.2,
   border=TRUE),
 xlab=Month,
 ylab=something else,
 main=Interesting Plot)

__
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] Stacked bar plot anomaly When column contains a negative and a positive value

2008-05-03 Thread Duncan Murdoch

On 03/05/2008 3:29 PM, Murray Richardson wrote:

Hello users,

I've noticed a problem when creating a stacked column plot when a column 
contains a negative and a positive value.  e.g.


series1-c(-1,-2, 3, 4, 5)
series2-c( 5, -4,-3,-2, 1)
data-rbind(series1,series2)
barplot(as.matrix(data), beside=FALSE)


In these cases (i.e. first, third and fifth columns) the plotting is not 
handled correctly. Compare this output with that of:


It doesn't make sense to stack values with mixed signs, but what barplot 
does appears to make sense.  The first column has a bar of length -1 
overplotted with a bar of length 5, which completely obscures it.  What 
would you expect it to do?


Duncan Murdoch




barplot(as.matrix(data), beside=TRUE)


Shouldn't the plots look the same except in the beside=FALSE scenario 
the constituent bars should not be juxtaposed but instead are one on top 
of the other?


Thanks for any advice!

Murray Richardson

__
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.


[R] Stacked bar plot anomaly When column contains a negative and a positive value

2008-05-03 Thread Murray Richardson

Hello all

Please ignore this last post as I now realize it is because the stacked 
components are summed, as they should be.  I needed the columns to be 
stacked but not summed so just plotted the two series separately with add=T


Sorry...

Murray Richardson



 Original Message 
Subject: 	Stacked bar plot anomaly When column contains a negative and a 
positive value

Date:   Sat, 03 May 2008 15:29:41 -0400
From:   Murray Richardson [EMAIL PROTECTED]
To: r-help@R-project.org



Hello users,

I've noticed a problem when creating a stacked column plot when a column 
contains a negative and a positive value.  e.g.


series1-c(-1,-2, 3, 4, 5)
series2-c( 5, -4,-3,-2, 1)
data-rbind(series1,series2)
barplot(as.matrix(data), beside=FALSE)


In these cases (i.e. first, third and fifth columns) the plotting is not 
handled correctly. Compare this output with that of:



barplot(as.matrix(data), beside=TRUE)


Shouldn't the plots look the same except in the beside=FALSE scenario 
the constituent bars should not be juxtaposed but instead are one on top 
of the other?


Thanks for any advice!

Murray Richardson

__
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.