This question already has answers here:
Creating a histogram using aggregated data
(4 answers)
Closed 9 years ago.
HI Guys I'm trying to plot a frequency graph of a simple 2d file
file:data.csv
terms,count
1,10
5,17
3,28
9,30
I want the first col(terms) to be the x-axis and the col(count) be the height/percentage.
I've tried this:
d<-read.csv(data.csv)
hist(d)
Error in hist.default(d) : 'x' must be numeric
dc<-table(d)
hist(dc) <-- wrong result.
The problem is that hist() needs a vector containing your objects as often as they are present in your data. Your are providing it a frequency table.
See this:
> df <- data.frame(obj = c(1,2,3,4,5), count = c(2,3,5,4,2))
> hist(df)
Error in hist.default(df) : 'x' must be numeric
> hist(rep(df$obj, df$count), breaks=0:5)
[img]
> rep(df$obj, df$count)
[1] 1 1 2 2 2 3 3 3 3 3 4 4 4 4 5 5
rep(a,n) repeats element by element the value of a n-times. Then you have the vector you need and you can hand it to hist().
d<-read.csv(text="terms,count
1,10
5,17
3,28
9,30")
hist(d) # No error ... but not the plot you wanted.
Your lack of quotes around data.csv could be the problem or if the the first line in the file is really file:data.csv, that could be another problem. It does appear, however, that you probably want barchart or barplot, since you have already done the aggregation of the counts.
To illustrate why barchart or barplot could have been use:
require(lattice)
# dividing by total "counts" to get the fractional values
barchart(count/sum(d$count)~factor(terms), data=d)
Related
I am a newbie in R. I need to generate some graphs. I imported an excel file and need to create a histogram on one column. My importing code is-
file=read.xlsx('femalecommentcount.xlsx',1,header=FALSE)
col=file[2]
col looks like this (part) -
36961 1
36962 1
36963 7
36964 1
36965 2
36966 1
36967 1
36968 4
36969 1
36970 6
36971 3
36972 1
36973 6
36974 6
36975 2
36976 2
36977 8
36978 2
36979 1
36980 1
36981 1
the first column is the row number. I'm not sure how to remove this. The second column is my data that I want a histogram on. hist() function requires a vector, I'm not sure how exactly to convert.
If I just simple call -
hist(col)
it gives-
Error in hist.default(col) : 'x' must be numeric
I have tried few commands randomly from the internet, but they didn't work.
My eventual goal is to just generate a good histogram (and maybe other charts) on that column, to get a good understadning of the spread of my data.
It should be col=file[[2]] or col=file[, 2] --- solution given in comment
data import should be in correct way to avoid numeric issue
I have data file of the form:
unimportant1 unimportant2 unimportant3 matrixdata[i]
1e4 2e5 3e2 1 2 3 4 5
2e3 1e1 7e3 5 4 3 2 1
... ... ... ...
2e3 1e4 4e2 4 4 4 4 4
So it has columnheaders (here "unimportant1" to "unimportant3") as the first row. I want gnuplot to ignore these first three unimportant columns columns so the data entries in exponential notation. I want gnuplot to plot the matrixdata as a matrix. So as if I did it like this:
#!/usr/bin/gnuplot -p
plot '-' matrix with image
1 2 3 4 5
5 4 3 2 1
...
4 4 4 4 4
e
How do I get gnuplot to ignore the first three columns and the header row and plot the rest as matrix image? For compatibility, I would prefere a gnuplot built-in to do that, but I could write a shell script and use the `plot '< ...' syntax preprocessing the data file.
Edit: So neuhaus' answer almost solved it. The only thing I'm missing is, how to ignore the first row (line) with the text header data. Every seems to expect numeric data and so the whole plot fails as it's not a matrix. I don't want to comment out the fist line, as I'm using the unimportant data sets for other 2D plots that, in turn, use the header data.
So how do I skip a row in a matrix plot that already uses every to skip columns?
When using matrix gnuplot must first parse the data file before it can skip rows and columns. Now, your first row evaluates to four invalid number, the second row has 8 number and I get an error that Matrix does not represent a grid.
If you don't want to comment out the first line or skip it with an external tool like < tail -n +2 matrix.dat, then you could change it to contain some dummy strings like
unimportant1 unimportant2 unimportant3 matrixdata[i] B C D E
1e4 2e5 3e2 1 2 3 4 5
2e3 1e1 7e3 5 4 3 2 1
... ... ... ...
2e3 1e4 4e2 4 4 4 4 4
Now your first row has as many entries as the other rows, and you can plot this file with
plot 'test.txt' matrix every ::3:1 with image
This still gives you a warning: matrix contains missing or undefined values, but you don't need to care.
I'm not familiar with matrix plots, but I got some sample data and
plot 'matrix.dat' matrix every ::3 with image
seems to do the trick.
You could probably use shell commands, for instance, the following skips the first six lines of a file:
plot '<tail -n +7 terrain0.dem' matrix with image
I have data as follows in .csv format as I am new to ggplot2 graphs I am not able to do this
T L
141.5453333 1
148.7116667 1
154.7373333 1
228.2396667 1
148.4423333 1
131.3893333 1
139.2673333 1
140.5556667 2
143.719 2
214.3326667 2
134.4513333 3
169.309 8
161.1313333 4
I tried to plot a line graph using following graph
data<-read.csv("sample.csv",head=TRUE,sep=",")
ggplot(data,aes(T,L))+geom_line()]
but I got following image it is not I want
I want following image as follows
Can anybody help me?
You want to use a variable for the x-axis that has lots of duplicated values and expect the software to guess that the order you want those points plotted is given by the order they appear in the data set. This also means the values of the variable for the x-axis no longer correspond to the actual coordinates in the coordinate system you're plotting in, i.e., you want to map a value of "L=1" to different locations on the x-axis depending on where it appears in your data.
This type of fairly non-sensical thing does not work in ggplot2 out of the box. You have to define a separate variable that has a proper mapping to values on the x-axis ("id" in the code below) and then overwrite the labels with the values for "L".
The coe below shows you how to do this, but it seems like a different graphical display would probbaly be better suited for this kind of data.
data <- as.data.frame(matrix(scan(text="
141.5453333 1
148.7116667 1
154.7373333 1
228.2396667 1
148.4423333 1
131.3893333 1
139.2673333 1
140.5556667 2
143.719 2
214.3326667 2
134.4513333 3
169.309 8
161.1313333 4
"), ncol=2, byrow=TRUE))
names(data) <- c("T", "L")
data$id <- 1:nrow(data)
ggplot(data,aes(x=id, y=T))+geom_line() + xlab("L") +
scale_x_continuous(breaks=data$id, labels=data$L)
You have an error in your code, try this:
ggplot(data,aes(x=L, y=T))+geom_line()
Default arguments for aes are:
aes(x, y, ...)
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to create grouped barplot with R
Species Dbh Height
1 DF 383.7143 254.3036
2 ES 403.3333 280.0000
3 F 372.0000 270.0000
4 FG 381.5000 275.0000
5 GF 351.5838 242.6522
6 HW 209.0000 198.0000
7 LP 232.8571 218.3333
8 PP 568.5000 330.0000
9 SF 136.4286 154.1000
10 WC 375.0757 234.8777
11 WL 340.0588 252.5714
12 WP 319.7273 251.3939
I want to turn the data above into a bar graph like this one. Species as bins and dbh and height as bars for each bin.
I used the code:
aggregate(ufc[,4:5],ufc[3],mean,na.rm=TRUE)
to get the above data set
I can only get one variable at a time using this code:
barplot(ufc.means$Height, col=rainbow(20),
names.arg=(ufc.means$Species), las=2,main="Height")
First, do not double post! If necessary edit your previous question, rather than posting a new question.
Second, the error message that you mentioned in your earlier post is pretty self-explanatory. Here's the error message:
Error in barplot.default(ufc.means, col = rainbow(20), names.arg = (ufc.means$Species), : 'height' must be a vector or a matrix
Read that last part carefully: 'height' must be a vector or a matrix, but you are trying to use a data.frame. So, the solution is easy: convert your data.frame to a matrix before using barplot.
Assuming your data.frame is named "mydf":
mymat <- t(mydf[-1])
colnames(mymat) <- mydf[, 1]
barplot(mymat, beside = TRUE)
Result:
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
R filtering out a subset
I have an R dataset. In this dataset, I wish to create a crosstable using the package gmodels for two categorial variables, and then run a chisq.test on them.
The two variables are witness and agegroup. witness consists of observations that has value 1,2 and 9. agegroup consists of values 1,2.
I wish to exclude values if witness=9, or/and a 3rd variable EMS=2 from the table but I am not sure how to proceed.
library(gmodels)
CrossTable (mydata$witness, mydata$agegroup)
chisq.test (mydata$witness, mydata$agegroup)
...so my question is, how can i do the above with the conditions that witness!=9 and EMS!=2
data:
witness agegroup EMS
1 1 2
2 2 2
1 1 2
2 1 2
9 2 2
2 2 2
1 2 2
9 2 2
2 1 2
#save the data in your current working directory
data <- read.table("data", header=TRUE, sep = " ")
data$witness[data$witness == "9"] <- NA
mydata <- data[!is.na(data$witness),]
library("gmodels")
CrossTable(mydata$witness, mydata$agegroup, chisq=TRUE)
You can leave the variable "EMS" in "mydata". It does no harm to your analysis!
HTH
I expect this question to be closed as it really seems like a duplicate. But as both Chase and I suggested, I think some form of subsetting is the simplest way to go about this, e.g.
mydata[mydata$witness !=9 & mydata$EMS !=2,]