使用 R 中的基本图形绘制一系列箱线图 [英] Plot series of boxplots using base graphics in R

查看:21
本文介绍了使用 R 中的基本图形绘制一系列箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单个图表中绘制 R 中的多个箱线图,并将它们成对分组.我是 R 的初学者,虽然有几个线程处理同一个主题(R 中的多个箱线图),但我找不到解决此问题的综合方法.我只想尽可能使用基本图形.

I would like to plot multiple boxplots in R in a single graph and group them by pairs. I am a beginner in R and although several threads deal with the same subject (multiple boxplots in R), I could not find a comprehensive way to to this. I would like to use base graphics only if possible.

我有 10 组值,它们都包含 30 个值(可以是任何正值).在这 10 个集合中,5 个属于 1 型,另外 5 个属于 2 型.我的目标是有十个箱线图(每组值一个),并将每组类型 1 与一组类型 2 分组在一个图表中.最后,我想要 5 组数据,每组包含两个箱线图.我还希望每个集群有 1 个 x-label(A、B、C、D、E),并且类型 1 的数据为红色,类型 2 的数据为绿色.

I have 10 sets of values, all of them containing 30 values (which can be any positive value). Within those 10 sets, 5 are of type 1 and the 5 other are of type 2. My objective is to have ten boxplots (one per set of values) and group each set of type 1 with a set of type 2 in a single graph. In the end, I would like to have 5 clusters of data, each of them containing two boxplots. I would also like to have 1 x-label per cluster (A,B,C,D,E) and to have the data of type 1 in red and the data of type 2 in green.

到目前为止,我的代码是:

So far my code is:

A1data <- read.table("A1data.csv",header=TRUE,sep=";")
B1data <- read.table("B1data.csv",header=TRUE,sep=";")
C1data <- read.table("C1data.csv",header=TRUE,sep=";") 
D1data <- read.table("D1data.csv",header=TRUE,sep=";")
E1data <- read.table("E1data.csv",header=TRUE,sep=";")
A2data <- read.table("A2data.csv",header=TRUE,sep=";")
B2data <- read.table("B2data.csv",header=TRUE,sep=";")
C2data <- read.table("C2data.csv",header=TRUE,sep=";")
D2data <- read.table("D2data.csv",header=TRUE,sep=";")
E2data <- read.table("E2data.csv",header=TRUE,sep=";")

A1 <- 100*(A1data$x-A1data$y)/A1data$x
B1 <- 100*(B1data$x-B1data$y)/B1data$x
C1 <- 100*(C1data$x-C1data$y)/C1data$x
D1 <- 100*(D1data$x-D1data$y)/D1data$x
E1 <- 100*(E1data$x-E1data$y)/E1data$x
A2 <- 100*(A2data$x-A2data$y)/A1data$x
B2 <- 100*(B2data$x-B2data$y)/B1data$x
C2 <- 100*(C2data$x-C2data$y)/C1data$x
D2 <- 100*(D2data$x-D2data$y)/D1data$x
E2 <- 100*(E2data$x-E2data$y)/E1data$x

A <- cbind(A1,A2)
B <- cbind(B1,B2)
C <- cbind(C1,C2)
D <- cbind(D1,D2)
E <- cbind(E1,E2)

test <- cbind(A,B,C,D,E)
boxplot(test,col=c(2,3),legend(1000,10,c("type 1","type 2)))

这会产生错误strwidth(legend, units = "user", cex = cex, font = text.font) 中的错误:尚未调用 plot.new".但是,如果我将最后一行更改为:

Which produces the error "Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet". However, if I change the last line with:

boxplot(test,col=c(2,3))

我获得了 10 个正确着色的箱线图,但我找不到将它们成对分组的方法,也找不到正确的图例.有没有一种简单的方法可以做到这一点,还是我需要以不同的方式组织我的数据?如果是这样,谢谢你指出正确的方向.

I obtain 10 boxplots correctly colored but I cannot find a way to group them by pairs, nor to put the correct legend. Is there a simple way to do this or do I need to organize my data differently? If so, thank you for pointing out the right direction.

推荐答案

您需要使用公式表示,并且可能更好地 rbind 所有这些数据帧,而不是 cbind.

You need to use a formula representation and it's probably better to rbind all of these dataframes rather than cbind.

# sample data
A1 <- rnorm(100,1)
B1 <- rnorm(100,2)
C1 <- rnorm(100,3)
D1 <- rnorm(100,4)
E1 <- rnorm(100,5)
A2 <- rnorm(100,3)
B2 <- rnorm(100,4)
C2 <- rnorm(100,5)
D2 <- rnorm(100,6)
E2 <- rnorm(100,7)

dflist <- list(A1=A1,B1=B1,C1=C1,D1=D1,E1=E1,A2=A2,B2=B2,C2=C2,D2=D2,E2=E2)
out <- data.frame(test=do.call(c,dflist))
out$group1 <- rep(1:10,times=sapply(dflist,function(x) length(x)))

# plot
boxplot(test~group1, data=out, at = c(seq(1,13,by=3),seq(2,14,by=3)),
    names=NA, col=rep(c("red","blue"),each=5))
axis(1,at=seq(1.5,13.5,by=3),labels=LETTERS[1:5])
legend(x=1, y=9, legend=c("Type 1","Type 2"), fill=c("red","blue"))

结果:

这篇关于使用 R 中的基本图形绘制一系列箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆