Skip to content

boxplot with grouped variables

5 messages · maggy yan, arun, Bert Gunter +1 more

#
Hi,
Try this:
dat1<- read.table(text="
??? V1????? V2???????? V3
1? Dosis Gewicht Geschlecht
2????? 0??? 6.62????????? m
3????? 0??? 6.65????????? m
4????? 0??? 5.78????????? m
5????? 0??? 5.63????????? m
6????? 1??? 6.78????????? m
7????? 1??? 6.45????????? m
8????? 1??? 5.68????????? m
9????? 1??? 5.89????????? m
10????? 0??? 6.72????????? f
11????? 0??? 6.75????????? f
12????? 0??? 5.89????????? f
13????? 0??? 5.78????????? f
14????? 1??? 6.89????????? f
15????? 1??? 7.75????????? f
16????? 1??? 4.89????????? f
17????? 1??? 5.89????????? f
",header=TRUE,sep="",stringsAsFactors=FALSE) 
dat2<-dat1[-1,]
boxplot(as.numeric(dat2$V2)[dat2$V3=="m"])# works
dat2$V2<- as.numeric(dat2$V2)
library(ggplot2)
library(plyr)
library(reshape2)
?dat2New<-mutate(melt(dat2,id.var=c("V1","V3")),combin= as.character(interaction(V1,V3,sep="_")))
ggplot(dat2New,aes(x=combin,y=value))+geom_boxplot()
#or
ggplot(dat2New,aes(x=combin,y=value))+geom_boxplot()+facet_wrap(~V3,scales="free_x",ncol=2)
A.K.



----- Original Message -----
From: maggy yan <kiotoqq at gmail.com>
To: R-help at r-project.org
Cc: 
Sent: Saturday, May 11, 2013 11:40 AM
Subject: [R]  boxplot with grouped variables

my dataset looked like this in the beginning:
? ? ? V1? ? ? V2? ? ? ?  V3
1? Dosis Gewicht Geschlecht
2? ? ? 0? ? 6.62? ? ? ? ? m
3? ? ? 0? ? 6.65? ? ? ? ? m
4? ? ? 0? ? 5.78? ? ? ? ? m
5? ? ? 0? ? 5.63? ? ? ? ? m

I need box plots for V2 with all combination of V1 and V3, so I deleted the
first row, and tried this:
boxplot(Daten$V2[Daten$V3=="m"])
but it does not work and I have no clue what I did wrong.
I'm thankful for any help!

??? [[alternative HTML version deleted]]

______________________________________________
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.
#
It will not work because the presence of the first row means that all
the variables are read in as factors, not numeric. You must convert
numeric variables to numeric **after** eliminating the first row, or
read the data in using read.table(..., head=TRUE). See ?read.table for
details.

**After** the above changes, perhaps

with(Daten, boxplot(Gewicht ~ interaction(Dosis,Geshlecht, drop=TRUE)) )

will work if you read in column names properly. Otherwise, V2
~interaction(V1,V3, drop=TRUE) if you do the conversions to numeric.

Please read *An Introduction to R" or other online R tutorial before
posting further here. You need to understand basic R data handling
before you proceed. At present, you do not.

-- Bert
On Sat, May 11, 2013 at 8:40 AM, maggy yan <kiotoqq at gmail.com> wrote:

  
    
#
You can't mix number and character data in a data frame column, so you will probably find that all your variables are factors, not numbers. Try, for example
class(Daten$V2)

It looks like you failed to specify 'header=TRUE' in a read.table statement. Reread the data with headers properly treated so that those first items become the variable names.

In additon, Daten$V2[Daten$V3=="m"]) will only give you data for rows in which V3=="m", which will be a single boxplot. (see ?"[" for what that subsetting operation does). To plot all combinations ( if V2 were correctly numeric) you could try
boxplot(V2~V1+V3, data=Daten)

S Ellison
#
You may also try:
library(ggplot2)
p<-ggplot(dat2New,aes(x=combin,y=value))+geom_boxplot()+facet_wrap(~V3,scales="free_x",ncol=2)
p+stat_summary(fun.y=mean,shape=2,col="red",geom="point")
A.K.

----- Original Message -----
From: maggy yan <kiotoqq at gmail.com>
To: R-help at r-project.org
Cc: 
Sent: Saturday, May 11, 2013 8:57 PM
Subject: [R]? need means on all boxplots, but only half of them got that

I tried to draw a point on all boxplots for their means, I did:

boxplot( Daten$weight~interaction(Daten$Dosis,Daten$sex, drop=TRUE))
means<-tapply( Daten$weight, Daten$Dosis, mean)
points(means, pch=5, col="red", lwd=5)

but only the boxplots for male got that point on them, its really weird
because I don't think that I separated the sex in the codes above




----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: maggy yan <kiotoqq at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, May 11, 2013 12:41 PM
Subject: Re: [R]  boxplot with grouped variables

Hi,
Try this:
dat1<- read.table(text="
??? V1????? V2???????? V3
1? Dosis Gewicht Geschlecht
2????? 0??? 6.62????????? m
3????? 0??? 6.65????????? m
4????? 0??? 5.78????????? m
5????? 0??? 5.63????????? m
6????? 1??? 6.78????????? m
7????? 1??? 6.45????????? m
8????? 1??? 5.68????????? m
9????? 1??? 5.89????????? m
10????? 0??? 6.72????????? f
11????? 0??? 6.75????????? f
12????? 0??? 5.89????????? f
13????? 0??? 5.78????????? f
14????? 1??? 6.89????????? f
15????? 1??? 7.75????????? f
16????? 1??? 4.89????????? f
17????? 1??? 5.89????????? f
",header=TRUE,sep="",stringsAsFactors=FALSE) 
dat2<-dat1[-1,]
boxplot(as.numeric(dat2$V2)[dat2$V3=="m"])# works
dat2$V2<- as.numeric(dat2$V2)
library(ggplot2)
library(plyr)
library(reshape2)
?dat2New<-mutate(melt(dat2,id.var=c("V1","V3")),combin= as.character(interaction(V1,V3,sep="_")))
ggplot(dat2New,aes(x=combin,y=value))+geom_boxplot()
#or
ggplot(dat2New,aes(x=combin,y=value))+geom_boxplot()+facet_wrap(~V3,scales="free_x",ncol=2)
A.K.



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.