【问题标题】:Y axis is cut with axis functionY轴用轴功能切割
【发布时间】:2017-05-26 09:11:14
【问题描述】:

我正在使用 barplot 绘制两个变量。一切正常,除了当我添加左 y 轴时,值超出范围并被剪切。我试图查看是否应该使用par 修复图形视图,但它不起作用。 这是我的脚本

barplot(t(as.matrix(prey[, c("weasels", "voles")])), axisnames=FALSE,names.arg=NULL,yaxt= "n",beside  = TRUE)
axis(1, cex.axis=0.8, at = c(2,5,8,11,14,17,20,23,26),line=0.4,labels = c("2003","2004","2005","2006","2007","2008","2014","2015","2016"), 2)
axis(2, cex.axis=0.8,at = c(0, 5,10,15,20,25,30,35,40),labels = round(c(0, 5,10,15,20,25,30,35,40)), 2)
axis(4, cex.axis=0.8,at = c(0, 5,10,15,20,25,30),labels = round(c(0, 5,10,15,20,25,30)), 2)

我制作的情节图片:

【问题讨论】:

    标签: r bar-chart axis


    【解决方案1】:

    使用 ylim 参数更改条形图的限制:

    barplot(t(as.matrix(prey[, c("weasels", "voles")])), axisnames=FALSE,names.arg=NULL,yaxt= "n",beside  = TRUE, ylim=c(0,40))
    

    【讨论】:

    • 谢谢@Dash。如果你知道答案,我还有一个问题。我在我的条形图中添加了一个情节。我在这个图中添加了一个 y 轴,但它不适合条形图 y 轴的比例。它有点偏移。
    • 这里是脚本barplot(t(as.matrix(prey[, c("weasels", "voles")])), cex.axis=0.8,ylim=c(0,35),axisnames=FALSE,names.arg=NULL,beside = TRUE) x=axis(1, cex.axis=0.8, at = c(2,5,8,11,14,17,20,23,26),line=0.4,labels = c("2003","2004","2005","2006","2007","2008","2014","2015","2016"), 2) par(new=T) plot(prey$snow, xaxt="n", lty=3,lwd=2,col=c("black"),yaxt="n",ylab = "", xlab="", type="c",frame=F, ylim = c(0,120)) points(prey$snow, col="darkblue", lwd=2) axis(4, cex.axis=0.8,yaxp=c(0, 35, by=7),at = seq(0, 120, by=10),labels = seq(0, 120, by=10), 2)
    • 我不确定是否理解您的问题,但无论如何我都会尝试回答有关 barplot 的提示。例如barplot(c(3,4,2,5,6)) 不在 x 坐标 1、2、3、4、5 处绘制条形,而是在 x 坐标 0.7、1.9、3.1、4.3、5.5 处绘制条形图。 x 坐标可以通过x_coord <- barplot(c(3,4,2,5,6)) 获得,这对于将附加元素适合您的条形图很有用。
    • 谢谢@Dash。我会尝试看看这是否可行。
    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    相关资源
    最近更新 更多