ETT
ETT

Reputation: 13

R: 2 histogram questions

I'm new to R, and I've been running into trouble trying to make a histogram with a density line.

My data has 3 subsets of data, and the 1st subset gives me an error when I try to make a histogram.

Sample code:

dataset<- read.table(file.choose(), header = T, sep="\t")
g1<- dataset$Average..01
hist(g1)

Error message: Error in hist.default(dataset$Average..01,: 'x' must be numeric

Yet, for the other two subsets of data the histogram is able to be plotted, Although the plot shows, the density line is below the x-axis. Sample code:

hist(dataset$Average..10)
xfit<-seq(min(dataset$Average..10),max(dataset$Average..10),length=40)
yfit<-dnorm(xfit,mean=mean(dataset$Average..10),sd=sd(ataset$Average..10))
yfit <-yfit*diff(dataset$Average..10[1:2])*length(dataset$Average..10)
lines(xfit, yfit, col="red", lwd=3)

I'm not too sure what's going on, or how to fix this. Any advice would be much appreciated!

Upvotes: 0

Views: 123

Answers (2)

Aneesh Cherian K
Aneesh Cherian K

Reputation: 532

I don’t know about your data, I have similar experiences, however I have used read.csv() function to read data.

If the data field contain "NA" fields, then the numeric fields may be read as character fields. Hence you have to convert the column to numeric fields.

What you can do is check the structure of your data

str(g1)

Check the data type of the column Average, if found character convert it to numeric

g1$Average <- as.numeric(g1$Average)

If found factor first you have to convert it to character before converting it to numeric

g1$Average <- as.numeric(as.character(g1$Average))

Upvotes: 1

Heymans
Heymans

Reputation: 128

Without a known dataset, you can plot a histogram with density lines, of "Average..01" using

hist(dataset$Average..01, prob = TRUE)
lines(density(dataset$Average..01))

Upvotes: 0

Related Questions