2012-07-16 75 views
2

我试图将名称添加到我的柱状图中的列barplot()......有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()函数的括号内。

我在做什么错了?

谢谢!

回答

1

正确使用您的数据并设置names.argaxisnames=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')) 

enter image description here

+0

谢谢你们两位!我用你的建议和图表出来了不起.. – 2012-07-17 14:01:17

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出现的名字。

+0

我确实有更多的问题..在设置我的初始情节,而不是列出值作为矢量绘图,我想从数据集中做到..这里我什么意思..数据作为向量(在rbind函数之后):[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] l 0.6286 0.2212 0.9961 0.5831 0.8703 0.699 0.9952 0.9948 ř0.2721 0.5663 0.0000 0.3961 0.0696 0.118 0.0000 0.0000 – 2012-07-17 14:02:18

+0

我从数据集中的数据看起来像这样rbind后:[1] [1,] 0.2721 [2,1] 0.5663 [3,] 0.0000 [4,] 0.3961 [5,1] 0.0606 [6,] 0.1180 [7,] 0.0000 [8,] 0.0000 [9,] 0.6286 [10,] 0.2212 [11,] 0.9961 [12,] 0.5831 [13,] 0.8703 [14,] 0.6990 [15,] 0.9952 [16,] 0.994 – 2012-07-17 14:04:59