我有代码
ggplot(bywells[bywells$Well_N == "KRT3",], aes(x = Date_m)) +
geom_line(aes(y = QOM, colour = "Oil, m3/month"))
绘制图表。此代码可以正常运行。现在我想在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"))
}
执行函数后我收到错误信息
Error: Aesthetics must either be length one, or the same length as the dataProblems:QOM, Date_m
当我尝试
时ggplot(data[wellname == Well_N %in% data,], aes(x = Date_m))
我得到了
Error in match(x, table, nomatch = 0L) : object 'Well_N' not found
任何提示如何正确地将其定义为变量?
例如,重复性我是一小部分数据样本:
"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
Well_N
的类型为factor
,Date_m
为POSIXct
,其他为num
。
答案 0 :(得分:1)
以下是可重复的数据(我只留下了三个需要的列,在将来的问题中使用函数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"))
这是没有功能的电话:
library(dplyr)
library(ggplot2)
library(magrittr)
qplot(x=Date_m, y=QOM, data = bywells %>% filter(Well_N == "KRT3"), geom="line")
这是功能:
pfun <- function(data,wellname) {
qplot(x=Date_m, y=QOM, data=data %>% filter(Well_N == wellname), geom="line")
}
与相应的电话:
pfun(bywells,"KRT3")
您的错误是将变量wellname
上的引号置于过滤数据的条件中。你的颜色设置也不行,因为没有这种颜色叫做“油,m3 /月”。针对英语单词,如“红色”,“蓝色”,请参阅colors()
以获取更多变体。颜色应该在aes参数之外设置,以便它可以工作。