[R] Coloring Stripchart Points, or Better, Lattice Equivalent
Gabor Grothendieck
ggrothendieck at gmail.com
Sun Jun 22 22:13:47 CEST 2008
I tried it with a simple example to make it easier to check the
result and it does seem we need a panel function, pnl:
library(lattice)
DF <- data.frame(x = 1:5, col = c(1:4, 1), g = c(1, 1, 2, 2, 2), pch =
c(1:3, 2:1))
pnl <- function(x, y, subscripts, col, pch, ...) panel.stripplot(x, y,
col = col[subscripts], pch = pch[subscripts], ...)
stripplot(~ x | g, DF, col = DF$col, pch = DF$pch, panel = pnl)
Using the same panel function, pnl, we can spread out resp into two different
panels like this:
all$resp <- as.factor(all$resp) # as before
stripplot(~ index | resp, all, col = all$col, pch = all$sym, panel =
pnl, layout = 1:2)
On Sun, Jun 22, 2008 at 1:09 PM, Bryan Hanson <hanson at depauw.edu> wrote:
> Thanks Gabor, I'm getting closer.
>
> Is there a way to spread out resp values vertically for a given value of
> index? In base graphics, stripchart does this with method = "stack". But
> in lattice, stack = TRUE does something rather different, and I don't see a
> combination of lattice arguments that does it like base graphics.
>
> Thanks, Bryan
>
>
> On 6/22/08 12:48 PM, "Gabor Grothendieck" <ggrothendieck at gmail.com> wrote:
>
>> Actually I am not sure if my prior answer was correct. I think its ok
>> with one panel but
>> you might have to use a panel function is there are several. With one
>> panel it seems
>> ok:
>>
>> stripplot(~ index, all, col = all$col, pch = all$sym)
>>
>> On Sun, Jun 22, 2008 at 12:28 PM, Gabor Grothendieck
>> <ggrothendieck at gmail.com> wrote:
>>> Try this:
>>>
>>> library(lattice)
>>> all$resp <- as.factor(all$resp)
>>> stripplot(~ index | resp * yr, all, col = all$col, pch = all$sym,
>>> layout = c(1, 4))
>>>
>>>
>>> On Sun, Jun 22, 2008 at 10:43 AM, Bryan Hanson <hanson at depauw.edu> wrote:
>>>> Below is a revised set of code that demonstrates my question a little more
>>>> clearly, I hope.
>>>>
>>>> When plotting all the data (5th panel), col & sym don't seem to be passed
>>>> correctly, as the (random) first value for col & sym are used for all points
>>>> (run the code, then run it again, you'll see how the 5th panel changes
>>>> depending upon col & sym for the first data point). The 5th panel should
>>>> ideally be the "sum" of the 4 panels above, keeping col & sym intact.
>>>>
>>>> Also, I would rather have this in lattice or ggplot2, if anyone sees how to
>>>> convert it.
>>>>
>>>> Thanks once again, several of you have made very useful suggestions off
>>>> list. Bryan
>>>>
>>>> samples <- 100 # must be even
>>>> index <- round(runif(samples, 1, 100)) # set up data
>>>> resp <- rbinom(samples, 1, 0.5)
>>>> yr <- rep(c("2005", "2006"), samples/2)
>>>> all <- data.frame(index, resp, yr)
>>>> all$sym <- ifelse(all$resp == 1, 1, 3)
>>>> all$col <- ifelse(all$yr == 2005, "red", "blue")
>>>> all$count <- rep(1, length(all$index))
>>>> all <- all[order(all$index, all$yr, all$resp),] # for easier inspection
>>>> row.names(all) <- c(1:samples) # for easier inspection
>>>>
>>>> one <- all[(all$yr == 2005 & all$resp == 0),] # First 2005/0 at top
>>>> two <- all[(all$yr == 2005 & all$resp == 1),] # Then 2005/1
>>>> three <- all[(all$yr == 2006 & all$resp == 0),] # Now 2006/0
>>>> four <- all[(all$yr == 2006 & all$resp == 1),] # Finally 2006/1
>>>>
>>>> par(mfrow = c(5, 1))
>>>> par(plt = c(0.1, 0.9, 0.25, 0.75))
>>>> stripchart(one$index, method = "stack", ylim = c(0,10), xlim = c(1,100), col
>>>> = one$col, pch = one$sym)
>>>> mtext("2005/0", side = 3)
>>>> stripchart(two$index, method = "stack", ylim = c(0,10), xlim = c(1,100), col
>>>> = two$col, pch = two$sym)
>>>> mtext("2005/1", side = 3)
>>>> stripchart(three$index, method = "stack", ylim = c(0,10), xlim = c(1,100),
>>>> col = three$col, pch = three$sym)
>>>> mtext("2006/0", side = 3)
>>>> stripchart(four$index, method = "stack", ylim = c(0,10), xlim = c(1,100),
>>>> col = four$col, pch = four$sym)
>>>> mtext("2006/1", side = 3)
>>>> stripchart(all$index, method = "stack", ylim = c(0,10), xlim = c(1,100), col
>>>> = all$col, pch = all$sym)
>>>> mtext("col & sym always taken from 1st data point when all data is
>>>> plotted!", side = 3)
More information about the R-help
mailing list