-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
5
  • 2
    It expects a data.frame: ggplot(data = data.frame(x = x), aes(x)) + geom_histogram() Please study the documentation. Commented Apr 13, 2015 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. Commented Sep 16, 2015 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. Commented Sep 16, 2015 at 21:50
  • @smci: good idea. I followed your suggestion and marked it as duplicate! Commented Sep 17, 2015 at 8:36
  • Yes, it's a community-run site, each of us can do stuff. Commented Sep 17, 2015 at 11:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.