使用带有ylim参数的barplot()时,图超出R中的轴限制

时间:2018-09-05 18:03:42

标签: r bar-chart limit yaxis

我正在尝试创建一个带有百分比的堆积条形图。我已经生成了一些用于复制的示例数据:

#Create dummy data (class is table)
one=c(1,1,.9855,1,1,.9956,.9827,.9868,.988,.9701,1,1,1)
zero=1-one
comp=as.table(rbind(one,zero));comp

#Set wanted axis limits
yax=c(max(min(comp[1,])-.1,0),1);yax

现在,以下是在我使用ylim设置轴限制之前的结果图:

#Before setting limits looks fine
par(mar=c(5,6,4,5)) #resize margins
barplot(comp,col=c("#3399CC","#CC0033"),main="Proportion of Results",
    yaxt="n",legend.text=TRUE, 
    args.legend=list(x="topright",inset=c(-0.15,.45)))
axis(2,at=pretty(comp),lab=paste0(pretty(comp)*100," %"),las=TRUE)

enter image description here

然后,当我添加ylim参数时,我的柱线会超出设置的限制。

#After setting limits it's messed up
par(mar=c(5,6,4,5)) #resize margins
barplot(comp,col=c("#3399CC","#CC0033"),main="Proportion of Results",
    yaxt="n",ylim=yax,legend.text=TRUE, 
    args.legend=list(x="topright",inset=c(-0.15,.45)))
axis(2,at=pretty(yax),lab=paste0(pretty(yax)*100," %"),las=TRUE) #Show y-axis labels as percent

enter image description here

我想根据我选择的限制截取并仅查看图的一部分。有人知道我在做什么错吗?

0 个答案:

没有答案