Skip to content
Back to formatted view

Raw Message

Message-ID: <CADfFDC5cm0gX7u5edv8VWy3Nq8Dk3B-jndxK4oNk7dqsK=PP=g@mail.gmail.com>
Date: 2011-08-19T04:30:20Z
From: Deepayan Sarkar
Subject: dotchart vs. dotplot ... groups
In-Reply-To: <1650309088.91371.1313605792602.JavaMail.root@sz0052a.westchester.pa.mail.comcast.net>

On Wed, Aug 17, 2011 at 11:59 PM,  <mkzodet at comcast.net> wrote:
> I'm trying to create a dotplot with some grouping.
>
> I've been able to create what I want using dotchart (basic graphics), but can't quite get it using dotplot (lattice). I prefer to use lattice (or ggplot2) because I think it's a bit easier to control some other aspects of the plot appearance.
>
> Basically, w/ lattice I've not been able to get the y-axis label to include the group variable.
>
> I'd like...
>
> A
> 1
> 2
> B
> 1
> 2
> 3
> 4
> C
> 1
> 2
> 3
>
> I'm getting...
> 1
> 2
> 1
> 2
> 3
> 4
> 1
> 2
> 3
>
> The following example code illustrates....
>
> set.seed(18)
> dta <- data.frame(var1=factor(c("A", "A", "B", "B", "B", "B", "C", "C", "C")),
> var2=c(1,2,1,2,3,4,1,2,3),
> var3=round(runif(9,1,10),1),
> plotorder=9:1)
>
> dta
>
> windows(3,3)
> dotchart(dta$var3[order(dta$var1, -dta$var2)], groups=dta$var1,
> labels=dta$var2[order(dta$var1, -dta$var2)], cex=.75,
> gcolor=c("blue", "red", "dark green"),
> col=c(rep("blue",2), rep("red",4), rep("dark green",3)),
> axes=NULL)

Well, the more "lattice-like" version of this would be


dta$var2fac <- factor(dta$var2, levels = rev(sort(unique(dta$var2))))

dotplot(var2fac ~ var3 | var1, data=dta, groups=var1,
        col=c("blue", "red", "dark green"),
        layout = c(1, 3), as.table = TRUE,
        scales=list(y = list(relation = "sliced")))


But if you really want the dotchart()-like output, you can try

dta$var12fac <- with(dta, interaction(var2fac, factor(var1, levels =
rev(levels(var1)))))
dta$var12fac <- droplevels(dta$var12fac)
lab <- substring(levels(dta$var12fac), 1, 1)

dotplot(var12fac ~ var3,
        data = dta, groups=var1,
        col=c("blue", "red", "dark green"),
        scales = list(y = list(labels = lab,
                               col = rep(c("dark green", "red", "blue"),
                                         c(3, 4, 2)))),
        ylab = list(label = c("C", "B", "A"),
                    y = c(3, 7, 9) / 9.2,
                    rot = 0, col = c("dark green", "red", "blue")))


-Deepayan