首页 > 解决方案 > 连续数据的千层面图

问题描述

此代码适用于分类数据

library(longCatEDA)
library(colorspace)
set.seed(642531)
y <- matrix(sample(1:24, 500, replace=TRUE), 100, 5)
set.seed(963854)
times <- matrix(runif(600, 1, 3), 100, 6)

# times must be cumulative
times <- t(apply(times, 1, cumsum))
lc <- longCat(y, times=times)

par(mfrow=c(1,1), bg='white', mar=c(5.1, 4.1, 4.1, 10.1), xpd=TRUE)


cols <- longCatPlot(lc,  colScheme='heat', legendBuffer=0, groupBuffer=0,main='Individually test Varying Times of Observation')
legend(15.5, 100, legend=lc$Labels, lty=1, col=cols, lwd=2)

在此处输入图像描述

但如图所示,如果我有很多类别(超过 10 个),则图例应该是连续刻度而不是 24 个类别。在longCatEDA他们推荐longContPlot在这种情况下使用的库中,我正在尝试这样的事情:

longContPlot(lc, times, jog=TRUE, main='Individually varying times', ylab='', xlab='Days')

但它不工作..

标签: rlongitudinal

解决方案


不知道怎么做,longCatEDA但在 plotrix 中使用这个color.legend函数很容易:

library(colorspace)
library(plotrix)
set.seed(642531)
y <- matrix(sample(1:24, 500, replace=TRUE), 100, 5)
set.seed(963854)
times <- matrix(runif(600, 1, 3), 100, 6)

# times must be cumulative
times <- t(apply(times, 1, cumsum))
lc <- longCat(y, times=times)

par(mfrow=c(1,1), bg='white', mar=c(5.1, 4.1, 4.1, 10.1), xpd=TRUE)


cols <- longCatPlot(lc,  colScheme='heat', legendBuffer=0, groupBuffer=0,main='Individually test Varying Times of Observation')
plotrix::color.legend(xl=par('usr')[1]+1.05*(par('usr')[2]-par('usr')[1]),
                      xr=par('usr')[1]+1.15*(par('usr')[2]-par('usr')[1]),
                      yb=par('usr')[3]+0.20*(par('usr')[4]-par('usr')[3]),
                      yt=par('usr')[3]+0.80*(par('usr')[4]-par('usr')[3]),
                      rect.col = cols,gradient="y",
                      legend = lc$Labels[seq(1,length(lc$Labels),3)])

在此处输入图像描述


推荐阅读