ggplot-将下标和第二行添加到x轴刻度标签

时间:2020-04-14 17:18:25

标签: r ggplot2 axis-labels subscript

我正在尝试创建x轴刻度标记标签,使其不包含两个下标以及第二行。我能够创建第二行以及带有下标的单行。但是,我无法同时创建两者。尝试在使用的标签代码上添加“ n \”会导致错误。

简而言之,我想创建添加了下标的x轴刻度线标签的第一行,然后向刻度线标签添加第二行。

以下代码创建数据框

    `
    n=50
    data <- function(ids) {
    obs.data <- data.frame(expand.grid(ids),Route=rbinom(n,1,.5),Cohort = sample(1:3, n, 
    replace=T),Cohorts = sample(4:6, n, replace=T), Result= runif(n, 25000, 75000))
    names(obs.data) <- c("ID","Sex","Cohort","Cohorts","Result")
    obs.data<-obs.data[order(obs.data$ID),]
    return(obs.data)
    }
    test<-data(ids=1:n)
    test$Cohorts<-ifelse(test$Cohorts==4,"Cohort 1, 2, 3",ifelse(test$Cohorts==5,"Cohort 2, 
    3","Cohort 3"))
    test$Parameter<-ifelse(test$Cohorts== "Cohort 1, 2, 3","X14",ifelse(test$Cohorts=="Cohort 2, 
    3","X43","X84"))

`

以下代码创建我想要的双行刻度标签

`AUClabels <-c(“ XYZ0-14 \ n组别1,2,3”, “ XYZ0-43 \ n第2、3组”, “ XYZ0-84 \ n第3组”)

testp1<-ggplot(test, aes(x=as.factor(Parameter), y = Result), group=interaction(Cohort,Cohorts,Sex))+
geom_point(aes(colour=factor(Sex), fill=factor(Sex),shape=factor(Sex)),size=2.75)+
scale_x_discrete(labels = AUClabels)+
scale_shape_manual(values=c(19, 1))+
scale_fill_manual(values=c("black","black"))+
scale_color_manual(values=c("black","black"))+
scale_y_continuous(limit = c(20000, 80000))+
ggtitle("")+
ylab("")+
theme(axis.title.x = element_blank())+
theme(legend.position="")+
theme(legend.title=element_blank())
testp1

`
Two lined X axis tick mark labels

此代码创建下标

' AUClabels2 <-c(表达式(XYZ [0-14]),表达式(XYZ [0-43]),表达式(XYZ [0-84]))

testp2<-ggplot(test, aes(x=as.factor(Parameter), y = Result), group=interaction(Cohort,Cohorts,Sex))+
geom_point(aes(colour=factor(Sex), fill=factor(Sex),shape=factor(Sex)),size=2.75)+
scale_x_discrete(labels = AUClabels2)+
scale_shape_manual(values=c(19, 1))+
scale_fill_manual(values=c("black","black"))+
scale_color_manual(values=c("black","black"))+
scale_y_continuous(limit = c(20000, 80000))+
ggtitle("")+
ylab("")+
theme(axis.title.x = element_blank())+
theme(legend.position="")+
theme(legend.title=element_blank())
testp2

' Subscripted X axis tick mark label

0 个答案:

没有答案