2

我正在尝试将名称添加到我的条形图的列中...每组中有 2 个条具有相同的名称..

我正在使用这样的代码:

l<-c(.6286, .2212, .9961, .5831, .8703, .6990, .9952, .9948)

r<-c(.2721, .5663, .0, .3961, .0696, .1180, .0, .0)

tab<-rbind(l,r)

plot<-barplot(tab, beside=TRUE, axisnames=FALSE, main = 'Time Spent Left vs. Right', sub = 'Female 2c', xlab= 'Days After Entry', ylab = 'Proportion of Time Spent', col=c('blue', 'red'), ylim = c(0,1))
legend('topleft', .8,  c('Left' , 'Right'), pch=c(.8), col=c('blue', 'red'))
names.arg = jd2c$d.in.p

names.arg=c(3,4,5,6,6,7,10,11) #listed for informational purposes, same as jd2c$d.in.p

jd2c$d.in.p 也在上面列出。由于某种原因,names.arg 函数似乎没有按照我的预期进行。即使我把它放在 bar plot() 函数的括号内..

我究竟做错了什么?

谢谢!

4

2 回答 2

1

使用你的数据和names.arg适当的设置axisnames=TRUE,我得到一个合理的结果。我调整了其他一些东西(设置las=1为水平轴标签,使用fill而不是col在图例中,摆脱了pch=0.8)。

par(las=1)
bnames <- c(3,4,5,6,6,7,10,11) #listed for informational purposes, same as jd2c$
plot<-barplot(tab, beside=TRUE, axisnames=TRUE,
              main = 'Time Spent Left vs. Right',
              sub = 'Female 2c', xlab= 'Days After Entry',
              ylab = 'Proportion of Time Spent',
              col=c('blue', 'red'), ylim = c(0,1),
              names.arg=bnames)
legend('topleft', cex=1,  c('Left' , 'Right'), fill=c('blue', 'red'))

在此处输入图像描述

于 2012-07-16T18:12:48.360 回答
1

names.arg不是将其定义为barplot函数的参数,而是定义为一个完全独立的变量——它位于括号之外。这个:

plot<-barplot(tab, beside=TRUE, axisnames=FALSE, main = 'Time Spent Left vs. Right', sub = 'Female 2c', xlab= 'Days After Entry', ylab = 'Proportion of Time Spent', col=c('blue', 'red'), ylim = c(0,1))
legend('topleft', .8,  c('Left' , 'Right'), pch=c(.8), col=c('blue', 'red'))
names.arg = jd2c$d.in.p

应该:

plot<-barplot(tab, beside=TRUE, axisnames=FALSE, main = 'Time Spent Left vs. Right', sub = 'Female 2c', xlab= 'Days After Entry', ylab = 'Proportion of Time Spent', col=c('blue', 'red'), ylim = c(0,1), names.arg = jd2c$d.in.p)
legend('topleft', .8,  c('Left' , 'Right'), pch=c(.8), col=c('blue', 'red'))

您还必须设置axisnames=TRUE要显示的名称。

于 2012-07-16T18:17:35.443 回答