jsakaluk
jsakaluk

Reputation: 549

Error creating facets in ggplot2-based plotting function

So I am trying to develop aggplot2based plotting function for an existing R package. Users should be able to specify a data frame (datfile), a value for where a vertical line is placed in the plot (alpha), and a grouping variable (group) for creating subplots via faceting.

Here is some example data:

#Some hypothetical data
Computed=c(0.03, 0.25, 0.74, 0.02, 0.0023, 0.43, 0.56, 0.32, 0.005, 0.0032, 0.06)
Reported.P.Value=c(0.25, 0.24, 0.74, 0.01, 0.001, 0.40, 0.56, 0.32, 0.003, 0.0032, 0.02)
Journal=c("a", "b","a", "b","a", "b","a", "b","a", "b","a")

dat=data.frame(Computed, Reported.P.Value, Journal)

The function looks like this:

plot1 <- function(datfile, alpha, group){

p <- ggplot(datfile, aes(y = Computed,x = Reported.P.Value))   

p + geom_point(size=2.5)+
  geom_vline(xintercept=alpha, color="grey60",linetype="dashed")+
  geom_abline(intercept=0, slope=1, color="grey60")+
  annotate("text", x= 0.5, y = .10, label="overestimated")+
  annotate("text", x= 0.5, y = .90, label="underestimated")+
  scale_x_continuous(name="Reported p-values", breaks=c(0.00, 0.05, 0.10, 0.25, 0.50, 0.75, 1.0))+
  scale_y_continuous(name="Computed p-values", breaks=c(0.00, 0.05, 0.10, 0.25, 0.50, 0.75, 1.0))+
  facet_grid(group ~ .)
 }

If I ignore the function, and just execute the plotting code myself (substituting in the appropriate vectors from the data frame), everything works out fine: enter image description here

But if try to run the function the plotting function itself...:

plot1(dat, 0.05, Journal)

I get this error message: Error in layout_base(data, rows, drop = drop) : At least one layer must contain all variables used for faceting

My error seems related to the problem discussed here, where the author of the accepted answer wrote:

facet_grid is even more particular required names, not even functions thereof.

Does this mean that it's not possible for me to pass variables in a data frame to the faceting option within the function? My programming background is not fantastic, and it would be a real bummer if users of my function couldn't specify a faceting variable. Any help is greatly appreciated!

Upvotes: 4

Views: 2835

Answers (1)

eipi10
eipi10

Reputation: 93761

Two changes from your original code: pass the facetting statement to facet_grid using paste to construct the appropriate string (per @aosmith's comment, a formula wrapper is not necessary), then pass the facet variable to the function as a string.

plot1 <- function(datfile, alpha, group){

  p <- ggplot(datfile, aes(y = Computed,x = Reported.P.Value))   

  p + geom_point(size=2.5)+
    geom_vline(xintercept=alpha, color="grey60",linetype="dashed")+
    geom_abline(intercept=0, slope=1, color="grey60")+
    annotate("text", x= 0.5, y = .10, label="overestimated")+
    annotate("text", x= 0.5, y = .90, label="underestimated")+
    scale_x_continuous(name="Reported p-values", breaks=c(0.00, 0.05, 0.10, 0.25, 0.50, 0.75, 1.0))+
    scale_y_continuous(name="Computed p-values", breaks=c(0.00, 0.05, 0.10, 0.25, 0.50, 0.75, 1.0))+
    facet_grid(paste(group, "~ ."))
}

plot1(dat, 0.5, "Journal")

Upvotes: 3

Related Questions