Reputation: 25
my My hist function is not doing plots in R, what can i do? This is an example of the data:
b=c(34,34,34,5,6,9,0,10,22,23)
t=hist(b)
t
and the output is:
$breaks
[1] 0 5 10 15 20 25 30 35
$counts
[1] 2 3 0 0 2 0 3
$density
[1] 0.04 0.06 0.00 0.00 0.04 0.00 0.06
$mids
[1] 2.5 7.5 12.5 17.5 22.5 27.5 32.5
$xname
[1] "b"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
What can I do? thanks.
Upvotes: 2
Views: 2634
Reputation: 4505
The hist
function in your case does exactly what it supposed to do. This function is used mainly for it's sideffect i.e. plotting the histogram (when argument plot
is TRUE
which is the default). But the value function returns is a list with several components (breaks
, counts
, density
, mids
, xname
, equidist
). So when you call t = hist(b)
you assign the value returned by hist
function, to the variable t
(btw t
is the function in R, I wouldn't use t
as a variable name). As a result, when you cal t
it prints returned value, a list mentioned above, but not a plot which you probably expect to see.
Assuming that your objective is to save a histogram and plot it latter, you can use following observations:
First we create a reproducible example:
set.seed(72158867)
x <- rnorm(100)
Here we assigned 100 random values to the variable x
.
Now we are ready to create a histogram, and assign the result returned by hist()
to the variable h
:
h <- hist(x, plot = FALSE)
Here, with the argument plot = FALSE
we tell the function to return result without plotting.
h
The value returned by hist()
is an object. We can dig into this object, to find how to work with it in order to achieve our objectives.
First we can examine the structure of the returned object using the str()
function:
str(h)
# List of 6
# $ breaks : num [1:11] -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 ...
# $ counts : int [1:10] 5 8 15 20 17 14 14 5 1 1
# $ density : num [1:10] 0.1 0.16 0.3 0.4 0.34 0.28 0.28 0.1 0.02 0.02
# $ mids : num [1:10] -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 1.75 2.25 2.75
# $ xname : chr "x"
# $ equidist: logi TRUE
# - attr(*, "class")= chr "histogram"
Here we can see that the object returned by hist()
is a list-like structure, with several elements. We can also see that the result hist()
returned is a object of a class histogram
.
Another way to identify the class of the object returned by hist()
is calling a class()
function on it.
class(h)
#[1] "histogram"
You can also call ?hist
from your console, and find the information about returned value for the hist()
.
To find out what kind of methods are defined on the object of class histogram
we can use methods()
function, as shown in example below:
methods(class = class(h))
# [1] lines plot
# see '?methods' for accessing help and source code
Now we know that object of class histogram
accepts two methods lines
and plot
. We are not interested in first one (however I wonder what it does), but second one seems to be something we are looking for.
plot
method on the returned objectLet's call the plot()
function on a histogram
object:
plot(h, col = 'skyblue', border = 'yellow', main = 'Histogram for SOq #72158867')
It works as expected!
Basically, it is what hist()
function does when you call it:
histogram
from your data;plot
method on your histogram
object;histogram
object.Of course there is a shorter way to find it out. Just type ?hist
in your console, and you will find all hist
-related information by yourself.
Upvotes: 2