在晶格条形图函数中获取分组条形的中点值 [英] Obtaining midpoint values of grouped bars in lattice barchart function

查看:118
本文介绍了在晶格条形图函数中获取分组条形的中点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何确定分组条形图的中点值,即每个条形图中心的实际X位置.这可以在基本的R barplot函数中轻松完成,但是我希望能够在晶格barchart中实现.我的目标是在相应栏的顶部显示text列的值.

I am trying to figure out how to determine the midpoint values of grouped bars, i.e. the actual X positions of the center of each bar. This is easily done in the base R barplot function, however I'd like to be able to do it in lattice's barchart. My goal is to display the values of text column on top of the corresponding bar.

下面的代码允许我将文本放置在条形图的顶部,只要我不使用子组即可.我尝试在Internet上搜索解决方案,但似乎无济于事.从图中可以看出,中点仅针对整个组的中心确定.

The code below allows me to place the text on top of the bars as long as I do not do use subgroups. I have tried searching Internet for the solution but nothing seems to work. As you can see from the graph, the midpoints are determined only for the center of the entire group.

谢谢!

library(lattice)

test= data.frame(
  group=c("WK 1", "WK 1", "WK 1",  "WK 2", "WK 2", "WK 2", "WK 3", "WK 3", "WK 3"),
  subgroup=c(1,2,3,1,2,3,1,2,3)  ,
  percent=c(60,50,80,55,56,65,77,65,86),
  text=c("n=33", "n=37","n=39","n=25","n=27","n=22","n=13","n=16","n=11")
  )

barchart(data=test, 
         percent~group,
         groups=subgroup,

            panel = function(x,y,...){
              panel.barchart(x, y, ...)
              panel.text( x=unique(test$group), 
                          y=test$percent, 
                          label=unique(test$text) 
                          )

            }
         )

推荐答案

一种快捷方法是更改​​绘制条的panel.barchart的正文,以在适当的位置添加绘图文字.

A quick-and-dirty way would be to change the body of panel.barchart where the bars are drawn to include plotting text at the appropriate places.

创建一个新的面板功能(通常,这是一个好主意,因为如果您需要像我一样重新开始多次,那么原始功能仍然存在)

Create a new panel function (In general a good idea, because then the original function is still there, in case you need to start over as many times as I do)

myPanelBarchart <- panel.barchart

更改面板功能的相应部分(请记住:如果不再有组,水平绘制或堆叠,则将不再起作用).在这里,我向panel.text添加了一个调用,如果给myPanelBarchart传递了名为printVals的参数,而不是FALSE.此参数将是您要在每个小节上方打印的字符向量.它的长度必须与所绘制的条形数量相同,并且顺序必须与您的数据相对应.我没有添加任何东西来检查所有这些是否正确.

Change the corresponding part of the panel function (keep in mind: if you no longer have groups, if you plot horizontally, or if you stack, this will no longer work). Here, I added a call to panel.text if myPanelBarchart is passed an argument named printVals and it is not FALSE. This argument will be the character vector you'd like to print above each bar. It needs to be the same length as the number of bars that are plotted and in the order corresponding to your data. I didn't add anything to check that any of this is true.

body(myPanelBarchart)[[10]][[4]][[2]][[4]][[4]][[9]]<-substitute(for (i in unique(x)) {
    ok <- x == i
    nok <- sum(ok, na.rm = TRUE)
    panel.rect(x = (i + width * (groups[ok] - (nvals + 1)/2)), 
        y = rep(origin, nok), col = col[groups[ok]], border = border[groups[ok]], 
        lty = lty[groups[ok]], lwd = lwd[groups[ok]], width = rep(width, 
            nok), height = y[ok] - origin, just = c("centre", 
            "bottom"), identifier = identifier)
# This is the added part (adjust parameters as you see fit):
    if(!identical(printVals, FALSE)){
      panel.text(x = (i + width * (groups[ok] - (nvals + 1)/2)), y = y[ok],
        label = printVals[ok], adj = c(0.5, -1), identifier = identifier)
    }        

})

更改新函数的形式参数列表,以添加参数printVals并为其指定默认值.

Change the formal argument list of the new function to add the argument printVals and give it a default.

formals(myPanelBarchart) <- c(formals(panel.barchart), printVals = FALSE)

使用带有新参数的新面板函数绘制数据

Plot the data with the new panel function with the new argument

barchart(data=test, 
         percent~group,
         groups=subgroup,

            panel = function(x,y,...){
              myPanelBarchart(x, y, ..., printVals = test$text, )
            }
         )

哪个应该给你

总而言之,x位置是根据绘图的方向和组成来确定的.对于垂直的,未堆叠的分组条形图,x位置在body(myPanelBarchart)[[10]][[4]][[2]][[4]][[4]][[9]]中确定.

So the long and short of it is that the x positions are determined according to the orientation and composition of your plot. In the case of vertical, non-stacked, grouped bars, the x positions are determined in body(myPanelBarchart)[[10]][[4]][[2]][[4]][[4]][[9]].

这篇关于在晶格条形图函数中获取分组条形的中点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆