Re: [R] conditional assignment of colors in xyplot()
On Mon, Sep 27, 2010 at 7:11 AM, nathan pellegrin nathan.pelleg...@gmail.com wrote: # Dear R Community, # I have this data frame: df1 - data.frame( F1 = factor( c( rep(D1,12),rep(D2,12),rep(D3,12) ) ), F2 = factor( rep( rep( paste(O,1:6,sep=), rep(2,6) ), 3) ), F3 = factor( rep( c(V1,V2), 18 ) ), S1 = c(8.840955e-02,2.546822e-01,7.562658e-01,8.801181e-01,6.041766e-02,2.172731e-01,6.542187e-98, 7.067840e-04,1.430933e-01,9.764401e-01,1.380848e-05,1.192620e-01,9.107259e-01,1.235232e-01, 3.021847e-01,1.331351e-01,5.272103e-01,3.663577e-01,1.539690e-38,2.224451e-01,2.873052e-01, 5.110313e-01,7.840802e-01,8.210762e-10,1.553356e-01,4.173335e-01,5.964021e-01,4.955694e-01, 8.849240e-02,5.739598e-01,1.879075e-17,1.071003e-03,7.298928e-01,6.347287e-01,8.884034e-01, 4.460295e-11), S2 = c(1.32249139,1.02831831,-0.09650252,-0.05454486,2.62105492,2.00310250,8.07269064, -1.55397883,1.77390551,0.04161954,7.14188540,-2.98033713,-0.49904251,-0.74309058, -0.49904251,-0.74309058,1.22492623,-1.79003492,7.60003121,-0.74549596,2.53418936, -1.60112296,0.67131380,-15.31744351,-0.18380339,0.28628435,-0.18380339,0.28628435, 2.96108998,1.18267783,5.78419118,2.70861763,0.66287857,1.10397741,0.27160971, -15.37506924) ) # Two of the factors are used to define an array # of panels with the third showing up as bars along the x axis. # How do I color the bars according to their sign (red 0, blue 0) # conditional on the value of S2 .025 - in which case color them gray? # Initially, I tried to pass a character vector specifying colors, # which does not achieve the desired result: library(lattice) library(latticeExtra) df1$barcols - ifelse(df1$S1 .025, gray, ifelse( df1$S2 0, blue,red)) ctp - xyplot(S2 ~ F2 | F1 + F3, data=df1, as.table=TRUE, ylim=c(-10,10), panel = function(x,y){ panel.barchart(x,y,horizontal=FALSE, origin=0, col=df1$barcols ) } ) useOuterStrips(ctp) # Any help you can provide would be greatly appreciated. # Thank you! Itis better to keep the idea of grouping and the actual colors distinct. df1 - transform(df1, colgrp = factor(ifelse(S1 .025, A, ifelse( df1$S2 0, B, C)), levels = c(A, B, C))) barchart(S2 ~ F2 | F1 + F3, stack = TRUE, data=df1, as.table=TRUE, ylim=c(-10,10), groups = colgrp, par.settings = simpleTheme(col = c(grey, blue, red))) -Deepayan __ 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.
Re: [R] conditional assignment of colors in xyplot()
Thank you, Deepayan, for the answer - and for creating amazingly helpful tools! Thanks also to Jim Lemon who pointed out a typo in the code: S2 .025 should read S2 .025, Nathan On Thu, Sep 30, 2010 at 5:21 AM, Deepayan Sarkar deepayan.sar...@gmail.comwrote: On Mon, Sep 27, 2010 at 7:11 AM, nathan pellegrin nathan.pelleg...@gmail.com wrote: # Dear R Community, # I have this data frame: df1 - data.frame( F1 = factor( c( rep(D1,12),rep(D2,12),rep(D3,12) ) ), F2 = factor( rep( rep( paste(O,1:6,sep=), rep(2,6) ), 3) ), F3 = factor( rep( c(V1,V2), 18 ) ), S1 = c(8.840955e-02,2.546822e-01,7.562658e-01,8.801181e-01,6.041766e-02,2.172731e-01,6.542187e-98, 7.067840e-04,1.430933e-01,9.764401e-01,1.380848e-05,1.192620e-01,9.107259e-01,1.235232e-01, 3.021847e-01,1.331351e-01,5.272103e-01,3.663577e-01,1.539690e-38,2.224451e-01,2.873052e-01, 5.110313e-01,7.840802e-01,8.210762e-10,1.553356e-01,4.173335e-01,5.964021e-01,4.955694e-01, 8.849240e-02,5.739598e-01,1.879075e-17,1.071003e-03,7.298928e-01,6.347287e-01,8.884034e-01, 4.460295e-11), S2 = c(1.32249139,1.02831831,-0.09650252,-0.05454486,2.62105492,2.00310250,8.07269064, -1.55397883,1.77390551,0.04161954,7.14188540,-2.98033713,-0.49904251,-0.74309058, -0.49904251,-0.74309058,1.22492623,-1.79003492,7.60003121,-0.74549596,2.53418936, -1.60112296,0.67131380,-15.31744351,-0.18380339,0.28628435,-0.18380339,0.28628435, 2.96108998,1.18267783,5.78419118,2.70861763,0.66287857,1.10397741,0.27160971, -15.37506924) ) # Two of the factors are used to define an array # of panels with the third showing up as bars along the x axis. # How do I color the bars according to their sign (red 0, blue 0) # conditional on the value of S2 .025 - in which case color them gray? # Initially, I tried to pass a character vector specifying colors, # which does not achieve the desired result: library(lattice) library(latticeExtra) df1$barcols - ifelse(df1$S1 .025, gray, ifelse( df1$S2 0, blue,red)) ctp - xyplot(S2 ~ F2 | F1 + F3, data=df1, as.table=TRUE, ylim=c(-10,10), panel = function(x,y){ panel.barchart(x,y,horizontal=FALSE, origin=0, col=df1$barcols ) } ) useOuterStrips(ctp) # Any help you can provide would be greatly appreciated. # Thank you! Itis better to keep the idea of grouping and the actual colors distinct. df1 - transform(df1, colgrp = factor(ifelse(S1 .025, A, ifelse( df1$S2 0, B, C)), levels = c(A, B, C))) barchart(S2 ~ F2 | F1 + F3, stack = TRUE, data=df1, as.table=TRUE, ylim=c(-10,10), groups = colgrp, par.settings = simpleTheme(col = c(grey, blue, red))) -Deepayan [[alternative HTML version deleted]] __ 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.
[R] conditional assignment of colors in xyplot()
# Dear R Community, # I have this data frame: df1 - data.frame( F1 = factor( c( rep(D1,12),rep(D2,12),rep(D3,12) ) ), F2 = factor( rep( rep( paste(O,1:6,sep=), rep(2,6) ), 3) ), F3 = factor( rep( c(V1,V2), 18 ) ), S1 = c(8.840955e-02,2.546822e-01,7.562658e-01,8.801181e-01,6.041766e-02,2.172731e-01,6.542187e-98, 7.067840e-04,1.430933e-01,9.764401e-01,1.380848e-05,1.192620e-01,9.107259e-01,1.235232e-01, 3.021847e-01,1.331351e-01,5.272103e-01,3.663577e-01,1.539690e-38,2.224451e-01,2.873052e-01, 5.110313e-01,7.840802e-01,8.210762e-10,1.553356e-01,4.173335e-01,5.964021e-01,4.955694e-01, 8.849240e-02,5.739598e-01,1.879075e-17,1.071003e-03,7.298928e-01,6.347287e-01,8.884034e-01, 4.460295e-11), S2 = c(1.32249139,1.02831831,-0.09650252,-0.05454486,2.62105492,2.00310250,8.07269064, -1.55397883,1.77390551,0.04161954,7.14188540,-2.98033713,-0.49904251,-0.74309058, -0.49904251,-0.74309058,1.22492623,-1.79003492,7.60003121,-0.74549596,2.53418936, -1.60112296,0.67131380,-15.31744351,-0.18380339,0.28628435,-0.18380339,0.28628435, 2.96108998,1.18267783,5.78419118,2.70861763,0.66287857,1.10397741,0.27160971, -15.37506924) ) # Two of the factors are used to define an array # of panels with the third showing up as bars along the x axis. # How do I color the bars according to their sign (red 0, blue 0) # conditional on the value of S2 .025 - in which case color them gray? # Initially, I tried to pass a character vector specifying colors, # which does not achieve the desired result: library(lattice) library(latticeExtra) df1$barcols - ifelse(df1$S1 .025, gray, ifelse( df1$S2 0, blue,red)) ctp - xyplot(S2 ~ F2 | F1 + F3, data=df1, as.table=TRUE, ylim=c(-10,10), panel = function(x,y){ panel.barchart(x,y,horizontal=FALSE, origin=0, col=df1$barcols ) } ) useOuterStrips(ctp) # Any help you can provide would be greatly appreciated. # Thank you! # nathan.pelleg...@gmail.com [[alternative HTML version deleted]] __ 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.