scale_y_continuous oddity/error

989 views
Skip to first unread message

IsotopesforBreakfast

unread,
Apr 7, 2016, 11:25:18 AM4/7/16
to ggplot2
Hello all, 

I just ran into an odd situation that I haven't figured out yet. On adding a scale_y_continuous (or scale_x for that matter) line to a ggplot call, I get a weird  <ScaleContinuousPosition> bit in the console, and no plot is drawn. 
 
For example, using the CO2 dataframe from base:

data(CO2)

plot0 <- ggplot(data=CO2, aes(x=conc, y=uptake )) + geom_point()
plot0 <- scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10))
plot0

returns the following in console, with no plot:
:
<ScaleContinuousPosition>
 Range:  
 Limits:    0 --  100

OTOH, if I do this:

plot1 <- ggplot(df, aes(x=runif(100), y=1:100)) + geom_point()
plot1 <- plot1 + scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10))
plot1

all is fine. 

I havent spent a lot of time hunting this down, but it is killing me not being able to complete a number of tasks. First time ever seeing this type of "error", even though its not officially an error, i.e, "no trackeback available". 

I did find that this value, <ScaleContinuousPosition>, does appear in the examples portion of the following docs page, but I'm not clever enough to know what it might mean. 


Any ideas?

thanks!
isotope

Brandon Hurr

unread,
Apr 7, 2016, 11:53:49 AM4/7/16
to IsotopesforBreakfast, ggplot2
The problem is that you're not '+' it to your plot. A scale is meaningless without plot data and a geom. 
ggplot(data=CO2, aes(x=conc, y=uptake )) +
    geom_point() +
    scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10)) 

Does that work? 


Dennis Murphy

unread,
Apr 7, 2016, 3:41:49 PM4/7/16
to IsotopesforBreakfast, ggplot2
ggplot2 lets you save reusable code chunks as R objects. To add to
Brandon's comments:

#--------
plot0 <- ggplot(data=CO2, aes(x=conc, y=uptake )) + geom_point()
plot0 <- scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10))
plot0
#-------

failed because you replaced a ggplot object in the first line with a
ggplot2 code segment in the second. When you call plot0, you get the
print method associated with the code chunk. It is not an object of
class "gg" and "ggplot" (see below).

#------
plot1 <- ggplot(df, aes(x=runif(100), y=1:100)) + geom_point()
plot1 <- plot1 + scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10))
plot1
#------

works because you added a chunk of ggplot2 code to an existing ggplot
object, which adds to the ggplot object.

You can combine the two with

#------
library(ggplot2)
data(CO2)

plot0 <- ggplot(data=CO2, aes(x=conc, y=uptake )) + geom_point()
plot1 <- scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10))
plot0 + plot1
#------

This saves the second ggplot2 code chunk as a separate object (which
you did the first time) and adds it to the existing plot0 object in
the last line. To see the distinction among the three objects created
above,

> class(plot1)
[1] "ScaleContinuousPosition" "ScaleContinuous"
[3] "Scale" "ggproto"

> class(plot0)
[1] "gg" "ggplot"
> class(plot0 + plot1)
[1] "gg" "ggplot"

Dennis
>

On Thu, Apr 7, 2016 at 8:25 AM, IsotopesforBreakfast
<sant...@columbus.rr.com> wrote:
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages