R 如何为图例生成函数的返回值
我制作了一个从csv文件打印数据的函数 我想从调用函数时使用的变量名称中获取图例中的标签 对于下面的代码,R 如何为图例生成函数的返回值,r,function,R,Function,我制作了一个从csv文件打印数据的函数 我想从调用函数时使用的变量名称中获取图例中的标签 对于下面的代码,names(dataPlot1)使用图例中的名称“temp1”“temp2”“temp3”。我想把它变成“x”,“y”,“z”。我该怎么做 temperature <- function(temp1,temp2,temp3) { dataPlot1 <- data.frame(temp1,temp2,temp3) matplot(dataPlot1,axes=T,
names(dataPlot1)
使用图例中的名称“temp1”“temp2”“temp3”。我想把它变成“x”,“y”,“z”。我该怎么做
temperature <- function(temp1,temp2,temp3)
{
dataPlot1 <- data.frame(temp1,temp2,temp3)
matplot(dataPlot1,axes=T,frame=T,type="l",
xlab="time (hour)",ylab="temperature(C)",
main=names(dataPlot1))
lines(dataPlot1[1],lty=1,col="blue")
lines(dataPlot1[2],lty=2,col="red")
lines(dataPlot1[3],lty=2,col="forestgreen")
legend("topright",names(dataPlot1),lty=c(1,2,2),
col=c("blue","red","forestgreen"))
}
temperature(x,y,z)
温度您有几个选项:
命名数据:dataPlot1您有几个选项:
将数据命名为:dataPlot1可能是这样的:
temperature <- function(temp1,temp2,temp3)
{ t1 <- deparse(substitute(temp1))
t2 <- deparse(substitute(temp2))
t3 <- deparse(substitute(temp3))
dataPlot1 <- data.frame(temp1,temp2,temp3)
matplot(dataPlot1,axes=T,frame=T,type="l",
xlab="time (hour)",ylab="temperature(C)",
main=names(dataPlot1))
lines(dataPlot1[1],lty=1,col="blue")
lines(dataPlot1[2],lty=2,col="red")
lines(dataPlot1[3],lty=2,col="forestgreen")
legend("topright", c(t1,t2,t3), lty=c(1,2,2),
col=c("blue","red","forestgreen"))
温度可能是:
temperature <- function(temp1,temp2,temp3)
{ t1 <- deparse(substitute(temp1))
t2 <- deparse(substitute(temp2))
t3 <- deparse(substitute(temp3))
dataPlot1 <- data.frame(temp1,temp2,temp3)
matplot(dataPlot1,axes=T,frame=T,type="l",
xlab="time (hour)",ylab="temperature(C)",
main=names(dataPlot1))
lines(dataPlot1[1],lty=1,col="blue")
lines(dataPlot1[2],lty=2,col="red")
lines(dataPlot1[3],lty=2,col="forestgreen")
legend("topright", c(t1,t2,t3), lty=c(1,2,2),
col=c("blue","red","forestgreen"))
temperature可能我误解了这个问题(因此编辑得很糟糕),但我认为OP希望从函数本身访问传递给函数的变量的名称。i、 e.temperature(foo,bar,foobar)
将用“foo”,“bar”和“foobar”标记图例,而不将其硬编码到函数体中。如果传入带有名称的data.frame,则选项1仍然有效。姓名信息必须来自某个地方。。。我添加了第四个选项。但是,如果数据没有附加名称,则无法在图例中自动分配名称。好的,是的。更改函数以接受包含所有三个变量的单个数据帧可能比以某种方式使用get
更可取:也许我误解了这个问题(因此编辑得很糟糕),但我认为OP希望从函数本身访问传递给函数的变量的名称。i、 e.temperature(foo,bar,foobar)
将用“foo”,“bar”和“foobar”标记图例,而不将其硬编码到函数体中。如果传入带有名称的data.frame,则选项1仍然有效。姓名信息必须来自某个地方。。。我添加了第四个选项。但是,如果数据没有附加名称,则无法在图例中自动分配名称。好的,是的。更改函数以接受包含所有三个变量的单个数据帧可能比以某种方式使用get
更可取: