Reputation: 93
I'm new to working in Unix, so I need help on how to put a histogram together using R in Linux environment?
File:
48302 50 0
48303 46 0
48304 45 0
48305 41 15
48306 44 21
48307 74 0
48308 71 0
48309 35 19
48310 66 0
48311 26 42
48312 44 23
48313 69 0
48314 77 0
48315 64 0
48316 60 3
48317 60 2
48318 62 15
48319 71 9
48320 65 13
48321 88 0
48322 4 29
I need to create a histogram using the data from the 3rd column.
Upvotes: 2
Views: 6268
Reputation: 16436
If you put the data in your example into a file, sample.txt
, you can then invoke R and do the following:
$ R
Now you're at a R prompt:
> d = read.table('sample.txt',col.name=c("col1","col2","col3"))
You can confirm that the data was loaded correctly into table d
using the dim
command:
> dim(d)
[1] 21 3
Now you can graph column 3 (col3
) as we labeled it above when we read it in from the file, like so:
> hist(d$col3)
Resulting in this plot:
If you want you can create the following .r
file, call it hist.r
:
d = read.table('sample.txt',col.name=c("col1","col2","col3"))
dim(d)
hist(d$col3)
Then run it using R's Rscript
command like this:
$ Rscript hist.r
[1] 21 3
This will appear to have done nothing, but it automatically will put a .pdf file in the directory from where you ran it with the contents of the histogram in it.
$ ls -l
total 24
-rw-rw-r-- 1 saml saml 80 Sep 11 02:35 hist.r
-rw-rw-r-- 1 saml saml 12840 Sep 11 02:37 Rplots.pdf
-rw-rw-r-- 1 saml saml 302 Sep 11 02:19 sample.txt
You can customize this so that instead of a .pdf file you'll get a .png file or what have you.
Upvotes: 5
Reputation: 299
The command hist(...)
will get an histogram for you.
More help about the command from the interactive help from R: ?hist
.
Upvotes: 0