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

查看:268
本文介绍了使用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个数据簇,每个簇包含两个箱形图.我还希望每个群集(A,B,C,D,E)具有1个x标签,并且红色的类型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错误(图例,单位=用户",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天全站免登陆