R:在ggplot2中重新标记面包装标题
试试这种方法。您可以使用所需的组合直接为标签创建变量,然后使用R:在ggplot2中重新标记面包装标题,r,ggplot2,boxplot,facet-wrap,R,Ggplot2,Boxplot,Facet Wrap,试试这种方法。您可以使用所需的组合直接为标签创建变量,然后使用label\u parsed对其进行计算。代码如下: dummy_data %>% ggplot(aes(x = method, y = mse, fill = method)) + geom_boxplot() + facet_wrap(~csh, labeller = labeller(csh = c("0" = expression(paste(beta[0])),
label\u parsed
对其进行计算。代码如下:
dummy_data %>%
ggplot(aes(x = method, y = mse, fill = method)) +
geom_boxplot() +
facet_wrap(~csh, labeller = labeller(csh = c("0" = expression(paste(beta[0])),
"1" = expression(paste(beta[1])),
"2" = expression(paste(beta[2]))))) +
theme_bw() +
theme(axis.text.x=element_blank())
输出:
更新:
library(ggplot2)
library(dplyr)
#Code
dummy_data %>%
mutate(csh=paste('beta[',csh,']')) %>%
ggplot(aes(x = method, y = mse, fill = method)) +
geom_boxplot() +
facet_wrap(~csh, labeller = label_parsed) +
theme_bw() +
theme(axis.text.x=element_blank())
输出:
谢谢。一个后续问题。如果我希望标签改为beta=0、beta=1和beta=2呢?我尝试了
mutate(csh=paste('beta=',csh))
但这并没有给我想要的。@YQC让我在这一刻为你添加一个更新@YQC我已经为您添加了一个更新。我希望这对你有帮助!!!您只需要dummy_data%>%ggplot(aes(x=method,y=mse,fill=method))+geom_boxplot()+facet_wrap(~csh,labeller=label_bquote(beta[(csh)])
library(ggplot2)
library(dplyr)
#Code
dummy_data %>%
mutate(csh=paste('beta[',csh,']')) %>%
ggplot(aes(x = method, y = mse, fill = method)) +
geom_boxplot() +
facet_wrap(~csh, labeller = label_parsed) +
theme_bw() +
theme(axis.text.x=element_blank())
#Code 2
dummy_data %>%
mutate(csh=paste("beta==", csh)) %>%
ggplot(aes(x = method, y = mse, fill = method)) +
geom_boxplot() +
facet_wrap(~csh,labeller = label_parsed) +
theme_bw() +
theme(axis.text.x=element_blank())