代码之家  ›  专栏  ›  技术社区  ›  Mona Jalal

为什么我的柱状图中的y轴没有完全显示出来?

  •  0
  • Mona Jalal  · 技术社区  · 10 年前

    我不知道如何更改代码,使条形图中的y轴完全显示?我预计它会显示为10,因为我的数据点为9.2,但它只显示为8。知道这是怎么回事吗?

    代码如下: enter image description here

    下面是它显示的内容: enter image description here

    1 回复  |  直到 10 年前
        1
  •  1
  •   rawr Alex    10 年前

    只需设置 ylim = c(0, 10) 就像你改变了 xlim

    默认情况下,不会绘制组所在的轴,因此垂直条形图不会有x轴;水平方向没有y轴。你当然可以加上。使用的返回值 barplot :

    par(mfrow = c(2, 1))
    bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
                  ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
    
    ## this will draw the x-axis but at points 1, 2, 3, ... which is not
    ## where the centers of your bars are plotted; you get that info in bp
    axis(1)
    
    bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
                  ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
    
    ## so try again with a fancy axis, bp is a matrix containing the centers
    ## of the plotted bars
    axis(1, at = bp, labels = c('Model1','Model2'), lwd = 0, lwd.ticks = 1, tcl = -.5)
    

    enter image description here