我正在尝试添加一个带有变量名称的双行x轴标签,其中包含希腊字母和我的boxplot图表的计数(例如:α螺旋\ nn = 1000),类似于此处接受的答案发布:
Combining `expression()` with `\n`
我使用bquote和atop获得了换行符和希腊字母,但是标签和计数之间存在差距,所以我更喜欢使用引用帖子中描述的unicode方法。
a <- runif(500)
b <- runif(500)
c <- c(a, b)
variable <- factor(rep(1:3, c(1000, 500, 500 ))) #combined, helix, loop
protein <- factor(rep(letters[1:3], c(1000, 500, 500))) #protA, protB, protC (there are different counts of each protein type for each variable, but I couldn't figure out how to generate that).
mydata <- data.frame(variable, c(a,b,c))
names(mydata) <- c("variable", "value")
mydata$prot <- as.character(protein)
mydata.stat<-boxplot(value~variable, data=mydata, plot=FALSE)
mydata.n<-mydata.stat$n
count1 <- mydata.n[1]
count2 <- mydata.n[2]
count3 <- mydata.n[3]
xlab1 <-bquote(atop("combined", "(n="*.(count1)*")"))
xlab2 <-bquote(atop(alpha*" helix", "(n="*.(count2)*")"))
xlab3 <-bquote(atop(beta*alpha*" loops", "(n="*.(count3)*")"))
p <- ggplot(mydata, aes(x=variable, y=value, group=(variable)))
p <- p + geom_boxplot(notch=addnotch, outlier.size=0, fill = NA, col =
p <- p + scale_x_discrete(labels=c(xlab1, xlab2, xlab3))
ggsave(p, file="boxplot.pdf" , device=cairo_pdf, width=8, height=8)
使用相同的数据,我也尝试根据蛋白质列创建刻面图,但只使用前3个标签。
mydata.stat<-boxplot(value~protein+variable, data=mydata, plot=FALSE)
mydata.n<-mydata.stat$n
mydata <- ddply(mydata,.(protein,variable),transform,N=length(protein))
mydata$label <- as.character(mydata$N)
count1 <- mydata.n[1]
count2 <- mydata.n[2]
count3 <- mydata.n[3]
count4 <- mydata.n[4]
count5 <- mydata.n[5]
count6 <- mydata.n[6]
count7 <- mydata.n[7]
count8 <- mydata.n[8]
count9 <- mydata.n[9]
Combined1 <-bquote(atop("Prot1", "(n="*.(count1)*")"))
Combined2 <-bquote(atop("Prot2", "(n="*.(count2)*")"))
Combined3 <-bquote(atop("Prot3", "(n="*.(count3)*")"))
Helix1 <-bquote(atop("Prot1", "(n="*.(count4)*")"))
Helix2 <-bquote(atop("Prot2", "(n="*.(count5)*")"))
Helix3 <-bquote(atop("Prot3", "(n="*.(count6)*")"))
Loop1 <-bquote(atop("Prot1", "(n="*.(count7)*")"))
Loop2 <-bquote(atop("Prot2", "(n="*.(count8)*")"))
Loop3 <-bquote(atop("Prot3", "(n="*.(count9)*")"))
xaxislabs <- c (Combined1, Combined2, Combined3, Helix1, Helix2, Helix3, Loop1, Loop2, Loop3)
p <- ggplot(datacolor, aes(x=protein, y=value, group=(protein)))
p <- p + geom_boxplot(notch=addnotch, outlier.size=0, fill = NA, col = "black", lwd = 0.5)
p <- p + facet_grid(.~variable, scales = "free_x")
p <- p + scale_x_discrete(labels=c(xaxislabs))
ggsave(p, file="boxplot-facet.pdf" , device=cairo_pdf, width=8, height=8)
对不起,很长的帖子。我很感激任何建议。