# [R] subset inside a lattice plot using panel.lines

Mark Difford mark_difford at yahoo.co.uk
Mon Aug 4 17:44:34 CEST 2008

```Hi Michael,

>> Pulling my hair out here trying to get something very simple to work. ...

I can't quite see what you are trying to do [and I am not sure that you
clearly state it], but you could make things easier and simpler by (1)
creating a factor to identify your groups of rows more cleanly and (2) by
using the groups= argument in lattice.

## Something along these lines
copyDat <- originalDat
copyDat\$newFac <- gl(2, 387, 774, labels=c("A","B"))

xyplot(response ~ predictor|maybe.condition, data=copyDat, groups=newFac)

I hope this gives you some ideas.

Mark.

Michael Hopkins wrote:
>
>
>
> Hi R people [duplicate - sorry, just posted HTML by mistake]
>
> Pulling my hair out here trying to get something very simple to work.
> Have a data frame of 774 rows and want to plot first and second half
> on same axes with different colours.  A variable is present call 'row'
> created like this and checked to be OK:
>
> 	row <- seq( len=length( variable1 ) )
>
> ...so I just want to plot the two subsets where row <= 387 and where
> row > 387.  None of these work properly:
>
> plots both over each other in correct colours
> 	opt_plot2 <- function( var_string, c, units ) {
> 	print( xyplot( get(var_string) ~ variable1 | variable2, panel =
> 	function( x, y, ... ) {
> 		panel.xyplot( x, y, type ="n", ... )
> 		panel.grid( h=-1, v=-1, lty=3, lwd=1, col="grey" )
> 		panel.lines( x, y, col = "red", subset = row <= 387 )
> 		panel.lines( x, y, col = "dark green", subset = row > 387 )
> 		},
> 	}
>
> plots both just in red
> 	...		panel.lines( x[row <= 387], y[row <= 387], col = "red" )
> 			panel.lines( x[row > 387], y[row > 387], col = "dark green" )
>
> first <- (row <= 387)
> second <- (row > 387)
>
> plots both over each other in correct colours
> 	...		panel.lines( x, y, col = "red", subset = first )
> 			panel.lines( x, y, col = "dark green", subset = second )
> plots both just in red
> 	...		panel.lines( x[first], y[first], col = "red" )
> 			panel.lines( x[second], y[second], col = "dark green" )
>
>
> I'm feeling frustrated and a bit stupid but should this be so
> difficult?  Any help or tips on what I am doing wrong greatly
> appreciated.
>
> TIA
>
> Michael
>
> ______________________________________________
>
>      Hopkins Research      Touch the Future
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help