[R] clustered bar chart help

Jim Lemon jim at bitwrit.com.au
Sat Feb 19 12:07:16 CET 2011


On 02/19/2011 06:46 AM, Li, Qinghong, ST. LOUIS, NRC St. Louis Petcare 
wrote:
> Sorry the data were all messed up. Let me try again.
> Can anyone help me to plot a chart graph? I have a data set like this. I
> would like a bar chart graph which mouse1's two treatments are clustered
> together, so on.
>
> I tried with barplot, but couldn't get it right.
>
> 	Mouse1	Mouse2	Mouse3	
> Trial	C	T	C	T	C	T
>
> 1	2.4	4.3	2	4.3	2	7
> 2	2.8	5.4	2.8	6	2	8
> 3	3.1	4.5	3	4.5	3	7
> 4	3.1	4.4	3.1	4	3.1	7
> 5	2	4.1	2	4.1	2	7
> 6	2.2	4	2.2	3	4	6.9
> 7	2.7	5	4	5	2.7	6.5
> 8	4.5	5.6	4.5	6	4	5.6
> 9	2.3	4.5	2.3	4.5	2.3	4
> 10	3	4	4	6	2	4
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Li,Qinghong,ST. LOUIS,NRC St. Louis Petcare
> Sent: Friday, February 18, 2011 12:06 PM
> To: r-help at r-project.org
> Subject: [R] clustered bar chart help
>
> Hi,
>
>
>
> Can anyone help me to plot a chart graph? I have a data set like this. I
> would like a bar chart graph which mouse1's two treatments are clustered
> together, so on.
>
> I tried with barplot, but couldn't get it right.
>
Hi Johnny,
Try this (assume your data frame is named "mp"):

# string the scores out in a vector
mpv<-as.vector(as.matrix(mp))
# make another two vectors specifying which score
# belongs to which mouse and which condition
# and combine these into a data frame
mp2<-data.frame(mpv,mouse=rep(c("M1","M2","M3"),each=20),
  cond=rep(c("C","T","C","T","C","T"),each=10))
library(plotrix)
# make a list of colors for the bars
mc_colors<-list("lightgray",2:4,c("orange","brown"))
# display a clustered bar chart
barNest(mpv~mouse+cond,mp2,col=mc_colors,main="Mouse performance")

Jim



More information about the R-help mailing list