-2

This might be an easy question for regular ggplot users, but I'm running into some unexpected behavior between ggplot and qplot and I don't understand the reason.

I'm trying to plot a simple histogram with counts. This works well with qplot:

x <- c(1,2,3,3,4,5)
qplot(x)

samplepic

However, when I try to achieve the same result using ggplot, I get the following error message. Any suggestions of what might be the problem?

ggplot(data=x, aes(x)) + geom_histogram()
Error: ggplot2 doesn't know how to deal with data of class numeric
Sol
  • 724
  • 1
  • 5
  • 18
  • 2
    It expects a data.frame: `ggplot(data = data.frame(x = x), aes(x)) + geom_histogram()` Please study the documentation. – Roland Apr 13 '15 at 12:20
  • @smci. I think that you might be right but I am not sure. I think that it is a case where the question is different from that post ("How to plot frequency...") but you are right in that the answer to both posts is the same. – Sol Sep 16 '15 at 14:23
  • @SolLago: both questions are asking *How to plot the (histogram/) frequency count of a vector with ggplot?*, although their titles use different terms. You are correct that not all users will infer "histogram" from "frequency" let alone "frequency count". I will go add *"histogram/ frequency count"* to that question's title to canonicalize it. Now these questions are exact duplicates. – smci Sep 16 '15 at 21:50
  • @smci: good idea. I followed your suggestion and marked it as duplicate! – Sol Sep 17 '15 at 08:36
  • Yes, it's a community-run site, each of us can do stuff. – smci Sep 17 '15 at 11:05

1 Answers1

1

This is not a weird behaviour: ggplot2 simply operates on data.frame objects - and not vectors:

ggplot(data.frame(x=c(1,2,3,3,4,5)), aes(x=x)) + geom_histogram()

enter image description here

Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87