Search code examples
rhistogramlattice

R Error in hist.default(Sepal.Length ~ Species, data = iris) : 'x' must be numeric


I am trying to plot 2 histograms of a numeric variable by a factor variable in my df.

I searched hist() to do this but why am I getting this error?

data(iris)
hist(Sepal.Length~Species,data=iris)
Error in hist.default(Sepal.Length ~ Species, data = iris) : 
  'x' must be numeric

UPDATE: I am able to get this to work:

histogram(~Sepal.Length|Species,data=iris,
          type="count",
          xlab="Sepal Length",
          main="Iris Dataset",
          layout=c(1,3))

It seems there might be a difference between histogram functions? https://www.rforge.net/doc/packages/FSA/hist.formula.html and http://127.0.0.1:27473/library/lattice/html/histogram.html


Solution

  • use

    library(FSA)
    hist(Sepal.Length~Species,data=iris)
    

    It works