【问题标题】:xlim and x-axis not the same in barplot条形图中的 xlim 和 x 轴不相同
【发布时间】:2016-08-23 13:23:13
【问题描述】:

我正在用以下数据的基本图形覆盖两个条形图(因为它们具有不同的比例,到目前为止,在 ggplot 上做这件事是一场噩梦):

> str(pidf)
'data.frame':   1300 obs. of  7 variables:
$ spec1  : num  0.00192 0.00213 0.00151 0.00234 0.00133 ...
$ spec2  : num  0.00241 0.00278 0.00189 0.00264 0.00155 ...
$ spec3  : num  0.00231 0.00262 0.00187 0.00274 0.00165 ...
$ spec4  : num  0.00209 0.0026 0.00166 0.00225 0.00165 ...
$ spec5  : num  0.0025 0.00285 0.00188 0.00285 0.00181 ...
$ numbers: int  1 2 3 4 5 6 7 8 9 10 ...
$ ratios : num  0.59 0.642 0.544 0.625 0.567 ...

这是我的绘图代码:

par(mar = c(5,5,2,5))
barplot(pidf$ratios, col = "skyblue", ylim = c(0, 1),
ylab = "Sequenced Base Ratios", border = NA, main = "Chlorocebus tantalus")
axis(1, at = round(seq(0, 1300, 100)))
par(new = T)
barplot(pidf$spec5, col = "chartreuse3", border = NA, ylim = c(0, 0.003),
    axes = F, xlab = NA, ylab = NA)
axis(side = 4)
mtext(side = 4, line = 3, expression(pi))
legend("topright", legend=c(expression(pi), "Ratios"), cex = 0.75,
    pch=c(15, 15), col=c("chartreuse3", "skyblue"))

当我运行此代码时,x 轴比数据短 如果我在第一个图中指定xlim = c(1,1300),那么我会得到这个

有人知道为什么会这样,以及如何解决吗?我想这可能与 par 值有关,但我需要这些以便为我在右侧绘制的轴留出空间。提前致谢!

【问题讨论】:

    标签: r plot


    【解决方案1】:

    我认为问题在于条形图不会自动以单位间隔定位。默认情况下,barplot 在条之间添加 0.2 个单位的间隙(请参阅?barplot,搜索“空格”)。例如,如果我们有 20 条,x 轴的右边缘在 x=24:

    z <- 1:20
    barplot(z)
    axis(side=1,at=c(0,24))
    

    对于您的应用程序,通过指定 space=0 可能最容易适应:

    barplot(z,space=0)
    axis(side=1,at=c(0,20))
    

    【讨论】:

      【解决方案2】:

      z &lt;- 1:20

      barplot(z,names.arg =z)

      enter image description here

      【讨论】:

        猜你喜欢
        • 2020-12-17
        • 1970-01-01
        • 1970-01-01
        • 2018-06-03
        • 1970-01-01
        • 2021-11-26
        • 2018-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多