ggplot2 gives error message inside a package but works if source the R function?

43 views
Skip to first unread message

Marcelo Bertalan

unread,
Feb 18, 2015, 3:21:29 AM2/18/15
to ggp...@googlegroups.com
I wrote an R package with many functions. Everything works. But when I run one function which uses ggplot2 and 2 data.frames for the same plot, it gives error. But if I source or do step by step it works. What is the difference? Why it doesn't work from the package ?

install_github("mbertalan/iPsychCNV")
library(iPsychCNV)
# CNV.Res and roi are loaded from the package.
PlotAllCNVs(df=CNV.Res, Name="Test.png", roi=roi)
Error: Don't know how to add o to a plot

I removed line by line and I found the problem happens in the second line:
b <- ggplot(tmp2, aes(Start, Indx)) # Until this line everything works in the package.
b <- b + geom_segment(aes(x = Start, y = Indx, xend = Stop, yend = Indx, colour=as.factor(Class)))
b <- b + scale_colour_manual(values = c("ROI" = Colors[6],"q" = Colors[5],"p" = Colors[4], "1" = Colors[1], "3" = Colors[2], "4" = Colors[3], "0"=Colors[7], "2"=Colors[9]))
b <- b + facet_wrap(~ Titles, scales = "free", ncol = NCOL)
b <- b + geom_vline(aes(xintercept = c(Start, Stop)), data=tmp2ROI, alpha=0.2)

# Error only happens here:
b <- b + geom_segment(aes(x = Start, y = Indx, xend = Stop, yend = Indx, colour=as.factor(Class))) 

But if I copy the code at github (PlotAllCNVs.R), paste in R and re-run the same command. It works, creating the plot and returning a data.frame.

I will be grateful if you can send me any help.

Ben Bond-Lamberty

unread,
Feb 18, 2015, 10:21:44 AM2/18/15
to ggplot2
I can't install your package–some of its dependencies aren't available
for R 3.1.2–and PlotAllCNVs.R is pretty convoluted, so without running
the code it's difficult to know what's going on. Have you tried
`debug(PlotAllCNVs)` and stepping through the function? Have you
examined the data frame `tmp2` immediately before the plotting step to
confirm its structure and classes are what you expect?

Ben
> --
> --
> 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.

Marcelo Bertalan

unread,
Feb 18, 2015, 10:40:04 AM2/18/15
to ggp...@googlegroups.com
Thanks Ben. I am using 3.1.2 and I could install. Be sure you have setRepositories(ind=c(1:2)).

Marcelo Bertalan

unread,
Feb 18, 2015, 10:41:35 AM2/18/15
to ggp...@googlegroups.com

To solve the problem you need to remove the dataframe from ggplot.

Passing the dataframe in ggplot seems to create a layer. Then it can not add to the plot (in the package). However, it works if the plot is geom_point (I tested it).

b <- ggplot(tmp2, aes(Start, Indx))

b <- b + geom_segment(aes(x = Start, y = Indx, xend = Stop, yend = Indx, colour=as.factor(Class)))
Error: Don't know how to add o to a plot

If you add the dataframe in geom_segment it works:

b <- ggplot() + geom_segment(data=tmp2, aes(x = Start, y = Indx, xend = Stop, yend = Indx, colour=as.factor(Class)))

For some reason, inside the package, it can not overwrite the layer. Then gives you the error:

Error: Don't know how to add o to a plot

Ben Bond-Lamberty

unread,
Feb 18, 2015, 11:01:23 AM2/18/15
to Marcelo Bertalan, ggplot2
Hi Marcelo, thanks for the setRepositories tip.

The problem is that geom_segment is being masked by a function with
the same name in the "ggbio" package, which you've loaded. To make
things work correctly, just do

b <- b + ggplot2::geom_segment(...)


Ben

Marcelo Bertalan

unread,
Feb 19, 2015, 9:50:37 AM2/19/15
to ggp...@googlegroups.com, mber...@gmail.com
Hey Ben,

Thanks for the reply. I did not tested your solution. I am not sure that is the problem as ggbio is loaded inside and outside of the package. The solution I found is to transfer the data.frame from ggplot and to geom_segment. See my previous reply for details. 

Best, 

Ben Bond-Lamberty

unread,
Feb 19, 2015, 10:08:56 AM2/19/15
to ggplot2
Whatever works for you. But FYI within that function, your call to
geom_segment was definitely referencing ggbio::geom_segment, not the
ggplot version.

Ben
Reply all
Reply to author
Forward
0 new messages