use*_*933 6 r ggplot2 axis-labels
我将如何在 ggplot 2 中的多行上放置 x 标签。我知道我将使用如下所示的 mtext 在基本图中执行此操作,但是在 ggplot2 中创建类似 x 轴标签的等效方法是什么?
最低工作示例:
# List containing the expressions for the x-axes
xaxeslist = list(bquote("Low" %<-% "Complexity" %->% "High"),
bquote("High" %<-% "Bias" %->% "Low"),
bquote("Low" %<-% "Variance" %->% "High"))
# In base R one would use mtext to plot the desired x-label values
# mtext(do.call(expression, xaxeslist), side = 1, line = 2:4)
# Dummy values for working example
xval = seq(0, 100, by = 0.001)
yval = seq(0, 100, by = 0.001)
data <- data.frame("x"=xval,"y"=yval)
# Plot the desired output
b<-ggplot(data) +
aes(x = x, y = y) +
geom_line(size = 1L) +
labs(
x = do.call(expression, xaxeslist),
y = "Y-value",
title = "Title"
) +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank())
print(b)
Run Code Online (Sandbox Code Playgroud)
我想操纵 x 标签,使其落在多行上,如上图,即xaxeslist成为 xlabel。
atopxlab函数是一个答案,它使您能够毫无困难地在您的of中使用两行表达式ggplot:
ggplot(data) +
aes(x = x, y = y) +
labs(x = expression(atop("Low" %<-% "Complexity" %->% "High",
"High" %<-% "Bias" %->% "Low"))
)
Run Code Online (Sandbox Code Playgroud)
然而,用这样的策略获得三行表达式并不容易,一种解决方案是:
ggplot(data) +
aes(x = x, y = y) +
labs(x = expression(atop(atop(textstyle("Low" %<-% "Complexity" %->% "High"),
textstyle("High" %<-% "Bias" %->% "Low")),
"Low" %<-% "Variance" %->% "High")),
y = "Y-value",
title = "Title"
)
Run Code Online (Sandbox Code Playgroud)
虽然不是没有缺点,因为第二行和第三行之间的间距太大了!