当我连续绘制5个箱形图时,我正在尝试制作条纹图(改变白色和灰色背景)。为什么我的代码中的背景颜色没有应用于整个图?当我单独绘制箱形图时,背景颜色会发生变化。
set.seed(42)
dev.off()
windows(width=6, height=4)
par(mfrow=c(1,5))
par(mar=c(2,4,1,1)) # bottom, left, top and right
par(bg = 'white')
boxplot(rnorm(20), ylab = "A")
title(xlab="n = 54", line=0)
par(bg = 'grey')
boxplot(rnorm(20), ylab = "B")
title(xlab="n = 54", line=0)
par(bg = 'white')
boxplot(rnorm(20), ylab = "C")
title(xlab="n = 54", line=0)
par(bg = 'grey')
boxplot(rnorm(20), ylab = "D")
title(xlab="n = 26", line=0)
par(bg = 'white')
boxplot(rnorm(20), ylab = "E")
title(xlab="n = 6", line=0)
为了澄清,我希望下图中红色矩形内的区域为灰色。
答案 0 :(得分:3)
抱歉,这不是很好,但我担心你可以做的一切。
正如您所发现的,bg仅仅控制了盒子本身的背景颜色,而不是整个绘图。该解决方案具有绘制数据两次的丑陋特征。一旦获得绘制所有内容的位置,使用它来显示背景矩形,然后再次绘制数据。但我相信这就是你要找的东西。
set.seed(42)
dev.off()
windows(width=6, height=4)
par(mfrow=c(1,5))
par(mar=c(2,4,1,1)) # bottom, left, top and right
Data = rnorm(20)
boxplot(Data, ylab = "A")
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "white")
boxplot(Data, ylab = "A", add=TRUE)
title(xlab="n = 54", line=0)
Data = rnorm(20)
boxplot(Data, ylab = "B")
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "grey")
boxplot(Data, ylab = "B", add=TRUE)
title(xlab="n = 54", line=0)
Data = rnorm(20)
boxplot(Data, ylab = "C")
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "white")
boxplot(Data, ylab = "C", add=TRUE)
title(xlab="n = 54", line=0)
Data = rnorm(20)
boxplot(Data, ylab = "D")
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "grey")
boxplot(Data, ylab = "D", add=TRUE)
title(xlab="n = 26", line=0)
Data = rnorm(20)
boxplot(Data, ylab = "E")
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "white")
boxplot(Data, ylab = "E", add=TRUE)
title(xlab="n = 6", line=0)
答案 1 :(得分:2)
这是一个ggplot
替代方案,需要较少的硬编码。
library(ggplot2)
library(gridExtra)
df <- data.frame(grp = rep(letters[1:5], c(12, 8, 11, 9, 10)), y = rnorm(50))
grp <- unique(df$grp)
cols <- c("grey80", "white")
l <- vector(length = length(grp), "list")
for(i in seq_along(grp)){
dat <- df[df$grp == grp[i], ]
col <- cols[i %% 2 + 1]
p <- ggplot(dat) +
geom_boxplot(aes(x = factor(1), y = y), fill = col) +
theme(plot.background = element_rect(fill = col),
panel.background = element_rect(fill = col),
panel.border = element_rect(fill = NA, colour = "black"),
panel.grid = element_blank()) +
scale_x_discrete(breaks = NULL) +
xlab(paste0(grp[i], "\n", "n = ", nrow(dat)))
l[[i]] <- p
}
do.call(grid.arrange, c(l, nrow = 1))
说明:
data.frame
中,并使用相关的分组变量('grp')来区分值for(i in seq_along(grp))
)df[df$grp == grp[i], ]
)i %% 2 + 1
)的奇数/偶数值的数字向量选择背景颜色,以交替灰色和白色背景。 ggplot
)theme
元素xlab
):抓住当前'grp'并计算样本大小(nrow(dat)
)l[[i]] <- p
)gridExtra::grid.arrange
呈现和排列列表中的图答案 2 :(得分:1)
这是我能想到的最好的。我不得不通过反复试验来近似每个矩形的四个角。我注释掉了三个白色矩形而没有正确调整它们的大小,但保留了代码以显示它们可以包含在所需的颜色中。
setwd('C:/Users/general1/Documents/simple R programs/')
jpeg(filename = "boxplot_background_color_with_layout.jpeg")
set.seed(1223)
par(xpd = NA, mar = c(2,4,1,1), bg = 'white')
layout(matrix(c(1,2,3,4,5), 1, 5, byrow = TRUE))
boxplot(rnorm(20), ylab = "A")
title(xlab="n = 54", line=0)
#rect(-1, -2.375, 1.585, 2, col = rgb(0,0,0,alpha=0.5), border=FALSE)
boxplot(rnorm(20), ylab = "B")
title(xlab="n = 54", line=0)
rect(-0.4, -2.229, 1.6, 2.0, col = rgb(0.5,0.5,0.5,alpha=0.5), border=FALSE)
boxplot(rnorm(20), ylab = "C")
title(xlab="n = 54", line=0)
#rect(-1, -2.375, 1.585, 2, col = rgb(0,0,0,alpha=0.5), border=FALSE)
boxplot(rnorm(20), ylab = "D")
title(xlab="n = 54", line=0)
rect(-0.4, -2.5, 1.6, 2.5, col = rgb(0.5,0.5,0.5,alpha=0.5), border=FALSE)
boxplot(rnorm(20), ylab = "E")
title(xlab="n = 54", line=0)
#rect(-1, -2.375, 1.585, 2, col = rgb(0,0,0,alpha=0.5), border=FALSE)
dev.off()