在 ggplot2 中添加多级 x 标签
Add multiple level x-label in ggplot2
我如何在 ggplot 2 中的多行上放置 x 标签。我知道我会使用如下所示的 mtext 在基本图中执行此操作,但是创建类似 x 的等效方法是什么-ggplot2 中的轴标签?
最小工作示例:
# 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)
我想操纵 x-label 使其像上图一样落在多行上,即 xaxeslist
成为 xlabel。
atop
函数是一个答案,它使您能够在 ggplot
的 xlab
中毫无困难地使用两行表达式:
ggplot(data) +
aes(x = x, y = y) +
labs(x = expression(atop("Low" %<-% "Complexity" %->% "High",
"High" %<-% "Bias" %->% "Low"))
)
然而,用这种策略获得三行表达式并不容易,一种解决方案是:
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"
)
尽管由于第二行和第三行之间的距离太大 space 而不是没有错误!
我如何在 ggplot 2 中的多行上放置 x 标签。我知道我会使用如下所示的 mtext 在基本图中执行此操作,但是创建类似 x 的等效方法是什么-ggplot2 中的轴标签?
最小工作示例:
# 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)
我想操纵 x-label 使其像上图一样落在多行上,即 xaxeslist
成为 xlabel。
atop
函数是一个答案,它使您能够在 ggplot
的 xlab
中毫无困难地使用两行表达式:
ggplot(data) +
aes(x = x, y = y) +
labs(x = expression(atop("Low" %<-% "Complexity" %->% "High",
"High" %<-% "Bias" %->% "Low"))
)
然而,用这种策略获得三行表达式并不容易,一种解决方案是:
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"
)
尽管由于第二行和第三行之间的距离太大 space 而不是没有错误!