在某些时候 ggplot 需要有可用的数据来绘制它,并且包的工作方式禁止简单地做你想做的事。如果您知道 x 和 y 轴限制,我想您可以设置一个空白图,然后循环遍历 q 的 9 个值,为该 q 生成数据,并将 geom_line 图层添加到现有绘图对象。但是,您必须自己为每个图层生成颜色。
如果这代表您遇到的问题的规模,我不会太担心内存占用。我们只讨论长度为 900 的两个向量
> object.size(rnorm(900))
7240 bytes
x 范围内的 100 个值似乎足以绘制平滑图。
for 循环添加层到 ggplot
require("ggplot2")
## something to replicate ggplot's colour palette, sure there is something
## to do this already in **ggplot** now...
ggHueColours <- function(n, h = c(0, 360) + 15, l = 65, c = 100,
direction = 1, h.start = 0) {
turn <- function(x, h.start, direction) {
(x + h.start) %% 360 * direction
}
if ((diff(h) %% 360) < 1) {
h[2] <- h[2] - 360 / n
}
hcl(h = turn(seq(h[1], h[2], length = n), h.start = h.start,
direction = direction), c = c, l = l)
}
f = function(p,q=0.9) {1-(1-(p*q)^3)^1024}
x = seq(0.0,0.99,by=0.01)
q = seq(0.1,0.9,by=0.1)
cols <- ggHueColours(n = length(q))
for(i in seq_along(q)) {
df <- data.frame(y = f(x, q[i]), x = x)
if(i == 1) {
plt <- ggplot(df, aes(x = x, y = y)) + geom_line(colour = cols[i])
} else {
plt <- plt + geom_line(data = df, colour = cols[i])
}
}
plt
给出:
剩下的交给你 - 我对 ggplot 不够熟悉,无法手动绘制图例。