Obtaining average values ​​of grouped bars as a function of a trellised bar code

I am trying to figure out how to determine the average values ​​of grouped bars, i.e. the actual X-positions of the center of each bar. This is easy to do in the base function of R barplot , however I would like to do it in the barchart lattice . My goal is to display the text column values ​​on top of the corresponding row.

The code below allows me to put text on top of the bars until I use subgroups. I tried to find the Internet for a solution, but nothing worked. As you can see from the graph, the midpoints are determined only for the center of the entire group.

Thanks!

 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) ) } ) 

enter image description here

+7
source share
1 answer

A quick and dirty way would be to change the body of panel.barchart , where strokes are drawn to enable the construction of text in appropriate places.

Create a new dashboard function (generally a good idea, because then the original function still exists if you need to start as many times as I do)

 myPanelBarchart <- panel.barchart 

Change the corresponding part of the panel function (keep in mind: if you no longer have groups, if you plot horizontally, or if you add, 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 symbol character you want to print over each panel. It should be the same length as the number of lines built on the chart, and in the order that matches your data. I have not added anything to verify that 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) } }) 

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

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

Place data using a new panel function with a new argument

 barchart(data=test, percent~group, groups=subgroup, panel = function(x,y,...){ myPanelBarchart(x, y, ..., printVals = test$text, ) } ) 

What should give you

Modified barchart

Thus, the long and small of them is that the x positions are determined in accordance with the orientation and composition of your plot. In the case of vertical, non-stationary, grouped bars, x positions are defined in body(myPanelBarchart)[[10]][[4]][[2]][[4]][[4]][[9]] .

+7
source

All Articles