简体   繁体   English

因子范围作为R中的函数变量

[英]factor range as function variable in R

I have code 我有代码

ggplot(bywells[bywells$Well_N == "KRT3",], aes(x = Date_m)) +
geom_line(aes(y = QOM, colour = "Oil, m3/month"))

which draw chart. 绘制图表。 This code works without any errors. 此代码可以正常工作。 Now I would like to elaborate it in function where bywells is variable data and "KRT3" is variable wellname , but if I write like this 现在,我想在其中bywells是变量data"KRT3"是变量wellname函数中进行详细说明,但是如果我这样写的话

simple_fun <- function(data, wellname, ...)
{
   require("ggplot2", quietly=TRUE)
   ggplot(data[data$Well_N == "wellname",], aes(x = Date_m)) +
   geom_line(aes(y = QOM, colour = "Oil, m3/month"))
}

after executing function I get error message 执行函数后,我收到错误消息

Error: Aesthetics must either be length one, or the same length as the dataProblems:QOM, Date_m

when I try 当我尝试

ggplot(data[wellname == Well_N %in% data,], aes(x = Date_m))

I get 我懂了

Error in match(x, table, nomatch = 0L) : object 'Well_N' not found

Any hints how can I define it as a variable properly? 有什么提示如何正确定义为变量?

For example reproducibility I ad small chunk of data sample: 例如,可重现性我投放了一小部分数据样本:

"Well_N";"Date_m";"QOM";"QWM";"QOMT";"BHP";"PRES";"QIW";"THPI";"QFM";"WCT"
"KRT3";2014-06-30;132;525;108;NA;NA;NA;NA;657;79
"KRT3";2014-07-30;36;120;29;NA;NA;NA;NA;156;76
"KRT3";2014-08-30;39;2.6;32.1;NA;NA;NA;NA;41.6;6.25
"KRT3";2014-09-30;211.274;749.362;174.070;NA;NA;NA;NA;960.636;78
"KRT3";2014-10-30;45;45;37.07;NA;NA;NA;NA;90;50
"KRT4";2014-08-30;108.37;1815.358;90.79;NA;NA;NA;NA;1923.73;94
"KRT4";2014-09-30;161.775;202.87;133;NA;NA;NA;NA;364;55
"KRT4";2014-10-30;30;1680;24;NA;NA;NA;NA;1710;98
"KRT4";2014-11-30;31.8;339;26;NA;NA;NA;NA;370.8;91

Type of Well_N is factor , Date_m is POSIXct , others are num . Well_N类型为factorDate_mPOSIXct ,其他为num

Here is the reproducible data (I've left only three columns which are needed, use function dput in your future questions): 这是可重现的数据(我只剩下了三列, dput在以后的问题中使用函数dput ):

bywells <-
structure(list(Well_N = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L), .Label = c("KRT3", "KRT4"), class = "factor"), Date_m = structure(c(16251, 
16281, 16312, 16343, 16373, 16312, 16343, 16373, 16404), class = "Date"), 
    QOM = c(132, 36, 39, 211.274, 45, 108.37, 161.775, 30, 31.8
    )), class = "data.frame", row.names = c(NA, -9L), .Names = c("Well_N", 
"Date_m", "QOM"))

Here is the call without the function: 这是没有功能的调用:

library(dplyr)
library(ggplot2)
library(magrittr)
qplot(x=Date_m, y=QOM, data = bywells %>% filter(Well_N == "KRT3"), geom="line")

Here is the function: 这是函数:

pfun <- function(data,wellname) {
    qplot(x=Date_m, y=QOM, data=data %>% filter(Well_N == wellname), geom="line")
}

with the corresponding call: 与相应的调用:

pfun(bywells,"KRT3")

Your error was to put the quotes on the variable wellname in the condition which filters the data. 您的错误是在过滤数据的条件下将引号放在变量wellname上。 Also your colour setting is not ok, since there is no such color called "Oil, m3/month". 另外,您的颜色设置也不正常,因为没有这样的颜色,称为“ Oil,m3 / month”。 Aim for English words such as "red", "blue", see colors() for more variants. 瞄准英语单词,例如“ red”,“ blue”,有关更多变体,请参见colors() Colour should be set outside aes argument in order for it to work. 为了使颜色起作用,应在es参数外设置颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM