我正在使用ggplot2并尝试使用粘贴和表达式合并复杂的表达式。
例如,我试图将0.5e-6的值显示为0.5微秒,并在y轴标签中显示(5 x 10 ^ -7秒)。
到目前为止,我能够做到这一点,但不能两者兼而有之。下面给出了一个最小的工作示例。
library(ggplot2)
dat <- data.frame(
A = factor(c("O", "O", "P", "P", "Q", "Q", "O", "O", "P", "P", "Q", "Q"), levels=c("O", "O", "P", "P", "Q", "Q","O", "O", "P", "P", "Q", "Q")),
B = factor(c("P-0.1", "P-0.1", "P-0.1", "P-0.1","P-0.1", "P-0.1", "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2"), levels = c("P-0.1", "P-0.1", "P-0.1", "P-0.1","P-0.1", "P-0.1", "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2")),
X = c( 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1),
Y = c(1e-6, 1.5e-6, 1.2e-6, 1.3e-6, 0.9e-6, 1.4e-6, 3.0e-6, 2.0e-6, 3.2e-6, 2.1e-6, 2.7e-6, 1.9e-6)
)
fancy_scientific_text <- function(l) {
# turn in to character string in scientific notation
l <- format(l, scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e", "'\\1'e", l)
l <- gsub("e\\+","e",l)
# turn the 'e+' into plotmath format
l <- gsub("e", "%*%10^", l)
# print (l)
l <- gsub("\\'1[\\.0]*\\'\\%\\*\\%", "", l)
l <- gsub("\\'0[\\.0]*\\'\\%\\*\\%10\\^00", "0", l)
return(l)
}
fancy_scientific <- function(l) {
# return this as an expression
parse(text=fancy_scientific_text(l))
}
human_time_format <- function(y){
if (!is.na(y)){
substitute(paste(m, " ", mu, "s", sep=""), list(m=y*1e6))
}
}
human_times <- function(x = NULL, smbl ="sec"){
sapply(x, human_time_format)
}
human_time_format_combined <- function(y){
if (!is.na(y)){
substitute(paste(y_lab, " (", m, " ", mu, "s)", sep=""), list(m=y*1e6, y_lab=fancy_scientific(y)))
}
}
human_times_combined <- function(x = NULL, smbl ="sec"){
sapply(x, human_time_format_combined)
}
p = ggplot(data=dat, aes(x=X, y=Y, colour=A, size=A, shape=A, linetype=A, fill=B, group=interaction(A,B))) + geom_point() + geom_line() + theme_bw()
p = p + geom_point(size=4, alpha=0) + geom_point(size=4, show.legend=FALSE) + guides(shape = guide_legend(nrow=3, byrow = TRUE, keywidth = 1.5, keyheight = 1), colour = guide_legend(override.aes = list(alpha=1)))
p = p + scale_shape_manual(name="", values=c(21,22,23))
p = p + scale_colour_manual(name="", values=c("#005ccc", "#007700", "#56B4E9"))
p = p + scale_linetype_manual(name="", values=c(0,0,1))
p = p + scale_size_manual(name="", values = c(1, 1, 1))
p = p + scale_fill_manual(name = "", values = c("red", "blue"), guide = guide_legend(override.aes = list(shape = 22, size = 5)))
p0 = p + ggtitle("p0")
p1 = p + scale_y_continuous(name = "Y", labels = fancy_scientific) + ggtitle("p1")
p2 = p + scale_y_continuous(name = "Y", labels = human_times) + ggtitle("p2")
p3 = p + scale_y_continuous(name = "Y", labels = human_times_combined) + ggtitle("p3")
p0是未格式化的版本。 p1是具有科学格式的版本,p2是具有公制单位格式的版本,p3是具有p1和p2格式的预期格式。但我无法在这里捕捉科学格式。
答案 0 :(得分:1)
这基本上就是我在评论中提出的内容。你不能嵌入&#34;表达&#34;在另一个表达内。您需要组合表达式中的调用。您可以通过索引获取表达式的内容。因此,如果您更改human_time_format_combined <- function(y){
if (!is.na(y)){
substitute(paste(y_lab, " (", m, " ", mu, "s)", sep=""),
list(m=y*1e6, y_lab=fancy_scientific(y)[[1]]))
}
}
函数以提取fancy_scientific返回的表达式的内容,那么您将全部设置
p3
然后paste()
将返回
另请注意,您通常不需要?plotmath
,因为它不会完全按照您在*
表达式的上下文中执行的操作。它通常可以由human_time_format <- function(y){
if (!is.na(y)){
substitute(m*mu*"s", list(m=y*1e6))
}
}
human_time_format_combined <- function(y){
if (!is.na(y)){
substitute(y_lab~~(m*" "*mu*"s"), list(m=y*1e6, y_lab=fancy_scientific(y)[[1]]))
}
}
替换。例如
myModule.factory('myHttpInterceptor', function ($q) {
return {
// optional method
'request': function (config) {
// return my data here [200], and stop the call from going through
return config;
}
};
});