Help with error - object 'y' not found?

4,490 views
Skip to first unread message

Paul

unread,
Jul 13, 2009, 7:53:56 PM7/13/09
to ggplot2
Hi everyone,

Just starting out with ggplot2 and liking it, but am getting an error
which I can't seem to figure out:

> ggplot(cat.va, aes(SxAg,No))+geom_freqpoly(aes(group = Vision))
Error in inherits(unit, "unit") : object 'y' not found

Isn't aes(x,y) format? In this case 'No' is the frequency count that I
want on y.

> No
[1] 7 12 11 3 4 6 4 8 5 0 0 0 19 39 6 7 16 2 3 17 2
9 6 2

I also tried a bar graph and got this:

> ggplot(cat.va, aes(SxAg,No, fill=Vision)) + geom_bar(position="dodge")
Error in pmin(y, 0) : object 'y' not found

I'm probably missing something very obvious.

Thanks a lot,

Paul

Winston Chang

unread,
Jul 13, 2009, 10:56:21 PM7/13/09
to Paul, ggplot2
Hard to tell without a reproducible example, but from the looks of it, your problem is that 'No' is not a column in the data frame cat.va -- it looks like 'No' is a separate vector.

You should make sure that cat.va is a data frame, and that the variables referenced by aes() are columns in that data frame.

If that isn't the issue, it would help if you sent a reproducible example that can be cut and pasted.

-Winston

Paul Sanfilippo

unread,
Jul 13, 2009, 11:26:19 PM7/13/09
to ggplot2, Winston Chang
Thanks for your reply Winston.

This is the data (cat.va):

SxAg Vision No
1 Any 6/12 and better 7
2 Any 6/18 to 6/60 12
3 Any <6/60 11
4 <3mth 6/12 and better 3
5 <3mth 6/18 to 6/60 4
6 <3mth <6/60 6
7 3-6mth 6/12 and better 4
8 3-6mth 6/18 to 6/60 8
9 3-6mth <6/60 5
10 >6mth 6/12 and better 0
11 >6mth 6/18 to 6/60 0
12 >6mth <6/60 0

cat.va is a dataframe:

> str(cat.va)
'data.frame': 12 obs. of 3 variables:
$ SxAg : Factor w/ 4 levels "3-6mth","<3mth",..: 4 4 4 2 2 2 1 1 1
3 ...
$ Vision : Factor w/ 3 levels "6/12 and better",..: 1 2 3 1 2 3 1 2
3 1 ...
$ No : int 7 12 11 3 4 6 4 8 5 0 ...


I was essentially wanting to create a frequency ploygon graph with
SxAg along x, the frequency (No) along y and 3 curves (Vision).

I really appreciate your help.

Thanks,

Paul

hadley wickham

unread,
Jul 14, 2009, 12:47:59 AM7/14/09
to Paul Sanfilippo, ggplot2, Winston Chang
Hi Paul,

The problem is that both geom_bar and geom_freqpoly assume that you
haven't already binned your data. If you have already binned, just
add stat = "identity" to override the default binning.
The error message could definitely be improved though.

Hadley
--
http://had.co.nz/

Paul Sanfilippo

unread,
Jul 14, 2009, 1:28:47 AM7/14/09
to hadley wickham, ggplot2
Thanks for your help Hadley,

That worked well. One more question if I can.

The chart draws a line from the last bin on the x axis back to the
first. How do I prevent that?

Regards,

Paul

poly.pdf

Paul Sanfilippo

unread,
Jul 14, 2009, 1:31:51 AM7/14/09
to hadley wickham, ggplot2
Don't worry,

Problem in the way I set up the Age variable.

Works fine now - thanks a lot for your help.

Regards,

Paul
Reply all
Reply to author
Forward
0 new messages