Plot series of boxplots using base graphics in R

2020-06-06 07:24发布

问题:

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.

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)))

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))

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.

回答1:

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"))

Result:



标签: r label boxplot