我试图从可变数量的参数中生成图。代码工作正常并生成预期的图。但是,标签功能(fill = labels [i])不能按预期工作。我无法弄清楚我在这里做错了什么。所有行都具有相同的标签“C 3”,因此具有相同的颜色。
library(ggplot2)
F.Linear <- function(A, a, b, c) {
C <- (c - a*A)/b
C[C < 0] <- NA
return(C)
}
plot.linear.equations <- function(X, ...){
p <- ggplot(data.frame(x = X), aes(x = x))
arguments <- list(...)
mycolors <- c("red", "blue", "green", "orange")
labels <- c("C 1", "C 2", "C 3", "C 4")
for(i in 1:length(arguments)){
C <- unlist(arguments[i])
p <- p + stat_function(fun=F.Linear, args = list(a=C[1], b=C[2], c=C[3]), geom="area", colour="black", alpha=0.2, aes(fill = labels[i]))
}
p <- p + scale_x_continuous(name = "A") +
scale_y_continuous(name = "C") +
scale_fill_manual("Constraints", values = c("red", "blue", "green", "orange"))
return(p)
}
C1 <- c(12, 6, 20400)
C2 <- c(9, 15, 25200)
C3 <- c(6, 6, 12000)
plot.linear.equations(c(0, 3000), C1, C2, C3)