我有以下代码在ggplot2的旧版2.1之前的版本中完美运行:
dd <-
structure(list(Dataset = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("train",
"validation"), class = "factor"), Iterations = c(4L, 2L, 3L,
4L, 1L, 3L), L1 = c(1e-07, 1e-07, 1e-07, 1e-06, 1e-07, 1e-06),
L2 = c(1e-05, 1e-05, 1e-05, 1e-05, 1e-05, 1e-05), Accuracy = c(0.2079478,
0.2079829, 0.2081201, 0.2086698, 0.2091378, 0.2093453), Loss = c(0.4468469,
0.446877, 0.4472973, 0.4485992, 0.4496757, 0.4499354)), .Names = c("Dataset",
"Iterations", "L1", "L2", "Accuracy", "Loss"), row.names = c(NA,
6L), class = "data.frame")
labelAxis <- function(i) {
function(values) {
sapply(values, function(x) { bquote(lambda[.(i)]==10^.(round(log10(x)))) })
}
}
ggplot(dd, aes(x=Iterations, y=Loss, color=Dataset)) +
facet_grid(. ~ L1 + L2, scales="fixed",
labeller=labeller(L1=labelAxis(1), L2=labelAxis(2), keep.as.numeric=T)) +
geom_line()
现在ggplot2弃用labeller()
keep.as.numeric=...
参数,更糟糕的是,对于上面的代码,它不再生成标签的良好数学渲染。有谁知道如何解决这个问题?
答案 0 :(得分:7)
好的,我想我已经弄明白了。编辑构面标签的新方法是使用label_bquote()
,如下所示:
ggplot(dd, aes(x=Iterations, y=Loss, color=Dataset)) +
facet_grid(. ~ L1 + L2, scales="fixed", labeller=label_bquote(
cols={lambda[1]==10^.(round(log10(L1)))}*", "*lambda[2]==10^.(round(log10(L2))))) +
geom_line()
注意看似随意的一对花括号。这是一个问题。如果没有那些大括号,R解析器会在表达式中的第二个==
运算符上窒息。