我试图使用facet plot绘制数据子集.它是2列x4行图.第一列的值介于120和150之间,第二列的值介于180和250之间,变量在数据文件中指定为较小或较大.我在向列变量("Lesser","Greater")添加特定标签('120 <=希腊符号(alpha)<= 150','180 <=希腊符号(alpha)<= 250)时遇到问题.我尝试这样做:
require(graphics)
library(ggplot2)
hp <- ggplot2(data) + ....
xlow <- paste("120 <", expression(alpha), " < 150")
xhi <- paste("180 <", expression(alpha), " < 250")
mf_labeller <- function(var, value){
value <- as.character(value)
if (var=="regime") {
value[value=="Lesser"] <- xlow
value[value=="Greater"] <- xhi
}
return(value)
}
(hp %+% data) + facet_grid(param~regime, scales="free", labeller=mf_labeller)
Run Code Online (Sandbox Code Playgroud)
我得到"错误值=="更大":表达式不允许比较".我也尝试过强制数据.如果我以任何方式进行标记:
data2 <- transform(data, regime = factor(regime, levels=c("Lesser", "Greater"), labels=c(expression(paste("120 <", alpha," < 150")), expression("180 < alpha < 250"))))
Run Code Online (Sandbox Code Playgroud)
我逐字逐句地得到了整个表达式,而不是所需的情节.我可能会遗漏一些东西.任何帮助真的很感激!! 先感谢您..
我的样本摘录如下:
regime,param,line,XX,Var,sner
Lesser,Rise,VII,AA,4.968624,0.1275248
Lesser,Rise,VII,BB,3.719405,0.08470305
Lesser,Rise,VII,CC,7.608773,0.177848
Lesser,Rise,VII,DD,9.874395,0.1367159
Lesser,Text,VII,AA,4.968624,0.1275248
Lesser,Text,VII,BB,3.719405,0.08470305
Lesser,Text,VII,CC,7.608773,0.177848
Lesser,Text,VII,DD,9.874395,0.1367159
Lesser,Chant,VII,AA,0.1771826,0.186758
Lesser,Chant,VII,BB,0.3611497,0.5484656
Lesser,Chant,VII,CC,0.7719002,0.8864444
Lesser,Chant,VIII,DD,1.829022,0.2639881
Greater,Rise,VII,AA,4.968624,0.1275248
Greater,Rise,VII,BB,3.719405,0.08470305
Greater,Rise,VII,CC,7.608773,0.177848
Greater,Rise,VII,DD,9.874395,0.1367159
Greater,Text,VII,AA,4.968624,0.1275248
Greater,Text,VII,BB,3.719405,0.08470305
Greater,Text,VII,CC,7.608773,0.177848
Greater,Text,VII,DD,9.874395,0.1367159
Greater,Chant,VII,AA,0.1771826,0.186758
Greater,Chant,VII,BB,0.3611497,0.5484656
Greater,Chant,VII,CC,0.7719002,0.8864444
Greater,Chant,VIII,DD,1.829022,0.2639881
Run Code Online (Sandbox Code Playgroud)
和我使用的代码:我在帖子中以错误的方式写了早期的表达式,但我确实在代码中使用了正确的表达式.
x <- read.table("sample.csv", header=T, sep=',')
require(graphics)
library(ggplot2)
ppi <- 300
png("figure.png", width=6*ppi, height=6*ppi, res=ppi)
hp <- ggplot(data=x,aes(x=XX, y=Var, colour=line, group = line)) + geom_errorbar(aes(ymin=Var-sner, ymax=Var+sner, colour=line), width=.3) + geom_line(aes(ymax=Var+sner), size=0.7) + geom_point(aes(ymax=Var+sner), shape=21, size=2,fill="white") + theme_bw() + theme(axis.text.x = element_text(angle=90,vjust=0.25), panel.grid.minor=element_blank(), panel.grid.major=element_blank(), panel.background =element_blank(), legend.position="none" ) + scale_colour_hue(l=40)
xlow <- expression(paste("120 <",alpha," < 150"))
xhi <- expression(paste("180 <", alpha," < 250"))
.. earlier code block as alternative here...
data2 <- transform(x, regime = factor(regime, levels=c("Lesser", "Greater"), labels=c( bquote(120<.(alpha)~phantom()<150), bquote(180<.(alpha)~phantom()<250) )
(hp %+% data2) + facet_grid(param~regime, scales="free", labeller=label_bquote)
Run Code Online (Sandbox Code Playgroud)
我也和mf_labeller一样尝试过..提前谢谢你
你想要用于plotmath表达式的形式是
expression(120 <= alpha~phantom() <= 150)
expression(180 <= alpha~phantom() <= 250)
Run Code Online (Sandbox Code Playgroud)
您可以通过这种方式或间接获得
parse(text="120 <= alpha~phantom() <= 150")
parse(text="180 <= alpha~phantom() <= 250")
Run Code Online (Sandbox Code Playgroud)
最简单的方法是将因子的级别设置为这些字符串并使用该label_parsed函数.
用mtcars数据模拟,因为你没有提供可重复的例子:
dat <- mtcars
dat$regime <- factor(dat$am)
levels(dat$regime) <- list("120 <= alpha~phantom() <= 150" = "0",
"180 <= alpha~phantom() <= 250" = "1")
ggplot(aes(x=wt, y=mpg), data=dat) +
geom_point() +
facet_grid(cyl ~ regime, labeller=label_parsed)
Run Code Online (Sandbox Code Playgroud)

如果您真的想使用自己的贴标机,并且不想更改基础数据的因子级别,那么您也可以这样做.从具有"较小"和"较大"级别的模拟数据开始:
dat <- mtcars
dat$regime <- factor(dat$am, levels=c(0,1), labels=c("Lesser", "Greater"))
Run Code Online (Sandbox Code Playgroud)
那么贴标机功能和ggplot代码将是:
mf_labeller <- function(var, value){
value <- as.character(value)
if (var=="regime") {
value[value=="Lesser"] <- "120 <= alpha~phantom() <= 150"
value[value=="Greater"] <- "180 <= alpha~phantom() <= 250"
value <- lapply(value, function(x) parse(text=x))
}
return(value)
}
ggplot(aes(x=wt, y=mpg), data=dat) +
geom_point() +
facet_grid(cyl ~ regime, labeller=mf_labeller)
Run Code Online (Sandbox Code Playgroud)
该图看起来一样.
我们没有您的数据可供测试(也没有您正在使用的代码),但之前的点阵绘图经验告诉我,可能需要和中bquote的一个或某些组合。尝试 :sapplyas.expression
..., labels=c( bquote(120<.(alpha)~phantom()<150), bquote(180<.(alpha)~phantom()<250) )
Run Code Online (Sandbox Code Playgroud)
在代码的第一部分中,我不认为您应该expression在内部使用paste,而是首先构建标签,然后构建表达向量。
如果你感到绝望,你可以先构建情节,然后强迫标签变得不同。如何使用网格编辑 ggplot2 对象以将数学表达式添加到构面标签?