aceminer
aceminer

Reputation: 4295

creating histogram bins in r

I have this code.

a = c("a", 1)
b = c("b",2)
c = c('c',3)
d = c('d',4)
e = c('e',5)
z = data.frame(a,b,c,d,e)
hist = hist(as.numeric(z[2,]))

I am trying to have a histogram such that the bins would be a,b,c,d,e

and the freq values would be 1,2,3,4,5.

However, it gives me an empty screen(no bins at all for histogram model)

Upvotes: 1

Views: 195

Answers (2)

River
River

Reputation: 1514

Perhaps this would work for you: it creates a data frame with the x elements being the letters a through 'e', and the y elements being the numbers 1 through 5. It then renders a histogram and tells ggplot not to perform any binning.

library(ggplot2)
tmp <- data.frame(x = letters[1:5], y = 1:5)
ggplot(tmp, aes(x = x, y = y)) + geom_histogram(stat = "identity")

Upvotes: 1

Jonas Tundo
Jonas Tundo

Reputation: 6207

You are plotting the factor levels of each column for row 2, which is in this case always 1.

When creating the dataframe you add stringsAsFactors=FALSE to avoid converting the numbers to factors. This should work:

z = data.frame(a,b,c,d,e,stringsAsFactors=FALSE)
hist(as.numeric(z[2,]))

Upvotes: 1

Related Questions