Conditional panel function in a grid for several y variables

I have a data set in which the value ( mean ) may or may not fall within the interval specified by lower.bound and upper.bound . I would like to build it with lattice and achieved something really nice, but there are still three things left, I don't know how to handle it (I'm relatively new to lattice ).

 df <- read.table("http://pastebin.com/raw.php?i=FQh6F12t") require(lattice) lattice.options(default.theme = standard.theme(color = FALSE)) head(df) ## code topic problem mean lower.bound upper.bound consistent ## 7 A04C coke MP 99.5 36.45 95.95 0 ## 8 A04C coke MT 47.5 22.78 100.00 1 ## 11 A04C girl MP 50.0 4.75 9.75 0 ## 12 A04C girl MT 99.5 20.00 100.00 1 ## 23 A14G coke MP 88.5 21.25 66.75 0 ## 24 A14G coke MT 82.5 48.36 100.00 1 dotplot(lower.bound + mean + upper.bound ~ code | problem * topic, data = df, pch = c(6, 3, 2), scales = list(x = list(draw = FALSE)), as.table = TRUE) 

This gives: enter image description here

The lower arrows / triangles indicate the lower limit, the up / triangles arrows indicate the upper limit, and + indicates mean . The following things that I would like to add to the plot, but I have no idea how (apart from, obviously, setting the panel function):

  • The conditional pch depends on whether the mean value is inside the interval. The consistent variable indicates this (0 = out of range). pch should be 1 for values ​​inside and 3 for values ​​outside the range. (pch for the lower and upper bounds should not remain unchanged)
  • Interval marking . I would like to draw a thicker line between lower.bound and upper.bound for each tick along the x axis.
  • Add the proportion of out-of-range values ​​to the panel headers (for example, MP; 58.6% to the panel in the upper left corner).

For 1 and 2, my problem obviously is that I don’t know how to handle a custom panel function when there are several y-variables (i.e. how to write conditional panel functions based on this). But I could not find anything.

For 3, the proportion of values ​​outside the interval is set approximately as follows:

 1 - with(df, tapply(consistent, list(topic, problem), mean)) ## MP MT ## coke 0.5862 0.1724 ## girl 0.8276 0.1724 

If the answer also contained a good order of levels along the x axis, which would definitely be a plus. The order can be changed on each panel (i.e., even in panels located one above the other, the same tick along the x axis can correspond to a different code level). But it is not important.

+4
source share
1 answer

Well, this is not entirely beautiful, but he should get a real job (showing you how to get this kind of work).

The basic idea is to rewrite the formula so that it does not have a bunch of names on its LHS (i.e. lower.bound + mean + upper.bound ). This syntax is equivalent to defining the term groups= , which ends with running panel.superpose() , which is the kind of pain you need to configure as you want.

Instead, I simply include mean in the LHS, and then use subscripts inside the custom panel function to select the corresponding upper.bound and lower.bound elements in each case.

I hope the rest is pretty clear:

 LABS <- LETTERS[1:4] with(df, dotplot(mean ~ code | problem * topic, lb=lower.bound, ub=upper.bound, mpch = c(3,1)[consistent+1], ylim = extendrange(c(0,100)), panel = function(x, y, lb, ub, mpch, ..., subscripts) { panel.dotplot(x, y, ..., pch=mpch[subscripts]) lpoints(x, lb[subscripts], pch=6) lpoints(x, ub[subscripts], pch=2) lsegments(x,lb[subscripts],x,ub[subscripts],col="grey60") ltext(x=x[3], y=95, LABS[panel.number()], col="red",fontface=2) }, scales = list(x = list(draw = FALSE)), as.table = TRUE) ) 

enter image description here

+3
source

Source: https://habr.com/ru/post/1439006/


All Articles