R 作为函数参数的ggplot选项数

R 作为函数参数的ggplot选项数,r,ggplot2,R,Ggplot2,我在这里使用这个函数: library(ggplot2) getp1 <- function(names, data, colors) { num_lines <- length(names) p1_colors <- colors names(p1_colors) <- names p1 <- ggplot(data.frame(x = c(0,720)), aes(x)) + stat_function(fun=data[[1]], ge

我在这里使用这个函数:

library(ggplot2)
getp1 <- function(names, data, colors) {
  num_lines <- length(names)
  p1_colors <- colors
  names(p1_colors) <- names
  p1 <- ggplot(data.frame(x = c(0,720)), aes(x)) +
    stat_function(fun=data[[1]], geom="line", aes(colour=names[1]), size=1) +
    stat_function(fun=data[[2]], geom="line", aes(colour=names[2])) +
    stat_function(fun=data[[3]], geom="line", aes(colour=names[3])) +
    stat_function(fun=data[[4]], geom="line", aes(colour=names[4])) +
    scale_x_continuous(name="") + scale_y_continuous(name="") +
    scale_colour_manual(name = "", guide = FALSE, values = p1_colors)
  return(p1)
}
库(ggplot2)

getp1完全看不到这个问题的目的,可能误解了这个问题。但是我认为你可以使用
lappy
来实现你想要的

library(ggplot2)
# your version, changed so it works for me... 
# (may already be something else than you expected?)
getp1_old <- function(names, data, colors) {
  p1 <- ggplot(data.frame(x = c(0,720)), aes(x)) +
    stat_function(fun=data[[1]], geom="line", colour=colors[1], size=1) +
    stat_function(fun=data[[2]], geom="line", colour=colors[2]) +
    stat_function(fun=data[[3]], geom="line", colour=colors[3]) +
    stat_function(fun=data[[4]], geom="line", colour=colors[4]) +
    scale_x_continuous(name="") + scale_y_continuous(name="") +
    scale_colour_manual(guide = FALSE, values = colors)
  return(p1)
}
# my version, with lapply
getp1_new <- function(names, data, colors) {
  num_lines <- length(names)
  stat_fct_combine <- lapply(1:num_lines, function(index){
    stat_function(fun=data[[index]], geom="line", colour=colors[index]) 
  })
  p1 <- ggplot(data.frame(x = c(0,720)), aes(x)) + 
    stat_fct_combine +
    scale_x_continuous(name="") + 
    scale_y_continuous(name="") +
    scale_colour_manual(guide = FALSE, values = colors)
  return(p1)
}
# reproducible example
nms <- paste0('nr', 1:6)
dta <- list(function(x) 1, function(x) 2, function(x) 3, function(x) 4, 
            function(x) 5, function(x) 6)
cols <- rep(c('red', 'green', 'black', 'blue'), length=6)
# example plots
getp1_old(nms, dta, cols)
getp1_new(nms[1:4], dta, cols)
getp1_new(nms, dta, cols)
库(ggplot2)
#你的版本,改变了,所以它适合我。。。
#(可能已经超出了您的预期?)

getp1\u old请给出
数据的示例。