Dear R Users, I have a basis question regarding the use of color in the lattice package. I read the ?barchart help page and searched the R archives but could not understand how to do it. I just need to plot a barchart using specific colors for my groups, e.g. green and red instead of the default lattice colors. How do I do that? If I say: barchart(x ~ a_factor, groups=my_groups, data=my_data, col=c('green', 'red'), auto.key=TRUE) then my barplot has the correct colors, but the legend does not. What is the correct approach to set colors in barchart? Thanks Alex [[alternative HTML version deleted]]
> I have a basis question regarding the use of color in the latticepackage. I> read the ?barchart help page and searched the R archives but could not > understand how to do it. > > I just need to plot a barchart using specific colors for my groups, e.g. > green and red instead of the default lattice colors. How do I do that? > > If I say: > barchart(x ~ a_factor, groups=my_groups, data=my_data,col=c('green',> 'red'), auto.key=TRUE) > > then my barplot has the correct colors, but the legend does not. What isthe> correct approach to set colors in barchart?Two possibilities: 1. Manually specify the colours of the rectangles in the key by using the key argument instead of auto.key my_cols <- c("green", "red", "blue") barchart(yield ~ site, groups=variety, data = barley, col=my_cols, key=list(text=list(levels(barley$variety)), rectangles=list(col=my_cols))) 2. Set superpose.polygon$col in the plot settings instead of using the col argument, e.g. barchart(yield ~ site, groups=variety, data = barley, auto.key=TRUE, par.settings=list(superpose.polygon=list(col=my_cols))) Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
> Thank you for a clear example, it works. I tried to play with > superpose.polygon before to no avail, this clarifies things.You're welcome.> Another question: would you know how to add gridlines to the plot? > I'd like to have a few horizontal gridlines on my barchart plot for > better readability. Do I have to write a wrapper for panel.barchart?Probably, but it's not as bad as it sounds. To reuse the previous example: barchart(yield ~ site, groups=variety, data = barley, panel=function(...){panel.barchart(...); panel.grid(v=0, ...)}) (The v=0 argument stop vertical grid lines being drawn.) Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}