使用qplot绘制函数族而不复制数据 [英] Plotting family of functions with qplot without duplicating data
问题描述
给定函数族 f(x; q)( x 是参数, q 是参数)我想将此视觉化对于 q 的9个值(从0.1到0.9),从 x 函数族获取间隔 [0,1] 。到目前为止,我的解决方案是:
$ $ p $ code> 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)
qplot(rep(x ,9),f(rep(x,9),rep(q,each = 100)),color = factor(rep(q,each = 100)),
geom =line,size = I (0.9),xlab =x,ylab = expression(y = f(x)))
使用 qplot 可以快速轻松地获得视觉效果:
我担心的是,这种方法相当耗费内存,因为我需要为每个参数重复x并重复整个x范围的每个参数值。在没有这些重复的情况下生成相同图形的另一种方法是什么?
在某些时候 ggplot 会需要有可用的数据来绘制它,而包的工作方式则禁止只是做你想做的事。我想如果你知道x和y轴的限制,你可以设置一个空白图,然后遍历 q
的9个值,生成 q
,然后将 geom_line
图层添加到现有的绘图对象。但是,您必须自己为每个图层生成颜色。
如果这代表您遇到的问题的大小,我不会太担心内存占用。我们只讨论了两个向量长度为900 b
$ b
> object.size(rnorm(900))
7240字节
x
的范围似乎足以给出一个平滑的阴谋。
for
循环添加图层到 ggplot
require(ggplot2)
##复制ggplot的颜色调色板,确保有
##这样的东西已经在** ggplot **现在...
ggHueColours< - function(n,h = c(0,360)+15,1 = 65,c = 100,
方向= 1,h.start = 0){
turn < - 函数(x,h.start,方向){
(x + h.start)%% 360 *方向
}
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 = 1)
}
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 =长度(q))
(i在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(color = cols [i])
} else {
plt< - plt + geom_line(data = df,color = cols [i])
plt
给出: / p>
我将其余部分留给您 - 我不熟悉 ggplot 手动绘制图例。
Given family of functions f(x;q) (x is argument and q is parameter) I'd like to visulaize this function family on x taking from the interval [0,1] for 9 values of q (from 0.1 to 0.9). So far my solution is:
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)
qplot(rep(x,9), f(rep(x,9),rep(q,each=100)), colour=factor(rep(q,each=100)),
geom="line", size=I(0.9), xlab="x", ylab=expression("y=f(x)"))
I get quick and easy visual with qplot:
My concern is that this method is rather memory hungry as I need to duplicate x for each parameter and duplicate each parameter value for whole x range. What would be alternative way to produce same graph without these duplications?
At some point ggplot will need to have the data available to plot it and the way that package works prohibits simply doing what you want. I suppose you could set up a blank plot if you know the x and y axis limits, and then loop over the 9 values of q
, generating the data for that q
, and adding a geom_line
layer to the existing plot object. However, you'll have to produce the colours for each layer yourself.
If this is representative of the size of problem you have, I wouldn't worry too much about the memory footprint. We're only talking about a two vectors of length 900
> object.size(rnorm(900))
7240 bytes
and the 100 values over the range of x
appears sufficient to give a smooth plot.
for
loop to add layers to 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
which gives:
I'll leave the rest to you - I'm not familiar enough with ggplot to draw a legend manually.
这篇关于使用qplot绘制函数族而不复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!