Error in “scale_x_continuous(limits = c(0, 2))”

299 views
Skip to first unread message

Tao Gao

unread,
Mar 15, 2012, 7:53:41 AM3/15/12
to ggplot2
Dear ggplot2 community,

When i'm trying the following code

(d <- ggplot(diamonds, aes(carat, price)) +
stat_bin2d(bins = 25, colour="grey70") +
opts(legend.position = "none"))
d + scale_x_continuous(limits = c(0, 2))

in the document of ggplot2, i found an error like this:

"Error in seq.default(origin[1], max(range$x) + binwidth[1],
binwidth[1]) : invalid (to - from)/by in seq(.)"

i wonder how to fix this problem, expecting your reply. Thank you very
much!

Best wishes!

Tao

igreg

unread,
Mar 15, 2012, 4:20:54 PM3/15/12
to ggplot2

(d <- ggplot(diamonds, aes(carat, price)) +
stat_bin2d( colour="grey70") +
opts(legend.position = "none"))
d + scale_x_continuous(limits = c(0, 6))

... this one works. maybe it's because the max values are outside your
limits c(0,2)

best, greg

Tao Gao

unread,
Mar 17, 2012, 7:19:56 AM3/17/12
to ggplot2
yep, when "limits=c(0,6)" it indeed works, but i want to compare the
difference between setting scale limits and setting coordinate system
limits.

In the section 7.3.3 of the book "ggplot2 Elegant Graphics for Data
Analysis", you could find this example. Do you know how to solve this
problem?
i think the scale limits conflicts with stat_bin2d.

Brian Diggs

unread,
Mar 19, 2012, 2:28:31 PM3/19/12
to ggplot2
On 3/17/2012 4:19 AM, Tao Gao wrote:
> yep, when "limits=c(0,6)" it indeed works, but i want to compare the
> difference between setting scale limits and setting coordinate system
> limits.
>
> In the section 7.3.3 of the book "ggplot2 Elegant Graphics for Data
> Analysis", you could find this example. Do you know how to solve this
> problem?
> i think the scale limits conflicts with stat_bin2d.
>

It does. the range of carat is

> range(diamonds$carat)
[1] 0.20 5.01

If the scale limits do not include those, then the error

Error in seq.default(origin[1], max(range$x) + binwidth[1], binwidth[1]) :
invalid (to - from)/by in seq(.)

is printed, but a plot is made (because this error happens inside a try
block) but without any bins (x scale seems reasonable, y doesn't, and
there are no 2d bins).

I think I found where the problem is. stat-bin2d.r:63, min(x) should be
min(x, na.rm=TRUE). I'll work on putting together a pull request if it
is really the problem.

> On Mar 16, 4:20 am, igreg<zooro...-hi6Y0CQ0nG0@public.gmane.org> wrote:
>> (d<- ggplot(diamonds, aes(carat, price)) +
>> stat_bin2d( colour="grey70") +
>> opts(legend.position = "none"))
>> d + scale_x_continuous(limits = c(0, 6))
>>
>> ... this one works. maybe it's because the max values are outside your
>> limits c(0,2)
>>
>> best, greg
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Brian Diggs

unread,
Mar 19, 2012, 3:45:21 PM3/19/12
to ggplot2
On 3/19/2012 11:28 AM, Brian Diggs wrote:
> On 3/17/2012 4:19 AM, Tao Gao wrote:
>> yep, when "limits=c(0,6)" it indeed works, but i want to compare the
>> difference between setting scale limits and setting coordinate system
>> limits.
>>
>> In the section 7.3.3 of the book "ggplot2 Elegant Graphics for Data
>> Analysis", you could find this example. Do you know how to solve this
>> problem?
>> i think the scale limits conflicts with stat_bin2d.
>>
>
> It does. the range of carat is
>
> > range(diamonds$carat)
> [1] 0.20 5.01
>
> If the scale limits do not include those, then the error
>
> Error in seq.default(origin[1], max(range$x) + binwidth[1], binwidth[1]) :
> invalid (to - from)/by in seq(.)
>
> is printed, but a plot is made (because this error happens inside a try
> block) but without any bins (x scale seems reasonable, y doesn't, and
> there are no 2d bins).
>
> I think I found where the problem is. stat-bin2d.r:63, min(x) should be
> min(x, na.rm=TRUE). I'll work on putting together a pull request if it
> is really the problem.

Created a pull request https://github.com/hadley/ggplot2/pull/448

>> On Mar 16, 4:20 am,

>> igreg<zooro...-hi6Y0CQ0nG0-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org>

Reply all
Reply to author
Forward
0 new messages