【问题标题】:R Histogram and Boxplot alignmentR 直方图和箱线图对齐
【发布时间】:2016-01-24 06:09:58
【问题描述】:

我有一个问题,我想将条形图放在直方图下,其中值的条形图正好在直方图的某个值之下。不幸的是,直方图的缩放比例与条形图中的不同,而且直方图中还有一点差距。

有没有可能重新安排?

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

【问题讨论】:

    标签: r layout histogram bar-chart par


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2015-08-27
      • 2020-06-02
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多