I have a data frame where some variables present little relative variation and some others present a high variation. For instance, consider the following data frame:
df <- data.frame(IDX = 1:10, V1 = runif(10) + 100000, V2 = runif(10))
with the following contents:
IDX V1 V2
1 1 100001.0 0.39601382
2 2 100000.1 0.76472032
3 3 100000.1 0.10183021
4 4 100000.2 0.12735142
5 5 100000.8 0.21488898
6 6 100000.1 0.92675265
7 7 100000.5 0.27987290
8 8 100000.6 0.69132304
9 9 100000.5 0.20719782
10 10 100000.8 0.02314787
The absolute variation for both variables is similar (0.10 for V1 and 0.09 for V2). But, if we consider the relative variation with respect to their means, then the variation for V1 is very little while the opposite occurs for V2:
> var(df$V1) / mean(df$V1)
[1] 1.082472e-06
> var(df$V2) / mean(df$V2)
[1] 0.2617366
The problem comes when I try to make a dotplot for both variables, and I try to set the limits for the X scale. If I use the same scale for each variable (relation = 'same', the default setting), I obtain the following:

Here you have the code used to create the plot:
library(lattice)
library(reshape2)
df <- melt(df, id.vars = 'IDX')
# approach 1
pl <- dotplot(IDX ~ value | variable, df,
scales = list(x = list(relation = 'same')))
print(pl)
As you can see V2’s values are so small, that when using the same scale for both panels it seems as if there were no variation among V2 values. If I make the X scale for each panel independent, then I get the following:

And here you have the code to produce the second plot:
# approach 2
pl <- dotplot(IDX ~ value | variable, df,
scales = list(x = list(relation = 'free')))
print(pl)
The problem now is that the range of the X scale for the left panel goes from 100000.2 to 100001.0, and the data looks as if there was a lot of variation, when in reality it is not like that.
Somehow, I would like to obtain a plot where the left panel looked like the left panel in the first plot, and the right panel looked like the right panel in the second plot. My idea was to define the X ranges based on the mean of the variable being plotted. For instance, for each panel, the X limits could go from 0.9 * mean to 1.1 * mean. But I do not know how to do that in the presence of multiple variables (and panels).
The cleanest solution will probably be to supply your own
prepanelfunction, which will set each panel’s x-limits ‘on the fly’. Here is an example that sets xlim to extend from 0 to the maximum value of x present in a panel