7

这是我在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")

那么,如何修改我的代码以获得下图,请参见此处: 在此处输入图像描述

我需要在直方图上显示全部数据范围,同时在 10-20 之间有一个有限的 x 轴,中间只有 15 个

4

2 回答 2

8

类似于此未经测试的代码的内容:

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) )
于 2013-10-15T15:51:39.777 回答
6

看看?axis(和论据),例如?parxaxt

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

在此处输入图像描述

于 2013-10-15T07:51:07.713 回答