Boris.Vasiliev at forces.gc.ca
2010-Jan-14 17:25 UTC
[R] lattice dotplot with missing levels in factor variable
Hi,
I am trying to create a dotplot where each panel shows levels vs.
responses; the levels are sorted by responses but levels vary from one
panel to another. However, I run into problems with controlling the
y-limits and y-labels.
In particular, suppose I have a data frame
rsp <- c(10,2,4,0,2,3)
lvl <-
factor(c("L1","L2","L3","L2","L3","L4"),levels=c("L1","L2","L3","L4"))
cat <-
factor(c("A","A","A","B","B","B"),levels=c("A","B"))
xx <- data.frame(cat,lvl,rsp)
print(xx)
cat lvl rsp
1 A L1 10
2 A L2 2
3 A L3 4
4 B L2 0
5 B L3 2
6 B L4 3
I tried to follow the "Lattice ..." book and used
dotplot(lvl~rsp|cat,data=xx,type=c("h","p"),
panel=function(x,y,...){y1 <- reorder(y,x)
y1 <- y1[,drop=TRUE]
panel.dotplot(x,y1,...)},
prepanel=function(x,y){y1 <- reorder(y,x)
y1 <- y1[,drop=TRUE]
return(list(ylim=levels(y1)))},
layout=c(1,2))
However, the resulting has incorrect y-labels:
Desired Plot Achieved Plot
Panel B: Panel B:
L4
L4 ----o L1 ----o
L3 ---o L3 ---o
L2 o L3 o
panel A: Panel A:
L4
L1 ----------o L1 ----------o
L3 ----o L3 ----o
L2 --o L2 --o
It appears that the y-limits and y-labels specified by the pre-panel
function are overwritten by something in dotplot. Can anybody suggest
what I am doing incorrectly?
Regards,
Boris.
Dennis Murphy
2010-Jan-14 19:40 UTC
[R] lattice dotplot with missing levels in factor variable
Hi:
Based on an old R-help post,
http://tolstoy.newcastle.edu.au/R/e2/help/06/09/1579.html
I managed to get the desired plot with the following call:
dotplot(reorder(lvl, rsp) ~ rsp | cat, data = xx,
type = c('h', 'p'), layout = c(1, 2),
scales = list(y = list(relation = 'free', rot = 0)),
prepanel = function(x, y, ...) {
yy <- y[, drop = TRUE]
list(ylim = levels(yy),
yat = sort(unique(as.numeric(yy))))
},
panel = function(x, y, ...) {
yy <- y[, drop = TRUE]
panel.dotplot(x, yy, ...)
}
)
Trivial :)
HTH,
Dennis
On Thu, Jan 14, 2010 at 10:53 AM, Dennis Murphy <djmuser@gmail.com> wrote:
> Hi:
>
> I can get you halfway there, but I haven't figured out yet how to get
rid
> of the
> unused levels...
>
> dotplot(reorder(lvl, rsp) ~ rsp | cat, data = xx,
> type = c('h', 'p'), layout = c(1, 2))
>
> I'll play with this for a few more minutes...if I get something,
I'll let
> you know.
>
> HTH,
> Dennis
>
>
> On Thu, Jan 14, 2010 at 9:25 AM, <Boris.Vasiliev@forces.gc.ca> wrote:
>
>> Hi,
>>
>> I am trying to create a dotplot where each panel shows levels vs.
>> responses; the levels are sorted by responses but levels vary from one
>> panel to another. However, I run into problems with controlling the
>> y-limits and y-labels.
>>
>> In particular, suppose I have a data frame
>>
>> rsp <- c(10,2,4,0,2,3)
>> lvl <-
>>
factor(c("L1","L2","L3","L2","L3","L4"),levels=c("L1","L2","L3","L4"))
>> cat <-
factor(c("A","A","A","B","B","B"),levels=c("A","B"))
>> xx <- data.frame(cat,lvl,rsp)
>> print(xx)
>>
>> cat lvl rsp
>> 1 A L1 10
>> 2 A L2 2
>> 3 A L3 4
>> 4 B L2 0
>> 5 B L3 2
>> 6 B L4 3
>>
>> I tried to follow the "Lattice ..." book and used
>>
>> dotplot(lvl~rsp|cat,data=xx,type=c("h","p"),
>> panel=function(x,y,...){y1 <- reorder(y,x)
>> y1 <- y1[,drop=TRUE]
>> panel.dotplot(x,y1,...)},
>> prepanel=function(x,y){y1 <- reorder(y,x)
>> y1 <- y1[,drop=TRUE]
>> return(list(ylim=levels(y1)))},
>> layout=c(1,2))
>>
>> However, the resulting has incorrect y-labels:
>>
>> Desired Plot Achieved Plot
>>
>> Panel B: Panel B:
>>
>> L4
>> L4 ----o L1 ----o
>> L3 ---o L3 ---o
>> L2 o L3 o
>>
>> panel A: Panel A:
>>
>> L4
>> L1 ----------o L1 ----------o
>> L3 ----o L3 ----o
>> L2 --o L2 --o
>>
>> It appears that the y-limits and y-labels specified by the pre-panel
>> function are overwritten by something in dotplot. Can anybody suggest
>> what I am doing incorrectly?
>>
>> Regards,
>> Boris.
>>
>> ______________________________________________
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
[[alternative HTML version deleted]]