[英]R Histogram and Boxplot alignment
我有一個問題,我想把條形圖放在直方圖下,其中一個值的條形圖正好在直方圖的某個值之下。 不幸的是,直方圖的縮放與條形圖中的縮放不同,另外在直方圖中存在一點間隙。
有可能重新排列嗎?
# data
set.seed(4566)
a <- rnorm(100)
a <- dnorm(a)*10+1
data <- a
#data plot 2
values <- matrix(,,3)
values[1,1] <- 1
values[1,2] <- 2
values[1,3] <- 3
colnames(values) <- c('Mean','Best 50%','Worst 50%')
# layout boxplot is at the bottom
nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE), height = c(3,1))
par(mar=c(3.1, 3.1, 1.1, 2.1),oma=c(0,2,1,1))
b <- c(0,1,2,3,4,5)
hist(data,xlim = range(0:6),ylim=range(0:25),col = "blue",breaks=b)
barplot(values, horiz=T, xlim=range(0:6),ylim=range(0:3),las=1)
一種方法是在hist
調用中使用xaxs
參數
nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE), height = c(3,1))
par(mar=c(3.1, 3.1, 1.1, 2.1),oma=c(0,2,1,1))
hist(data,xlim = range(0:6),ylim=range(0:25),col = "blue", breaks=b, xaxs="i")
barplot(values, horiz=T, xlim=range(0:6),ylim=range(0:3), las=1)
這使
xaxs
參數用於計算x軸(參見?par
)。
看看它做了什么
默認的xaxs = "r"
hist(data,xlim = range(0:6),ylim=range(0:25),col = "blue", breaks=b, xaxs="r")
par("usr")
#[1] -0.24 6.24 -1.00 26.00
前兩個點給出了x軸限制 - 你可以看到它已被擴展。 要強制它保持在數據范圍內,您可以使用xaxs="i"
選項
hist(data,xlim = range(0:6),ylim=range(0:25),col = "blue", breaks=b, xaxs="i")
par("usr")
#[1] 0 6 -1 26
這與你的情節一致
barplot(values, horiz=T, xlim=range(0:6),ylim=range(0:3), las=1)
par("usr")
#[1] 0.00 6.00 -0.12 3.12
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.