【问题标题】:labeling in barplot()barplot() 中的标签
【发布时间】:2012-07-16 17:54:33
【问题描述】:

我正在尝试将名称添加到我的条形图的列中...每个组中有 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() 函数的括号内..

我做错了什么?

谢谢!

【问题讨论】:

    标签: r


    【解决方案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'))
    

    【讨论】:

      【解决方案2】:

      您将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 才能显示名称。

      【讨论】:

      • 我确实有更多问题。在设置我的初始绘图时,我不想将值列为要绘图的向量,而是从数据集中进行。这就是我的意思.. 数据作为向量(在 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
      • 我的数据集在 rbind 之后看起来像这样: [,1] [1,] 0.2721 [2,] 0.5663 [3,] 0.0000 [4,] 0.3961 [5,] 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
      猜你喜欢
      • 2018-08-31
      • 2020-07-10
      • 2015-10-16
      • 2015-10-29
      • 2013-03-22
      • 2018-11-17
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      相关资源
      最近更新 更多