Gewure
Gewure

Reputation: 1268

barplotting a list of numbers in R

Im totally new to R, im working on some scripts for plotting some cheap barplot charts for my thesis.

I have a file with portnumbers, one by line:

115
220
192
196
1433
115
220
192
196
1433
115
220
192
196
1433
115
220
192
196
1433

now i want to plot this in a barplot . X-Axis: Occurences Y-Axis: Portnumber

Here is my not working script. It would work, if the Numbers wouldn't be interpreted as Strings, i suppose, because it works with strings. How do i fix this?

#!/usr/bin/Rscript

#barplot
dat=read.table("data/ports.txt", header=FALSE)
plot(dat, col=c("#ccf0fe9f"),horiz=TRUE,cex.names=0.5,las=1,width=5, xlab='Attacks')

when executing i get some very unrelated warnings.. so unrelated, i think it's of no use posting them in here.

thanks in advance for any hints & help!

EDIT: here are the warnings(sadly in german):

f00@localhost R$ ./ports.r 
There were 18 warnings (use warnings() to see them)
Warnmeldungen:
1: In plot.window(...) : "horiz" ist kein Grafikparameter
2: In plot.window(...) : "cex.names" ist kein Grafikparameter
3: In plot.window(...) : "width" ist kein Grafikparameter
4: In plot.xy(xy, type, ...) : "horiz" ist kein Grafikparameter
5: In plot.xy(xy, type, ...) : "cex.names" ist kein Grafikparameter
6: In plot.xy(xy, type, ...) : "width" ist kein Grafikparameter
7: In axis(side = side, at = at, labels = labels, ...) :
  "horiz" ist kein Grafikparameter
8: In axis(side = side, at = at, labels = labels, ...) :
  "cex.names" ist kein Grafikparameter
9: In axis(side = side, at = at, labels = labels, ...) :
  "width" ist kein Grafikparameter
10: In axis(side = side, at = at, labels = labels, ...) :
  "horiz" ist kein Grafikparameter
11: In axis(side = side, at = at, labels = labels, ...) :
  "cex.names" ist kein Grafikparameter
12: In axis(side = side, at = at, labels = labels, ...) :
  "width" ist kein Grafikparameter
13: In box(...) : "horiz" ist kein Grafikparameter
14: In box(...) : "cex.names" ist kein Grafikparameter
15: In box(...) : "width" ist kein Grafikparameter
16: In title(...) : "horiz" ist kein Grafikparameter
17: In title(...) : "cex.names" ist kein Grafikparameter
18: In title(...) : "width" ist kein Grafikparameter

Upvotes: 0

Views: 1105

Answers (2)

Zheyuan Li
Zheyuan Li

Reputation: 73405

I want to plot this in a bar plot . X-Axis: Occurences Y-Axis: Portnumber

I think you want

dat <- scan("data/ports.txt‌​")    ## use `scan` if you are to read in a vector
x <- table(dat)    ## contingency table
barplot(x, col=c("#ccf0fe9f"), horiz=TRUE, cex.names=0.5, las=1, width=5, 
        xlab="Occurences", ylab="Attacks")

enter image description here

Upvotes: 2

andyyy
andyyy

Reputation: 1015

Try

barplot(table(dat), col=c("#ccf0fe9f"),horiz=TRUE,cex.names=0.5,las=1,width=5, xlab='Attacks') 

plot() will create a scatter plot as default, so you need to use the barplot() function, and the table() function creates the counts which are required for your bar chart

Upvotes: 2

Related Questions