英文:
strip.text does not work with labeller - facet_wrap ggplot2
问题
我正在使用facet_wrap
,并需要在标题中使用表达式,所以我正在使用labeller
。但是strip.text
不起作用(我希望标题加粗)。请看以下代码:
这段代码会产生加粗的标题,如预期所示:
但是,如果我使用labeller
添加表达式:
vars2plot <- c(expression(paste("Hypol. ",O[2])),expression(paste("Epil. ",PO[4]^"3-")),
expression(paste("Epil. ",NH[4]^"+")), expression(paste("Hypol. ",NO[3]^"-")))
names(vars2plot) <- vars
df1$variable <- factor(df1$variable, levels = vars, labels = vars2plot)
ggplot(df1, aes(y= scenario, x=pct)) +
geom_col(aes( fill = scenario), position = position_dodge()) +
facet_wrap(vars(variable), labeller = labeller(variable=label_parsed)) +
theme (strip.text = element_text(face = "bold"))
加粗效果不会出现:
有关如何使标题加粗的任何想法吗?
英文:
I am using facet_wrap and need expression in header so I am using labeller. But then strip.text does not work (I want the header to be in bold). see code:
sc <- c("Ref","DJ","DT","DTHW")
vars <- c("O2","PO4","NH4","NO3")
df1 <- data.frame(scenario=rep(sc,4),variable=rep(vars,each=4),pct=runif(16,-10,10))
ggplot(df1, aes(y= scenario, x=pct)) +
geom_col(aes( fill = scenario), position = position_dodge()) +
facet_wrap(vars(variable)) +
theme (strip.text = element_text(face = "bold"))
This gives bold header as expected:
But if I add the expression with labeller:
vars2plot <- c(expression(paste("Hypol. ",O[2])),expression(paste("Epil. ",PO[4]^"3-")),
expression(paste("Epil. ",NH[4]^"+")), expression(paste("Hypol. ",NO[3]^"-")))
names(vars2plot) <- vars
df1$variable <- factor(df1$variable, levels = vars, labels = vars2plot)
ggplot(df1, aes(y= scenario, x=pct)) +
geom_col(aes( fill = scenario), position = position_dodge()) +
facet_wrap(vars(variable), labeller = labeller(variable=label_parsed)) +
theme (strip.text = element_text(face = "bold"))
The bold does not appear:
Any ideas how to make the header bold?
答案1
得分: 3
如果您使用表达式,那么需要使用 plotmath
语法来使文本加粗。您不能使用字体属性来改变外观。您可以使用以下方式将您的表达式包装在 bold()
中:
vars2plot <- c(expression(paste("Hypol. ",O[2])), expression(paste("Epil. ",PO[4]^"3-")),
expression(paste("Epil. ",NH[4]^"+")), expression(paste("Hypol. ",NO[3]^"-")))
# 或者简化为 ...
# vars2plot <- expression(
# "Hypol. " ~ O[2],
# "Epil. " ~ PO[4]^"3-",
# "Epil. " ~ NH[4]^"+",
# "Hypol. " ~ NO[3]^"-")
# 现在将它们全部加粗
vars2plot <- as.expression(lapply(vars2plot, function(x) bquote(bold(.(x)))))
英文:
If you use expressions, then you need to use the plotmath
syntax to make things bold. You cannot use font properties to alter the appearance. You can wrap your expressions in bold()
with
vars2plot <- c(expression(paste("Hypol. ",O[2])),expression(paste("Epil. ",PO[4]^"3-")),
expression(paste("Epil. ",NH[4]^"+")), expression(paste("Hypol. ",NO[3]^"-")))
# or simplified ...
# vars2plot <- expression(
# "Hypol. " ~ O[2],
# "Epil. " ~ PO[4]^"3-",
# "Epil. " ~ NH[4]^"+",
# "Hypol. " ~ NO[3]^"-")
# now make them all bold
vars2plot <- as.expression(lapply(vars2plot, function(x) bquote(bold(.(x)))))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论