How to set xlim in hist () graph showing the full range of a variable in a histogram

This is the histogram I made in R, see here: enter image description here

Here is the code I used to get it:

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

So, how can I change my codes to get the following graph, see here: enter image description here

I need to show the entire data range on a histogram, having a limited x axis from 10-20, only 15 in the middle

+4
source share
2 answers

Something like the lines of this unverified code:

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) )
+6
source

Take a look ?axis(and in ?parfor an argument xaxt), for example:

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

enter image description here

+3
source

All Articles