我正在尝试使用构面图绘制数据子集。它是 2 列 x4 行图。第一列的值介于 120 和 150 之间,第二列的值介于 180 和 250 之间,并且变量在数据文件中分配为较小或较大。我在将特定标签('120 <= greek symbol(alpha) <= 150'、'180 <= greek symbol(alpha) <= 250)添加到列变量(“Lesser”、“Greater”)时遇到问题. 我试着这样做:
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)
我得到“值错误==“更大”:表达式不允许比较”。我也尝试强制数据。如果我以任何方式进行标记:
data2 <- transform(data, regime = factor(regime, levels=c("Lesser", "Greater"), labels=c(expression(paste("120 <", alpha," < 150")), expression("180 < alpha < 250"))))
我得到了完整的表达,而不是想要的情节。我可能会遗漏一些东西。任何帮助都非常感谢!先感谢您..
我的样本摘录是:
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
和我使用的代码:我在帖子中以错误的方式写了前面的表达式,但我确实在代码中使用了正确的表达式。
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)
我也像以前一样尝试过 mf_labeller .. 提前谢谢你