如何在hist()图中设置xlim,同时在直方图中显示变量的整个范围

QY *_*Luo 4 r

这是我在R中做的直方图,请看这里: 在此输入图像描述

这是我用来获取它的代码:

par(mfrow = c(3, 1))
hist(outcome[, 11], main = "Heart Attack", xlim = c(10,20), xlab = "30-day Death Rate")
hist(outcome[, 17], main = "Heart failure", xlim = c(10, 20), xlab = "30-day Death Rate")
hist(outcome[, 23], main = "Pneumonia", xlim = c(10,20), xlab = "30-day Death Rate")
Run Code Online (Sandbox Code Playgroud)

那么,如何修改我的代码以获得以下图表,请看这里: 在此输入图像描述

我需要显示直方图上的全部数据,同时x轴有限,从10到20,中间只有15

sgi*_*ibb 7

看一下?axis(以及?par论点xaxt),例如:

set.seed(1)
x <- rnorm(100)
## using xaxt="n" to avoid showing the x-axis
hist(x, xlim=c(-4, 4), xaxt="n")
## draw the x-axis with user-defined tick-marks
axis(side=1, at=c(-4, 0, 4))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


42-*_*42- 7

这个未经测试的代码的一些东西:

par(mfrow = c(3, 1)) #partition the graphics device, 3 stacked
# calculated a common max and min to allow horizontal alignment
# then make 3 histograms (that code seems pretty self-explanatory)
xrange <- range( c(outcome[, 11],outcome[, 17],outcome[, 23]) )
hist(outcome[, 11], main = "Heart Attack", xlim = xrange,xaxt="n", 
         xlab = "30-day Death Rate")
axis(1, at=seq(10,30,by=10), labels=seq(10,30,by=10) )
hist(outcome[, 17], main = "Heart failure", xlim = xrange,xaxt="n", 
         xlab = "30-day Death Rate")
axis(1, at=seq(10,30,by=10), labels=seq(10,30,by=10) )
hist(outcome[, 23], main = "Pneumonia", xlim = xrange, xaxt="n", 
         xlab = "30-day Death Rate")
axis(1, at=seq(10,30,by=10), labels=seq(10,30,by=10) )
Run Code Online (Sandbox Code Playgroud)