Error creating faces in the build function based on ggplot2

So, I'm trying to develop a build function based on an ggplot2existing package R. Users should be able to specify a data frame (datfile), a value in which the vertical line is placed in the graph (alpha), and a grouping variable (group) to create subtitles through the cut.

Here are some sample 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 is as follows:

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 build code itself (replacing the corresponding vectors from the data frame), everything works fine: <img src = "https://i.stack.imgur.com/k9QQ5.png" alt = "enter a description of the image here ">

But if you try to run the function of the build function itself ...:

plot1(dat, 0.05, Journal)

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

, :

facet_grid - , .

, ? , , . !

+4
1

: facetting facet_grid paste, ( @aosmith, ), .

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")
+3

Source: https://habr.com/ru/post/1607190/


All Articles