barplot Y-Axis中的不同起点(不是0)?

时间:2022-10-19 14:06:06

I am trying to create a simple bar graph including two bars showing the average math scores of two groups of students. The averages are 363.2 and 377.4. creating bar graph is simple. What I'd like to do is to have my y-axis starts from 340 rather than 0. I do know how to change the limits of y-axis, but the issue is when I change the limit of y-axis to c(340, 380), R still draws the whole bar which most of it is below the x-axis!

我正在尝试创建一个简单的条形图,其中包括两个条形图,显示两组学生的平均数学分数。平均值为363.2和377.4。创建条形图很简单。我想做的是让我的y轴从340而不是0开始。我知道如何改变y轴的极限,但问题是当我将y轴的极限改为c时( 340,380),R仍然绘制整个条,其大部分位于x轴下方!

Here is my code:

这是我的代码:

barplot(c(363.2, 377.4), beside = T, ylim = c(340,380), col = c("orange", "blue"))

I've also attached my plot:

我还附上了我的情节:

2 个解决方案

#1


9  

Adding xpd=FALSE and re-adding the horizontal axis works, sort of:

添加xpd = FALSE并重新添加水平轴工作,排序:

b <- barplot(c(363.2, 377.4), beside = TRUE, 
   ylim = c(340,380), col = c("orange", "blue"),xpd=FALSE)
axis(side=1,at=b,labels=c("group 1", "group 2"))
box(bty="l")

I claim (I can't point you to a definitive reference, although Googling "bar plot zero axis" seems to come up with useful stuff; perhaps others will chime in) that it is bad practice to draw bar graphs where the vertical axis does not include zero: the argument is that the viewer will assume that bar graphs are anchored to the origin (this argument is more commonly made when explaining why R doesn't make it easy to use a logarithmic axis for barplots: see comments here, for example). Those who feel this way would say you should use points to indicate the value instead; in this case the implicit assumption of zero-anchoring does not hold as strongly.

我声称(我不能指出你一个明确的参考,虽然谷歌搜索“条形图零轴”似乎提出了有用的东西;或许其他人会插话),绘制条形图垂直轴做的不好的做法不包括零:参数是观察者将假设条形图锚定到原点(这个参数在解释为什么R不容易使用对数轴用于条形图时更常见:请参阅此处的注释,例)。那些有这种感觉的人会说你应该使用积分来表示价值;在这种情况下,零锚定的隐含假设并不强烈。

In other words, "here's how you can do this -- but you shouldn't" ...

换句话说,“这就是你如何做到这一点 - 但你不应该......”

#2


3  

The following can be seen if you look at ?barplot:

如果你看一下,可以看到以下内容?barplot:

"xpd: logical. Should bars be allowed to go outside region?"

“xpd:逻辑。是否应允许禁止进入区域外?”

You just need to include xpd=FALSE in your parameters for the barplot.

您只需要在条形图的参数中包含xpd = FALSE。

#1


9  

Adding xpd=FALSE and re-adding the horizontal axis works, sort of:

添加xpd = FALSE并重新添加水平轴工作,排序:

b <- barplot(c(363.2, 377.4), beside = TRUE, 
   ylim = c(340,380), col = c("orange", "blue"),xpd=FALSE)
axis(side=1,at=b,labels=c("group 1", "group 2"))
box(bty="l")

I claim (I can't point you to a definitive reference, although Googling "bar plot zero axis" seems to come up with useful stuff; perhaps others will chime in) that it is bad practice to draw bar graphs where the vertical axis does not include zero: the argument is that the viewer will assume that bar graphs are anchored to the origin (this argument is more commonly made when explaining why R doesn't make it easy to use a logarithmic axis for barplots: see comments here, for example). Those who feel this way would say you should use points to indicate the value instead; in this case the implicit assumption of zero-anchoring does not hold as strongly.

我声称(我不能指出你一个明确的参考,虽然谷歌搜索“条形图零轴”似乎提出了有用的东西;或许其他人会插话),绘制条形图垂直轴做的不好的做法不包括零:参数是观察者将假设条形图锚定到原点(这个参数在解释为什么R不容易使用对数轴用于条形图时更常见:请参阅此处的注释,例)。那些有这种感觉的人会说你应该使用积分来表示价值;在这种情况下,零锚定的隐含假设并不强烈。

In other words, "here's how you can do this -- but you shouldn't" ...

换句话说,“这就是你如何做到这一点 - 但你不应该......”

#2


3  

The following can be seen if you look at ?barplot:

如果你看一下,可以看到以下内容?barplot:

"xpd: logical. Should bars be allowed to go outside region?"

“xpd:逻辑。是否应允许禁止进入区域外?”

You just need to include xpd=FALSE in your parameters for the barplot.

您只需要在条形图的参数中包含xpd = FALSE。