I'm trying to generate a barplot, but the y-axis scale is too short. Here's my code:
我正在尝试生成一个条形图,但是y轴刻度太短。这是我的代码:
barplot(as.matrix(dat), log="y", ylim=c(10000,100000000), beside=TRUE,
ylab = "Number of reads", col = c("gray","black","white"))
It leaves the room for the axis (as per ylim), but doesn't fill in the actual axis. I've been through ?barplot
and tried a few things (from googling around I thought xpd = F, yaxs = c(10000,10000000,5)
should work, but it didn't).
它为轴留下了空间(根据ylim),但没有填充实际的轴。我经历过?barplot并尝试了一些事情(从谷歌搜索我认为xpd = F,yaxs = c(10000,10000000,5)应该有效,但事实并非如此)。
I know it's a minor thing, but it's exactly the kind of problem I get stuck on for ages, instead of actually working, so any help would be much appreciated!
我知道这是一件小事,但这正是我长期坚持的问题,而不是实际工作,所以任何帮助都会非常感激!
Edit: Cheers for the input guys!
编辑:为输入人员干杯!
I did initially plot without ylim, but it ends up with an even more bizarre axis (with the same problem); I actually picked my ylim values to give it a nicer spaced axis.
我最初没有使用ylim进行绘图,但最终会出现一个更奇怪的轴(同样的问题);我实际上选择了我的ylim值来给它一个更好的间隔轴。
Here's the data:
这是数据:
dat <- read.table(text="D2,D3n,D3m,D4n,D4m
21234722,34262282,31920464,25486357,20712943
35343,64403,22537,39934,46547
126646,312286,101105,81537,76944", header=TRUE, sep=",")
Edit 2: @DWin had it right - I updated my R, and now it plots fine - thanks everyone!
编辑2:@DWin没错 - 我更新了我的R,现在情节很好 - 谢谢大家!
3 个解决方案
#1
25
I see you try to set ylim
bad you give bad values. This will change the scale of the plot(like a zoom) For example see this :
我看到你试图设置ylim坏你给坏的价值。这将改变绘图的比例(如缩放)例如,请看:
par(mfrow=c(2,1))
tN <- table(Ni <- stats::rpois(100, lambda = 5))
r <- barplot(tN, col = rainbow(20),ylim=c(0,50),main='long y-axis')
r <- barplot(tN, col = rainbow(20),main='short y axis')
Another option is to plot without axes , and set it manually using axis
and usr
:
另一种选择是在没有轴的情况下绘图,并使用axis和usr手动设置:
require(grDevices) # for colours
par(mfrow=c(1,1))
r <- barplot(tN, col = rainbow(20),main='short y axis',ann=FALSE,axes=FALSE)
usr <- par("usr")
par(usr=c(usr[1:2], 0, 20))
axis(2,at=seq(0,20,5))
#2
0
Simplest solution seems to be specifying the ylim
range. Here is some code to do this automatically (left default, right - adjusted):
最简单的解决方案似乎是指定ylim范围。以下是一些自动执行此操作的代码(左侧默认,右侧调整):
layout(t(c(1,2)))
# default y-axis
barplot(dat, beside=TRUE)
# automatically adjusted y-axis
barplot(dat, beside=TRUE, ylim=range(pretty(c(0, dat))))
The trick is to use pretty()
which returns a list of interval breaks covering all values of the provided data. It guarantees that the maximum returned value is 1) a round number 2) greater than maximum value in the data.
诀窍是使用pretty()返回一个包含所提供数据的所有值的间隔中断列表。它保证最大返回值是1)数字2)大于数据中的最大值。
In the example 0 was also added pretty(c(0, dat))
which makes sure that axis starts from 0.
在示例0中还添加了漂亮(c(0,dat)),这确保了轴从0开始。
#3
#1
25
I see you try to set ylim
bad you give bad values. This will change the scale of the plot(like a zoom) For example see this :
我看到你试图设置ylim坏你给坏的价值。这将改变绘图的比例(如缩放)例如,请看:
par(mfrow=c(2,1))
tN <- table(Ni <- stats::rpois(100, lambda = 5))
r <- barplot(tN, col = rainbow(20),ylim=c(0,50),main='long y-axis')
r <- barplot(tN, col = rainbow(20),main='short y axis')
Another option is to plot without axes , and set it manually using axis
and usr
:
另一种选择是在没有轴的情况下绘图,并使用axis和usr手动设置:
require(grDevices) # for colours
par(mfrow=c(1,1))
r <- barplot(tN, col = rainbow(20),main='short y axis',ann=FALSE,axes=FALSE)
usr <- par("usr")
par(usr=c(usr[1:2], 0, 20))
axis(2,at=seq(0,20,5))
#2
0
Simplest solution seems to be specifying the ylim
range. Here is some code to do this automatically (left default, right - adjusted):
最简单的解决方案似乎是指定ylim范围。以下是一些自动执行此操作的代码(左侧默认,右侧调整):
layout(t(c(1,2)))
# default y-axis
barplot(dat, beside=TRUE)
# automatically adjusted y-axis
barplot(dat, beside=TRUE, ylim=range(pretty(c(0, dat))))
The trick is to use pretty()
which returns a list of interval breaks covering all values of the provided data. It guarantees that the maximum returned value is 1) a round number 2) greater than maximum value in the data.
诀窍是使用pretty()返回一个包含所提供数据的所有值的间隔中断列表。它保证最大返回值是1)数字2)大于数据中的最大值。
In the example 0 was also added pretty(c(0, dat))
which makes sure that axis starts from 0.
在示例0中还添加了漂亮(c(0,dat)),这确保了轴从0开始。