本文介绍了在曲面图中用N标注x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图按照处理条件和访问次数来产生一些数字结果的箱型图,每个箱子中的观察数量都放置在图下,并且也标记访问数量。以下是一些可用于说明的虚假数据,我举了两个我尝试过的方法,但并不奏效。 vis< b(b) ; - 因子(c(rep(1,150),rep(2,100),rep(3,50)),order = TRUE) val data data.sum< - ddply(data,。(vis,trt),summary, N = length(na.omit(val))) mytheme< - theme_bw()+ theme(panel.margin = unit(0,lines),strip.background = element_blank()) 下面的代码会生成一个在我想要它们的地方有N个标签的图。它通过从我创建的辅助数据集中获取摘要数据来完成此操作。但是,我无法弄清楚如何在x轴上标记访问(理想情况下,在单个框标签下),或者以其他方式(例如,将它们分隔成多个面板的线条)直观地描绘访问。 b $ b plot1 geom_boxplot(aes(x = vis:trt,y = val,group = vis: trt,color = trt),show.legend = FALSE)+ scale_x_discrete(labels = paste(data.sum $ trt,data.sum $ N,sep =\\\))+ 实验室(x =访问)+ mytheme 下图比我想要的更接近一个上面,因为它有一个很好的治疗和访问层次,并描绘访问的一个漂亮的格式。但是,对于每个面板,它都会从摘要数据中第一行抓取符合处理条件的N,因为它不会知道每个面都需要使用与该访问对应的行。 plot2 facet_wrap(〜vis,drop = FALSE,switch =x,nrow = 1)+ scale_x_discrete(labels = paste(data.sum $ trt,data.sum $ N ,sep =\\\))+ labs(x =Visit)+ mytheme trt 和之间的交互。 N 。 解决您现有的问题,您可以将 N 添加到原始数据集通过 merge 。 test = merge(data,data .sum) 然后创建一个新变量c结合 trt 和 N 。 test = transform(test,trt2 = paste(trt,N,sep =\\\)) 现在使用x轴上新的 trt2 变量并使用 scales =free_x code> in facet_wrap 以允许每个面的不同标签。 ggplot(test)+ geom_boxplot(aes(x = trt2,y = val,group = trt,color = trt),show.legend = FALSE)+ facet_wrap(〜vis,drop = FALSE,switch =x,nrow = 1,scales =free_x)+ 实验室(x =访问)+ mytheme I'm trying to produce a boxplot of some numeric outcome broken down by treatment condition and visit number, with the number of observations in each box placed under the plot, and the visit numbers labeled as well. Here's some fake data that will serve to illustrate, and I give two examples of things I've tried that didn't quite work.library(ggplot2)library(plyr)trt <- factor(rep(LETTERS[1:2],150),ordered=TRUE)vis <- factor(c(rep(1,150),rep(2,100),rep(3,50)),ordered=TRUE)val <- rnorm(300)data <- data.frame(trt,vis,val)data.sum <- ddply(data, .(vis, trt), summarise, N=length(na.omit(val)))mytheme <- theme_bw() + theme(panel.margin = unit(0, "lines"), strip.background = element_blank())The below code produces a plot that has N labels where I want them. It does this by grabbing summary data from an auxiliary dataset I created. However, I couldn't figure out how to also label visit on the x-axis (ideally, below the individual box labels), or to delineate visits visually in other ways (e.g. lines separating them into panels).plot1 <- ggplot(data) + geom_boxplot(aes(x=vis:trt,y=val,group=vis:trt,colour=trt), show.legend=FALSE) + scale_x_discrete(labels=paste(data.sum$trt,data.sum$N,sep="\n")) + labs(x="Visit") + mythemeThe plot below is closer to what I want than the one above, in that it has a nice hierarchy of treatments and visits, and a pretty format delineating the visits. However, for each panel it grabs the Ns from the first row in the summary data that matches the treatment condition, because it doesn't "know" that each facet needs to use the row corresponding to that visit.plot2 <- ggplot(data) + geom_boxplot(aes(x=trt,y=val,group=trt,colour=trt), show.legend=FALSE) + facet_wrap(~ vis, drop=FALSE, switch="x", nrow=1) + scale_x_discrete(labels=paste(data.sum$trt,data.sum$N,sep="\n")) + labs(x="Visit") + mytheme 解决方案 One workaround is to manipulate your dataset so your x variable is the interaction between trt and N.Working off what you already have, you can add N to the original dataset via a merge.test = merge(data, data.sum)Then make a new variable that is the combination of trt and N.test = transform(test, trt2 = paste(trt, N, sep = "\n"))Now make the plot, using the new trt2 variable on the x axis and using scales = "free_x" in facet_wrap to allow for the different labels per facet.ggplot(test) + geom_boxplot(aes(x = trt2, y = val, group = trt, colour = trt), show.legend = FALSE) + facet_wrap(~ vis, drop = FALSE, switch="x", nrow = 1, scales = "free_x") + labs(x="Visit") + mytheme 这篇关于在曲面图中用N标注x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-09 20:31