简体   繁体   English

在R的单个图中使用ggplot2绘制多盒图以重复测量238个受试者

[英]plotting multiple box plots for repeated measures for 238 subjects using ggplot2 in a single graph in R

i got 238 patients. 我有238名患者。 each patient came on a monthly visit and had pre void urine volume estimation and post void urine volume estimation. 每位患者每月进行一次就诊,并进行排尿前尿液估计和排尿后尿液估计。 now this was done 23 times for every patient over almost two years.i want to plot boxplots for pre and post void urine for entire population to see trend over time (for each visit) in a single graph. 现在,在近两年的时间里,每位患者都进行了23次检查。我想为整个人群绘制尿液前后尿液的箱线图,以便在一张图中查看随时间变化的趋势(每次访问)。 the prevoid volumes are usually 300+ while post void volumes are <50. 前空隙体积通常为300+,而后空隙体积小于50。 Is there a way to do it in ggplot2 ? 有没有办法在ggplot2中做到这一点 i could only make 2 separate graphs using boxplot (one showing multiple prevoids over time in single graph with medians connected via line and another showing similar thing with post void).....in fact i could not draw even one box plot with ggplot as somehow my data doesn't have xy axis 我只能使用boxplot制作2个单独的图形(一个图形随着时间的流逝在一个图形中显示多个prevoid,中位数通过线连接,另一个图形显示与后空隙相似的东西).....实际上,我什至无法用ggplot绘制一个方框图因为我的数据没有xy轴

i tried plotrix and what not...nothing worked. 我尝试了plotrix,但没有...什么都没有。

This kind of code worked somewhat with boxplot() 这种代码与boxplot()有点配合

boxplot((mydata)[,c(13:35)], outline=FALSE, xlab= "Time in months", ylab="Prevoid urine",las=2)       ##plots box plots of columns 13-35 in one graph(23 boxplots)with outliers suppressed

p<-lines(1:23,(boxplot((mydata)[,c(13:35)],outline=FALSE, col="yellow", xlab= "Time in months", ylab="prevoid urine", las=2,ylim = c(0, 600)))$stats[3,], col="blue", lwd=3)        ##plots blue line connecting medians of 23 boxplots shaded yellow

ggplot中facet选项对您有用吗?

Consider calling sapply to calculate median then pass that vector into lines to avoid calling boxplot again for second unneeded plot: 考虑调用sapply计算median ,然后传递载体导入lines ,以避免调用boxplot再次进行第二不必要的情节:

## plots box plots of columns 13-35 in one graph(23 boxplots)with outliers suppressed
boxplot(mydata[,c(13:35)], outline=FALSE, xlab= "Time in months", ylab="Prevoid urine",las=2)   

## plots blue line connecting medians of 23 boxplots shaded yellow
lines(1:23, sapply(mydata[,c(13:35)], median), col="blue", lwd=3) 

To demonstrate with random data: 为了演示随机数据:

set.seed(8132019)
mydata <- data.frame(replicate(35, runif(500, 300, 500)))

boxplot(...)
lines(...)

绘图输出

Your data is quite messy and we don't have a bunch of information about the order of the variables. 您的数据非常混乱,我们没有大量有关变量顺序的信息。 For what you provided on an answer that you deleted, this is as much as I could do. 对于您提供的删除答案,这是我所能做的。

The data 数据

mydata <-data.frame(   serialnumber = 1:238,
                       urinarysummarypreop = c(68.75, 87.5, 81.25, 81.25, 81.25, 81.25, 87.5, 68.75, 81.25, 81.25, 81.25, 87.5, 81.25, 87.5, 81.25, 87.5, 68.75, 87.5, 87.5, 81.25, 81.25, 68.75, 68.75, 81.25, 87.5, 87.5, 87.5, 87.5, 81.25, 81.25, 81.25, 68.75, 81.25, 87.5, 81.25, 68.75, 77.08333333, 81.25, 77.08333333, 68.75, 81.25, 81.25, 68.75, 81.25, 87.5, 81.25, 68.75, 81.25, 68.75, 68.75, 81.25, 81.25, 81.25, 81.25, 68.75, 87.5, 87.5, 87.5, 87.5, 87.5, 81.25, 81.25, 87.5, 87.5, 87.5, 87.5, 81.25, 77.08333333, 87.5, 77.08333333, 87.5, 87.5, 68.75, 87.5, 87.5, 81.25, 81.25, 87.5, 87.5, 87.5, 87.5, 87.5, 87.5, 87.5, 81.25, 87.5, 81.25, 81.25, 87.5, 87.5, 87.5, 81.25, 81.25, 81.25, 87.5, 81.25, 87.5, 87.5, 77.08333333, 77.08333333, 87.5, 87.5, 87.5, 81.25, 81.25, 87.5, 77.08333333, 87.5, 87.5, 87.5, 81.25, 87.5, 81.25, 68.75, 81.25, 87.5, 87.5, 68.75, 87.5, 81.25, 87.5, 87.5, 81.25, 87.5, 87.5, 87.5, 87.5, 81.25, 77.08333333, 81.25, 87.5, 77.08333333, 87.5, 81.25, 68.75, 81.25, 81.25, 81.25, 81.25, 77.08333333, 87.5, 87.5, 87.5, 87.5, 87.5, 77.08333333, 87.5, 87.5, 77.08333333, 81.25, 77.08333333, 87.5, 77.08333333, 81.25, 87.5, 81.25, 87.5, 81.25, 87.5, 87.5, 87.5, 87.5, 81.25, 81.25, 87.5, 77.08333333, 87.5, 81.25, 81.25, 77.08333333, 81.25, 87.5, 68.75, 81.25, 87.5, 68.75, 68.75, 87.5, 77.08333333, 87.5, 87.5, 81.25, 87.5, 77.08333333, 81.25, 87.5, 87.5, 77.08333333, 77.08333333, 81.25, 87.5, 87.5, 81.25, 87.5, 87.5, 81.25, 87.5, 81.25, 87.5, 87.5, 87.5, 87.5, 81.25, 81.25, 87.5, 87.5, 87.5, 81.25, 81.25, 81.25, 81.25, 87.5, 77.08333333, 87.5, 87.5, 77.08333333, 87.5, 81.25, 87.5, 87.5, 77.08333333, 81.25, 81.25, 81.25, 87.5, 81.25, 81.25, 68.75, 87.5, 87.5, 81.25, 68.75, 87.5, 81.25, 81.25, 81.25, 87.5, 81.25),
                       urinarysummary6wks = c(26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 22.16666667, 22.16666667, 26.33333333, 22.16666667, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 16.66666667, 26.33333333, 26.33333333, 22.16666667, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 22.16666667, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 22.16666667, 26.33333333, 26.33333333, 22.16666667, 22.16666667, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 16.66666667, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 16.66666667, 22.16666667, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 22.16666667, 26.33333333, 22.16666667, 26.33333333, 22.16666667, 22.16666667, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 16.66666667, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 16.66666667, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 16.66666667, 26.33333333, 26.33333333, 22.16666667, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 22.16666667, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333, 26.33333333),
                       urinarysummary3months = c(36.16666667, 57.66666667, 48.66666667, 32, 48.66666667, 26.33333333, 44.5, 36.16666667, 48.66666667, 46.58333333, 36.16666667, 57.66666667, 57.66666667, 48.66666667, 32, 36.16666667, 36.16666667, 36.16666667, 48.66666667, 46.58333333, 36.16666667, 48.66666667, 30.5, 32, 44.5, 36.16666667, 32, 57.66666667, 48.66666667, 36.16666667, 48.66666667, 36.16666667, 57.66666667, 57.66666667, 36.16666667, 36.16666667, 48.66666667, 46.58333333, 22.16666667, 36.16666667, 57.66666667, 44.5, 30.5, 57.66666667, 48.66666667, 36.16666667, 36.16666667, 48.66666667, 36.16666667, 36.16666667, 36.16666667, 36.16666667, 32, 36.16666667, 32, 36.16666667, 32, 36.16666667, 48.66666667, 57.66666667, 36.16666667, 48.66666667, 36.16666667, 36.16666667, 36.16666667, 36.16666667, 46.58333333, 26.33333333, 36.16666667, 36.16666667, 36.16666667, 57.66666667, 48.66666667, 36.16666667, 48.66666667, 36.16666667, 48.66666667, 57.66666667, 32, 36.16666667, 46.58333333, 36.16666667, 36.16666667, 36.16666667, 48.66666667, 48.66666667, 36.16666667, 36.16666667, 46.58333333, 36.16666667, 57.66666667, 36.16666667, 36.16666667, 36.16666667, 32, 57.66666667, 48.66666667, 36.16666667, 26.33333333, 36.16666667, 48.66666667, 48.66666667, 57.66666667, 48.66666667, 57.66666667, 36.16666667, 36.16666667, 36.16666667, 36.16666667, 36.16666667, 46.58333333, 36.16666667, 48.66666667, 41.66666667, 36.16666667, 36.16666667, 36.16666667, 32, 36.16666667, 36.16666667, 48.66666667, 44.5, 36.16666667, 36.16666667, 46.58333333, 36.16666667, 36.16666667, 36.16666667, 26.33333333, 48.66666667, 36.16666667, 48.66666667, 36.16666667, 22.16666667, 32, 57.66666667, 32, 32, 48.66666667, 36.16666667, 36.16666667, 48.66666667, 36.16666667, 36.16666667, 36.16666667, 48.66666667, 48.66666667, 36.16666667, 36.16666667, 36.16666667, 36.16666667, 46.58333333, 46.58333333, 46.58333333, 36.16666667, 48.66666667, 36.16666667, 32, 36.16666667, 48.66666667, 30.5, 48.66666667, 36.16666667, 36.16666667, 48.66666667, 46.58333333, 36.16666667, 36.16666667, 46.58333333, 26.33333333, 36.16666667, 36.16666667, 32, 57.66666667, 32, 30.5, 48.66666667, 32, 57.66666667, 36.16666667, 48.66666667, 36.16666667, 32, 46.58333333, 36.16666667, 46.58333333, 48.66666667, 36.16666667, 46.58333333, 36.16666667, 48.66666667, 36.16666667, 36.16666667, 46.58333333, 36.16666667, 36.16666667, 36.16666667, 46.58333333, 57.66666667, 36.16666667, 46.58333333, 46.58333333, 48.66666667, 36.16666667, 48.66666667, 57.66666667, 36.16666667, 36.16666667, 46.58333333, 48.66666667, 32, 36.16666667, 36.16666667, 32, 16.66666667, 36.16666667, 48.66666667, 46.58333333, 36.16666667, 48.66666667, 41.66666667, 46.58333333, 36.16666667, 16.66666667, 36.16666667, 48.66666667, 32, 36.16666667, 32, 36.16666667, 57.66666667, 30.5, 36.16666667, 36.16666667, 36.16666667, 36.16666667, 36.16666667, 57.66666667 ),
                       urinaryfunctionpreop = c(100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L),
                       urinaryfunction6wks = c(16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 0, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 0, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 0, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 0, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 0, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 0, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5),
                       urinaryfunction3months = c(33.5, 66.75, 58.5, 33.5, 58.5, 16.5, 58.5, 33.5, 58.5, 33.5, 33.5, 66.75, 66.75, 58.5, 33.5, 33.5, 33.5, 33.5, 58.5, 33.5, 33.5, 58.5, 16.5, 33.5, 58.5, 33.5, 33.5, 66.75, 58.5, 33.5, 58.5, 33.5, 66.75, 66.75, 33.5, 33.5, 58.5, 33.5, 16.5, 33.5, 66.75, 58.5, 16.5, 66.75, 58.5, 33.5, 33.5, 58.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 58.5, 66.75, 33.5, 58.5, 33.5, 33.5, 33.5, 33.5, 33.5, 16.5, 33.5, 33.5, 33.5, 66.75, 58.5, 33.5, 58.5, 33.5, 58.5, 66.75, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 58.5, 58.5, 33.5, 33.5, 33.5, 33.5, 66.75, 33.5, 33.5, 33.5, 33.5, 66.75, 58.5, 33.5, 16.5, 33.5, 58.5, 58.5, 66.75, 58.5, 66.75, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 58.5, 50, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 58.5, 58.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 16.5, 58.5, 33.5, 58.5, 33.5, 16.5, 33.5, 66.75, 33.5, 33.5, 58.5, 33.5, 33.5, 58.5, 33.5, 33.5, 33.5, 58.5, 58.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 58.5, 33.5, 33.5, 33.5, 58.5, 16.5, 58.5, 33.5, 33.5, 58.5, 33.5, 33.5, 33.5, 33.5, 16.5, 33.5, 33.5, 33.5, 66.75, 33.5, 16.5, 58.5, 33.5, 66.75, 33.5, 58.5, 33.5, 33.5, 33.5, 33.5, 33.5, 58.5, 33.5, 33.5, 33.5, 58.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 33.5, 66.75, 33.5, 33.5, 33.5, 58.5, 33.5, 58.5, 66.75, 33.5, 33.5, 33.5, 58.5, 33.5, 33.5, 33.5, 33.5, 0, 33.5, 58.5, 33.5, 33.5, 58.5, 50, 33.5, 33.5, 0, 33.5, 58.5, 33.5, 33.5, 33.5, 33.5, 66.75, 16.5, 33.5, 33.5, 33.5, 33.5, 33.5, 66.75),
                       urinarybotherpreop = c(53.125, 81.25, 71.875, 71.875, 71.875, 71.875, 81.25, 53.125, 71.875, 71.875, 71.875, 81.25, 71.875, 81.25, 71.875, 81.25, 53.125, 81.25, 81.25, 71.875, 71.875, 53.125, 53.125, 71.875, 81.25, 81.25, 81.25, 81.25, 71.875, 71.875, 71.875, 53.125, 71.875, 81.25, 71.875, 53.125, 65.625, 71.875, 65.625, 53.125, 71.875, 71.875, 53.125, 71.875, 81.25, 71.875, 53.125, 71.875, 53.125, 53.125, 71.875, 71.875, 71.875, 71.875, 53.125, 81.25, 81.25, 81.25, 81.25, 81.25, 71.875, 71.875, 81.25, 81.25, 81.25, 81.25, 71.875, 65.625, 81.25, 65.625, 81.25, 81.25, 53.125, 81.25, 81.25, 71.875, 71.875, 81.25, 81.25, 81.25, 81.25, 81.25, 81.25, 81.25, 71.875, 81.25, 71.875, 71.875, 81.25, 81.25, 81.25, 71.875, 71.875, 71.875, 81.25, 71.875, 81.25, 81.25, 65.625, 65.625, 81.25, 81.25, 81.25, 71.875, 71.875, 81.25, 65.625, 81.25, 81.25, 81.25, 71.875, 81.25, 71.875, 53.125, 71.875, 81.25, 81.25, 53.125, 81.25, 71.875, 81.25, 81.25, 71.875, 81.25, 81.25, 81.25, 81.25, 71.875, 65.625, 71.875, 81.25, 65.625, 81.25, 71.875, 53.125, 71.875, 71.875, 71.875, 71.875, 65.625, 81.25, 81.25, 81.25, 81.25, 81.25, 65.625, 81.25, 81.25, 65.625, 71.875, 65.625, 81.25, 65.625, 71.875, 81.25, 71.875, 81.25, 71.875, 81.25, 81.25, 81.25, 81.25, 71.875, 71.875, 81.25, 65.625, 81.25, 71.875, 71.875, 65.625, 71.875, 81.25, 53.125, 71.875, 81.25, 53.125, 53.125, 81.25, 65.625, 81.25, 81.25, 71.875, 81.25, 65.625, 71.875, 81.25, 81.25, 65.625, 65.625, 71.875, 81.25, 81.25, 71.875, 81.25, 81.25, 71.875, 81.25, 71.875, 81.25, 81.25, 81.25, 81.25, 71.875, 71.875, 81.25, 81.25, 81.25, 71.875, 71.875, 71.875, 71.875, 81.25, 65.625, 81.25, 81.25, 65.625, 81.25, 71.875, 81.25, 81.25, 65.625, 71.875, 71.875, 71.875, 81.25, 71.875, 71.875, 53.125, 81.25, 81.25, 71.875, 53.125, 81.25, 71.875, 71.875, 71.875, 81.25, 71.875),
                       urinarybother6wks = c(31.25, 31.25, 31.25, 25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 25, 25, 31.25, 25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 25, 25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 25, 31.25, 31.25, 25, 25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 25, 25, 25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 25, 25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 25, 25, 31.25, 25, 31.25, 25, 25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 25, 25, 31.25, 31.25, 31.25, 25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25, 25, 31.25, 25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 25, 31.25, 31.25, 25, 25, 31.25, 31.25, 31.25, 25, 31.25, 31.25, 31.25, 31.25, 31.25, 31.25), urinarybother3months = c(37.5, 53.125, 43.75, 31.25, 43.75, 31.25, 37.5, 37.5, 43.75, 53.125, 37.5, 53.125, 53.125, 43.75, 31.25, 37.5, 37.5, 37.5, 43.75, 53.125, 37.5, 43.75, 37.5, 31.25, 37.5, 37.5, 31.25, 53.125, 43.75, 37.5, 43.75, 37.5, 53.125, 53.125, 37.5, 37.5, 43.75, 53.125, 25, 37.5, 53.125, 37.5, 37.5, 53.125, 43.75, 37.5, 37.5, 43.75, 37.5, 37.5, 37.5, 37.5, 31.25, 37.5, 31.25, 37.5, 31.25, 37.5, 43.75, 53.125, 37.5, 43.75, 37.5, 37.5, 37.5, 37.5, 53.125, 31.25, 37.5, 37.5, 37.5, 53.125, 43.75, 37.5, 43.75, 37.5, 43.75, 53.125, 31.25, 37.5, 53.125, 37.5, 37.5, 37.5, 43.75, 43.75, 37.5, 37.5, 53.125, 37.5, 53.125, 37.5, 37.5, 37.5, 31.25, 53.125, 43.75, 37.5, 31.25, 37.5, 43.75, 43.75, 53.125, 43.75, 53.125, 37.5, 37.5, 37.5, 37.5, 37.5, 53.125, 37.5, 43.75, 37.5, 37.5, 37.5, 37.5, 31.25, 37.5, 37.5, 43.75, 37.5, 37.5, 37.5, 53.125, 37.5, 37.5, 37.5, 31.25, 43.75, 37.5, 43.75, 37.5, 25, 31.25, 53.125, 31.25, 31.25, 43.75, 37.5, 37.5, 43.75, 37.5, 37.5, 37.5, 43.75, 43.75, 37.5, 37.5, 37.5, 37.5, 53.125, 53.125, 53.125, 37.5, 43.75, 37.5, 31.25, 37.5, 43.75, 37.5, 43.75, 37.5, 37.5, 43.75, 53.125, 37.5, 37.5, 53.125, 31.25, 37.5, 37.5, 31.25, 53.125, 31.25, 37.5, 43.75, 31.25, 53.125, 37.5, 43.75, 37.5, 31.25, 53.125, 37.5, 53.125, 43.75, 37.5, 53.125, 37.5, 43.75, 37.5, 37.5, 53.125, 37.5, 37.5, 37.5, 53.125, 53.125, 37.5, 53.125, 53.125, 43.75, 37.5, 43.75, 53.125, 37.5, 37.5, 53.125, 43.75, 31.25, 37.5, 37.5, 31.25, 25, 37.5, 43.75, 53.125, 37.5, 43.75, 37.5, 53.125, 37.5, 25, 37.5, 43.75, 31.25, 37.5, 31.25, 37.5, 53.125, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 53.125)

Reshape 重塑

We need to reshape the data and clean the variable names. 我们需要重塑数据并清理变量名称。 The column names hide 2 to 3 different variables. 列名隐藏2到3个不同的变量。 This is not the way ggplot2 likes things (that's probably why you are having trouble). 这不是ggplot2喜欢事物的方式(这可能就是您遇到麻烦的原因)。

m_data <- reshape2::melt(mydata, id.vars=c("serialnumber")) %>%
  separate(variable, into = c("urinary", "timepoint"), "bother|summary|function", remove=FALSE) %>%
  mutate(variable = str_extract(variable, "bother|summary|function"))%>%
  dplyr::select(-urinary)

Take a peak of how the data looks now 充分了解数据的外观

 serialnumber variable timepoint value
1            1  summary     preop 68.75
2            2  summary     preop 87.50
3            3  summary     preop 81.25
4            4  summary     preop 81.25
5            5  summary     preop 81.25
6            6  summary     preop 81.25

Create an ordered x axis 创建有序的x轴

Now let's put the visits in order (not sure this is the way but I have no more info so...). 现在让我们按顺序整理访问(不确定是否是这种方式,但是我没有更多信息了……)。

Warning: By doing this, I'm mixing the "summary", "bother", and "function" values 警告:这样做是要混合“ summary”,“ bother”和“ function”值

m_data <- m_data %>%
  group_by(serialnumber) %>%
  mutate(visit_num = 1:length(serialnumber))

Plot 情节

Now you can do the plot 现在您可以进行绘图了

m_data  %>%
  ggplot(aes(factor(visit_num), value, group=serialnumber)) +
  geom_boxplot(aes(factor(visit_num), value, group=visit_num))+
  geom_line(color="red", alpha=0.01)+
  theme_bw()+
  NULL

在此处输入图片说明

I'm surprised that the timepoint 4 has 0 variability...what's going on with your data ? 我很惊讶时间点4的可变性为0 ...您的数据如何处理?

Additionally I only had 9 timepoints per patient, weren't we supposed to have 23 ? 另外,每个患者只有9个时间点,难道我们不应该有23个时间点吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM