0

I have a problem I want to draw the frequency histogram of a column using certain filters but then it tells me Error ggplot2 doesn't know how to deal with data of class numeric.

ggplot(dataset, aes(x=perdayDiff)) + geom_histogram(binwidth=.5) works fine but

ggplot(subset(dataset$perdayDiff, dataset$symbole == "ASYMO" & dataset$perdayDiff > 0), aes(x=perdayDiff)) + geom_histogram(binwidth=.5)

returns Error: ggplot2 doesn't know how to deal with data of class numeric

How could I draw a frequency histogram of one column of dataframe using filters on this column and others ? Using ggplot2 if possible.

Edit : I could do ggplot(subset(dataset, dataset$symbole == "AAPL" & dataset$perdayDiff > 0), aes(x=perdayDiff)) + geom_histogram(binwidth=.5) but does it mean that ggplot2 can't handle dataframe$x format ?

Wicelo
  • 2,358
  • 2
  • 28
  • 44

1 Answers1

1

You are really abusing subset() there. It should be more like

subset(dataset, symbole == "ASYMO" & perdayDiff > 0)

so, with in the plot

ggplot(subset(dataset, symbole == "ASYMO" & perdayDiff > 0), aes(x=perdayDiff)) +  
    geom_histogram(binwidth=.5)

At least I think that should work. You did not provide a proper reproducible example so there's no way to test this solution.

Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • yes your solution works I edited my post at the same time of your answer but does it mean that ggplot can't handle dataframe$x at all ? – Wicelo Sep 20 '14 at 03:35
  • Your `data=` parameter needs to be a data.frame. `dataframe$x` does not return a data.frame, it returns a vector. Vectors don't have column names so you can't do mapping with aesthetics. You could extract a data.frame with just one column with `dataframe[, "x", drop=F]` but the `$` syntax isn't appropriate here. – MrFlick Sep 20 '14 at 03:37
  • `dataframe[, "x", drop=F]` is a nice trick because you can do operations such as `-dataframe[, "x", drop=F]` which is very useful for my needs. You can't do that working with full dataframes. – Wicelo Sep 20 '14 at 04:30