Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: could not find function "unit"

Tags:

r

ggplot2

While trying to modify theme settings this simple code gives the following error:

library(ggplot2) theme_nogrid <- theme_set(theme_update(                     plot.margin=unit(c(.25, .25, .25, .25), "in"),))  Error in do.call(theme, list(...)) : could not find function "unit" 

R gives me this error for any element that uses 'unit'. Any other settings that do not call 'unit' work fine. I am running R v.2.15.2 (64-bit Windows).

I extensively searched online about this problem and found nothing. I appreciate any suggestions to the problem.

like image 546
opv Avatar asked Feb 14 '13 22:02

opv


People also ask

Why can't r find my function?

This error usually occurs when a package has not been loaded into R via library . R does not know where to find the specified function. It's a good habit to use the library functions on all of the packages you will be using in the top R chunk in your R Markdown file, which is usually given the chunk name setup .

Why ggplot function not found?

3 if you see Error in ggplot(...) : could not find function "ggplot" , it means that the ggplot() function is not accessible because the package that contains the function ( ggplot2 ) was not loaded with library(ggplot2) . Thus you cannot use the ggplot() function without the ggplot2 package being loaded first.


1 Answers

This is closely related to, although not exactly identical to, arrow() in ggplot2 no longer supported , which says:

[the] grid [package] was loaded automatically by previous versions of ggplot[2] (making grid functions visible/accessible to the user); now it's referred to via NAMESPACE imports instead, so you need to explicitly load grid if you want to use grid functions (or [to] look at their help pages).

"explicitly load" here means library("grid") or require("grid") (grid is a base package, so doesn't need to be installed separately).

unit() is a function from the grid package, so the answer above (which was about arrow()) applies.

Alternatively you can specify grid::unit(...) or grid::arrow(...) without explicitly loading the entire package.

like image 191
Ben Bolker Avatar answered Oct 23 '22 07:10

Ben Bolker