Kosh
Kosh

Reputation: 1246

histogram with one column per each value in R

I am trying to plot simple histogram in R. I have an integer vector and I want to draw a histogram with one column per each value.

test_data = c(1,1,1,2,2,3,3,4)
hist(test_data)

But I get this

enter image description here

Please tell me whether it is possible to get the same result as I have in Python?

import matplotlib.pyplot as plt
test_data = [1,1,1,2,2,3,3,4]
plt.hist(test_data)
plt.show()

enter image description here

Upvotes: 0

Views: 773

Answers (2)

John Smith
John Smith

Reputation: 1698

You can use the nclass or breaks argument to adjust the number of bins.

test_data = c(1,1,1,2,2,3,3,4)
hist(test_data,breaks=5)
hist(test_data,nclass=5)

enter image description here

In fact it is the same thing for python. The argument is bins. The default value is 10 (according to this page)

So if you modify it, we will get a different plot

import matplotlib.pyplot as plt
test_data = [1,1,1,2,2,3,3,4]
plt.hist(test_data,bins=4)
plt.show()

you get

enter image description here

Upvotes: 1

rg255
rg255

Reputation: 4169

You could us the barplot and table functions

barplot(table(test_data))

Upvotes: 1

Related Questions