我想将箱形图添加到底座R的折线图中.

我有一个相当大的数据集,我想创建许多不同的情节,这就是为什么我为我想要创建的每个情节创建了一个子集.

以下是我的问题的一个非常简明的版本:

cat <- rep(1:10,10)
#create 10 categories

x1 <- rnorm(cat)
x2 <- rnorm(cat)
x3 <- rnorm(cat)
#create 3 X variables


test_data<-data.frame(cat,x1,x2,x3)
#create data frame

cat <- 1:10

plot_data <- data.frame(
  y = cat,
  x1_mean = tapply(test_data$x1,test_data$cat,mean),
  x2_mean = tapply(test_data$x2, test_data$cat,mean),
  x3_mean = tapply(test_data$x3, test_data$cat,mean)
)
#create data frame for plotting
#mean for every x variable per category



plot(plot_data$y,plot_data$x1_mean,type = "b", xlab = "x", ylab = "y", col = "blue", ylim = c((-1),1))  
lines(plot_data$y,plot_data$x2_mean, type = "b",col = "green")
lines(plot_data$y,plot_data$x3_mean, type = "b",col = "red")
#plot means per category

I use the data frame data_plot to plot my line graph. And it looks like this: enter image description here

现在,我想添加每个类别(cat/y)中每个x变量的分布的盒图.

为此,我可能必须捕获数据框data_plot中的箱线图所需的信息,然后用它来绘制箱线图. 你知道怎么做吗?

谢谢!

推荐答案

我会这么做.

虽然一个类别的三个x变量的平均值可以放在相同的x坐标(1到10)上,但框图应该在不同的x坐标上绘制,以避免彼此覆盖.这可以使用三个x变量上的循环来完成,或者通过将数据转换为长格式,其中只有一个x变量和另一个变量来指示值属于哪个cat/x.我们将采用后一种方法.

# put all x variables in one column
test_data_long <- reshape(test_data, direction="long", varying=list(2:4), 
                          v.names="x", timevar="xn")
# category and variant of x (xn) together for placing boxplots along the x axis
test_data_long$cat2 <- with(test_data_long, cat + (xn - 2)/5)
# reorder so that boxplots are drawn in the correct order
test_data_long <- test_data_long[order(test_data_long$cat2), ]

第一个命令只是将数据转换为具有单个x变量和表示原始索引x(x1,x2,x3)的xn变量的长格式.

然后我们创建cat2变量,它区分xncat,并用作箱线图的x坐标.我们希望有x2个值正好在cat(1,2,...,10),向左x1个值(0.8,1.8,...,9.8)和右侧的x2个值(1.2,2.2,...,10.2),这是用cat + (xn - 2) / 5.这个间隔是专门针对三个x变量(max(xn) = 3),通常它可以是cat + (xn - (max(xn) + 1)/2) / (max(xn) + 2).

接下来,我们只需要根据cat2对数据进行重新排序,以便以我们将提供的x个值的相同顺序绘制框图.

现在是密谋本身.在一个情节中有不同的信息层,我喜欢用透明的方式 suppress 不太重要的信息.

xn_col <- palette()[4:2]
box_col <- xn_col[unique(test_data_long$xn)]
# draw boxplots in semi-transparent colors
par(mar=c(4.5, 4.5, .5, .5))
boxplot(x ~ cat2, test_data_long, at=unique(test_data_long$cat2), 
        boxwex=.15, xaxt='n', xlab='cat',
        border=paste0(box_col, '66'), col=paste0(box_col, '11'))
axis(1, at=unique(test_data$cat))

# compute mean values
data_mean <- aggregate(. ~ cat, test_data, mean)
# draw semi-transparent points under the means to make them pop out more
sapply(data_mean[, -1], points, x=data_mean$cat, 
       pch=16, col='#ffffffcc', cex=2)
# draw mean values
for (cat in 1:3) {
  lines(data_mean$cat, data_mean[[paste0('x', cat)]], type='b', 
        col=xn_col[cat], lwd=1.7, cex=1.2)
}

boxplots

R相关问答推荐

从R中的另一个包扩展S3类的正确方法是什么

变量计算按R中的行更改

为什么st_join(ob1,ob2,left = True)返回具有比ob1更多功能的sf对象?

如何 bootstrap glm回归、估计95%置信区间并绘制它?

带有叠加饼图系列的Highmap

bslib::card_header中的shine::downloadButton,图标而不是文本

如何在Chart_Series()中更改轴值的 colored颜色 ?

如何自定义3D散点图的图例顺序?

将文件保存到新文件夹时,切换r设置以不必创建目录

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

根据列A中的差异变异列,其中行由列B中的相对值标识

try 将 colored颜色 编码添加到ggploly的标题中

将二进制数据库转换为频率表

无法将条件case_when()应用于使用!!创建的新变量Mutations

是否有可能从边界中找到一个点值?

避免在图例中显示VLINS组

如果满足条件,则替换列的前一个值和后续值

如何在R中创建这些列?

如何在R中的两列以上使用联合(&U)?

根据列和行的不同组合 Select 各种单元格