Hi all,
I have the following data in abc.dat
====================== 50 0 1 0 0
55 1 14 0 1
60 7 86 0 3
65 22 324 2 3
70 58 1035 1 7
75 30 2568 0 34
80 9 2936 15 162
85 27 2169 46 365
90 80 1439 212 432
95 236 1670 521 281
100 332 827 709 172
105 156 311 556 103
110 69 49 144 44
115 26 10 36 17
120 2 9 3 3
125 1 6 1 1
130 0 14 0 0
135 0 5 0 0
140 0 0 0 0
145 0 0 0 0
150 0 0 0 0
155 0 0 0 0
160 0 0 0 0
165 0 0 0 0
170 0 0 0 0
175 0 0 0 0
180 0 0 0 0
185 0 0 0 0
190 0 0 0 1
195 0 0 0 0
200 0 0 0 0
205 0 0 0 0
210 0 0 0 0
======================
which i have used
abc=read.table("abc.dat")
to read the table into R.
There are two problems:
1- I want the first column of the data to be
the 'column names', how should i read the data?
2- I want to plot the histogram, using the first column as
'x' values, and the 2nd,3rd,4th and 5th columns as the frequencies.
How do I plot it?
I have tried to add a 'row' of variable names to it,
and then read with 'header=T', then the first column
become 'col.names' as I was expecting it to be.
However, when I plot it using 'hist',
R uses the 2nd column as the 'x value', where it should be used as
'frequency'.
(the 50,55,60,65,70... should be on the x-axis)
Thanks!
Casper
--
View this message in context:
http://r.789695.n4.nabble.com/how-do-i-plot-this-hist-tp3032796p3032796.html
Sent from the R help mailing list archive at Nabble.com.
try this:> x <- read.table('clipboard') > xV1 V2 V3 V4 V5 1 50 0 1 0 0 2 55 1 14 0 1 3 60 7 86 0 3 4 65 22 324 2 3 5 70 58 1035 1 7 6 75 30 2568 0 34 7 80 9 2936 15 162 8 85 27 2169 46 365 9 90 80 1439 212 432 10 95 236 1670 521 281 11 100 332 827 709 172 12 105 156 311 556 103 13 110 69 49 144 44 14 115 26 10 36 17 15 120 2 9 3 3 16 125 1 6 1 1 17 130 0 14 0 0 18 135 0 5 0 0 19 140 0 0 0 0 20 145 0 0 0 0 21 150 0 0 0 0 22 155 0 0 0 0 23 160 0 0 0 0 24 165 0 0 0 0 25 170 0 0 0 0 26 175 0 0 0 0 27 180 0 0 0 0 28 185 0 0 0 0 29 190 0 0 0 1 30 195 0 0 0 0 31 200 0 0 0 0 32 205 0 0 0 0 33 210 0 0 0 0> x.m <- as.matrix(x) # dataframe -> matrix > barplot(t(x.m), names.arg = x.m[,1], las=2) >On Mon, Nov 8, 2010 at 5:42 PM, casperyc <casperyc at hotmail.co.uk> wrote:> > Hi all, > > I have the following data in abc.dat > > ======================> ?50 ? ? 0 ? ? 1 ? ? 0 ? ? 0 > ?55 ? ? 1 ? ?14 ? ? 0 ? ? 1 > ?60 ? ? 7 ? ?86 ? ? 0 ? ? 3 > ?65 ? ?22 ? 324 ? ? 2 ? ? 3 > ?70 ? ?58 ?1035 ? ? 1 ? ? 7 > ?75 ? ?30 ?2568 ? ? 0 ? ?34 > ?80 ? ? 9 ?2936 ? ?15 ? 162 > ?85 ? ?27 ?2169 ? ?46 ? 365 > ?90 ? ?80 ?1439 ? 212 ? 432 > ?95 ? 236 ?1670 ? 521 ? 281 > 100 ? 332 ? 827 ? 709 ? 172 > 105 ? 156 ? 311 ? 556 ? 103 > 110 ? ?69 ? ?49 ? 144 ? ?44 > 115 ? ?26 ? ?10 ? ?36 ? ?17 > 120 ? ? 2 ? ? 9 ? ? 3 ? ? 3 > 125 ? ? 1 ? ? 6 ? ? 1 ? ? 1 > 130 ? ? 0 ? ?14 ? ? 0 ? ? 0 > 135 ? ? 0 ? ? 5 ? ? 0 ? ? 0 > 140 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 145 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 150 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 155 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 160 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 165 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 170 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 175 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 180 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 185 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 190 ? ? 0 ? ? 0 ? ? 0 ? ? 1 > 195 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 200 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 205 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 210 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > ======================> > which i have used > > abc=read.table("abc.dat") > > to read the table into R. > > There are two problems: > > 1- I want the first column of the data to be > the 'column names', how should i read the data? > > 2- I want to plot the histogram, using the first column as > 'x' values, and the 2nd,3rd,4th and 5th columns as the frequencies. > > How do I plot it? > > > I have tried to add a 'row' of variable names to it, > and then read with 'header=T', then the first column > become 'col.names' as I was expecting it to be. > > However, when I plot it using 'hist', > R uses the 2nd column as the 'x value', where it should be used as > 'frequency'. > (the 50,55,60,65,70... should be on the x-axis) > > Thanks! > > Casper > -- > View this message in context: http://r.789695.n4.nabble.com/how-do-i-plot-this-hist-tp3032796p3032796.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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Typing too fast; last line should be: barplot(t(x.m[, 2:5]), names.arg = x.m[,1], las=2) On Mon, Nov 8, 2010 at 5:42 PM, casperyc <casperyc at hotmail.co.uk> wrote:> > Hi all, > > I have the following data in abc.dat > > ======================> ?50 ? ? 0 ? ? 1 ? ? 0 ? ? 0 > ?55 ? ? 1 ? ?14 ? ? 0 ? ? 1 > ?60 ? ? 7 ? ?86 ? ? 0 ? ? 3 > ?65 ? ?22 ? 324 ? ? 2 ? ? 3 > ?70 ? ?58 ?1035 ? ? 1 ? ? 7 > ?75 ? ?30 ?2568 ? ? 0 ? ?34 > ?80 ? ? 9 ?2936 ? ?15 ? 162 > ?85 ? ?27 ?2169 ? ?46 ? 365 > ?90 ? ?80 ?1439 ? 212 ? 432 > ?95 ? 236 ?1670 ? 521 ? 281 > 100 ? 332 ? 827 ? 709 ? 172 > 105 ? 156 ? 311 ? 556 ? 103 > 110 ? ?69 ? ?49 ? 144 ? ?44 > 115 ? ?26 ? ?10 ? ?36 ? ?17 > 120 ? ? 2 ? ? 9 ? ? 3 ? ? 3 > 125 ? ? 1 ? ? 6 ? ? 1 ? ? 1 > 130 ? ? 0 ? ?14 ? ? 0 ? ? 0 > 135 ? ? 0 ? ? 5 ? ? 0 ? ? 0 > 140 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 145 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 150 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 155 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 160 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 165 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 170 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 175 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 180 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 185 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 190 ? ? 0 ? ? 0 ? ? 0 ? ? 1 > 195 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 200 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 205 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > 210 ? ? 0 ? ? 0 ? ? 0 ? ? 0 > ======================> > which i have used > > abc=read.table("abc.dat") > > to read the table into R. > > There are two problems: > > 1- I want the first column of the data to be > the 'column names', how should i read the data? > > 2- I want to plot the histogram, using the first column as > 'x' values, and the 2nd,3rd,4th and 5th columns as the frequencies. > > How do I plot it? > > > I have tried to add a 'row' of variable names to it, > and then read with 'header=T', then the first column > become 'col.names' as I was expecting it to be. > > However, when I plot it using 'hist', > R uses the 2nd column as the 'x value', where it should be used as > 'frequency'. > (the 50,55,60,65,70... should be on the x-axis) > > Thanks! > > Casper > -- > View this message in context: http://r.789695.n4.nabble.com/how-do-i-plot-this-hist-tp3032796p3032796.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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hi casperyc,
While Jim Holtman's solution is quite neat, I thought I would add a
multiple histogram to the discussion in case that was what you wanted:
library(plotrix)
barp(t(x.m[,2:5]),names.arg=x.m[,1],col=rainbow(4))
legend(20,2500,paste("V",2:5,sep=""),fill=rainbow(4))
Jim