在 R 中使用 ggplot2 进行函数式编程 - NSE
functional programming with ggplot2 in R - NSE
我有一个情节,我重复了很多次,只是有不同的变量,所以我想把它变成一个函数:
getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){
sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
geom_line(aes_string(y = yvarsec), color = groupvar)
}
test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"), yvar = "severity",
yvarsec = "frequency", groupvar = "gender")
我收到一个错误:
Unknown colour name: gender
我假设这是因为颜色被评估为 color = "gender"
因为 aes_string
。
如何解决这个问题?我尝试使用 enquo()
和 !!
,但没有用。有什么建议吗?
也与此相关,我想通过参数缩放 yvarsec
。我已经完成了以下操作:
getSecPlot <- 函数(数据、xvar、yvar、yvarsec、groupvar、...){
param <- max(as.numeric(freqSevData$yvar), na.rm = TRUE)/max(as.numeric(freqSevData$yvarsec), na.rm = TRUE)
sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
geom_line(aes_string(y = as.numeric(yvarsec) * as.numeric(param)), color = groupvar) +
theme_pubclean()
}
其中return一个没有台词的情节。同样,我认为这与 NSE 和 aes_string
有关。
没有 as.numeric
我得到一个错误:
Error in yvarsec * param : non-numeric argument to binary operator
@linog
提出的解决方案
getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){
df[,"param"] <- max(as.numeric(data[, yvar]), na.rm = TRUE)/max(as.numeric(data[, yvarsec]), na.rm = TRUE)
df[,"param"] <- df[,"param"]* df[, yvarsec]
sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
geom_line(aes_string(y = "param", color = groupvar)) +
theme_pubclean()
}
test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"),
yvar = "severity", yvarsec = "frequency", groupvar = "gender")
test
我认为这是因为您的 color
参数不在 aes_string
中。您应该更正为 aes_string(y = yvarsec, color = groupvar)
.
试试下面的更正。
顺便说一下,我认为你不应该在函数调用中使用 get
,因为你不希望名称与值匹配(这是 aes_string
你)
getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){
param <- as.numeric(
freqSevData[, max(get(yvar), na.rm = TRUE)/max(get(yvarsec), na.rm = TRUE)]
)
df[,"param" := get(yvarsec)*param]
sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
geom_line(aes_string(y = "param")) +
theme_pubclean()
}
test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"), yvar = "severity",
yvarsec = "frequency", groupvar = "gender")
我有一个情节,我重复了很多次,只是有不同的变量,所以我想把它变成一个函数:
getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){
sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
geom_line(aes_string(y = yvarsec), color = groupvar)
}
test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"), yvar = "severity",
yvarsec = "frequency", groupvar = "gender")
我收到一个错误:
Unknown colour name: gender
我假设这是因为颜色被评估为 color = "gender"
因为 aes_string
。
如何解决这个问题?我尝试使用 enquo()
和 !!
,但没有用。有什么建议吗?
也与此相关,我想通过参数缩放 yvarsec
。我已经完成了以下操作:
getSecPlot <- 函数(数据、xvar、yvar、yvarsec、groupvar、...){
param <- max(as.numeric(freqSevData$yvar), na.rm = TRUE)/max(as.numeric(freqSevData$yvarsec), na.rm = TRUE)
sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
geom_line(aes_string(y = as.numeric(yvarsec) * as.numeric(param)), color = groupvar) +
theme_pubclean()
}
其中return一个没有台词的情节。同样,我认为这与 NSE 和 aes_string
有关。
没有 as.numeric
我得到一个错误:
Error in yvarsec * param : non-numeric argument to binary operator
@linog
提出的解决方案getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){
df[,"param"] <- max(as.numeric(data[, yvar]), na.rm = TRUE)/max(as.numeric(data[, yvarsec]), na.rm = TRUE)
df[,"param"] <- df[,"param"]* df[, yvarsec]
sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
geom_line(aes_string(y = "param", color = groupvar)) +
theme_pubclean()
}
test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"),
yvar = "severity", yvarsec = "frequency", groupvar = "gender")
test
我认为这是因为您的 color
参数不在 aes_string
中。您应该更正为 aes_string(y = yvarsec, color = groupvar)
.
试试下面的更正。
顺便说一下,我认为你不应该在函数调用中使用 get
,因为你不希望名称与值匹配(这是 aes_string
你)
getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){
param <- as.numeric(
freqSevData[, max(get(yvar), na.rm = TRUE)/max(get(yvarsec), na.rm = TRUE)]
)
df[,"param" := get(yvarsec)*param]
sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
geom_line(aes_string(y = "param")) +
theme_pubclean()
}
test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"), yvar = "severity",
yvarsec = "frequency", groupvar = "gender")