On Wed, 22 Aug 2018, Rich Shepard wrote:> More when I have results.Almost there. I've read the auto.key section in ?barchart and looked at examples from stackoverflow on the web without seeing my syntax errors. I would like help on two issues: 1. What I want is to have the legend text in black and the colored rectangles match the black and grey of the bars. Instead, I get the legend text colored and have no idea where the default colors in the rectangles got there. 2. I've not found how to have the years (rather than the sequence of years) as the x-axis labels. Here are the dput() output and the script: structure(list(year = c(1989L, 1989L, 1990L, 1990L, 1991L, 1991L, 1993L, 1993L, 1994L, 1994L, 1995L, 1995L, 1996L, 1996L, 1997L, 1997L, 1998L, 1998L, 1999L, 1999L, 2000L, 2000L, 2001L, 2001L, 2002L, 2002L, 2003L, 2003L, 2004L, 2004L, 2005L, 2005L, 2006L, 2006L, 2007L, 2007L, 2008L, 2008L, 2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2016L, 2016L, 2017L, 2017L, 2018L, 2018L), value = c(91.17, 93.32, 91.22, 93.43, 91.24, 92.89, 91.14, 93.02, 93.92, 95.74, 94.34, 96.85, 91.32, 95.86, 91.36, 94.25, 91.24, 93.67, 94.33, 97.42, 94.33, 97.42, 94, 94.99, 94.32, 96.58, 94.02, 96.57, 94.19, 96.32, 94.05, 95.96, 94.21, 97.4, 94.21, 97.28, 94.32, 96.72, 94.13, 97.43, 94.27, 95.95, 94.34, 97.82, 94.23, 97, 94.25, 96.6, 94.15, 96.24, 94.01, 96.68, 94.09, 96.96, 94.31, 96.39, 94.35, 96.95), type = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Max", "Med"), class = "factor")), class = "data.frame", row.names = c(NA, -58L)) med_max <- barchart(value ~ year, data=stage_heights, panel = lattice.getOption("panel.barchart"), default.prepanel = lattice.getOption("prepanel.default.barchart"), box.ratio = 2, horizontal=FALSE, auto.key=list(space='right', col=c('black', 'grey')), groups=factor(type,labels=c('Median','Maximum')), beside=TRUE, col = c('grey','black'), labels=list(c(1989,1990,1991,1992, 1993,1994, 1995,1996,1997,1998,1999,2000,2001, 2002,2003,2004,2005,2006,2007,2008, 2009,2010,2011,2012,2013,2014,2015, 2016,2017,2018), scales=list(x=list(rot=90)), main = 'Median and Maximum Stage Heights', ylab = 'Elevation (masl)', xlab = 'Year') print(med_max) Rich
color for the legend comes from trellis.par.get You can control that for an individual plot with the par.settings argument. tmp <- data.frame(y=sample(10), group=rep(c("Median", "Maximum"), each=5), year=factor(rep(1998:1999, length=10))) barchart(y ~ year, data=tmp, group=group, auto.key=TRUE, main="default legend", col = c('grey','black')) barchart(y ~ year, data=tmp, group=group, auto.key=TRUE, main="what you want", par.settings=list(superpose.polygon=list(col=c('grey','black')))) names(trellis.par.get()) trellis.par.get()$superpose.polygon On Fri, Aug 24, 2018 at 3:37 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:> On Wed, 22 Aug 2018, Rich Shepard wrote: > >> More when I have results. > > > Almost there. I've read the auto.key section in ?barchart and looked at > examples from stackoverflow on the web without seeing my syntax errors. I > would like help on two issues: > > 1. What I want is to have the legend text in black and the colored > rectangles match the black and grey of the bars. Instead, I get the legend > text colored and have no idea where the default colors in the rectangles got > there. > > 2. I've not found how to have the years (rather than the sequence of > years) as the x-axis labels. > > Here are the dput() output and the script: > > structure(list(year = c(1989L, 1989L, 1990L, 1990L, 1991L, 1991L, 1993L, > 1993L, 1994L, 1994L, 1995L, 1995L, 1996L, 1996L, 1997L, 1997L, 1998L, 1998L, > 1999L, 1999L, 2000L, 2000L, 2001L, 2001L, 2002L, 2002L, 2003L, 2003L, 2004L, > 2004L, 2005L, 2005L, 2006L, 2006L, 2007L, 2007L, 2008L, 2008L, 2009L, 2009L, > 2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L, 2013L, 2014L, 2014L, 2015L, > 2015L, 2016L, 2016L, 2017L, 2017L, 2018L, 2018L), value = c(91.17, 93.32, > 91.22, 93.43, 91.24, 92.89, 91.14, 93.02, 93.92, 95.74, 94.34, 96.85, 91.32, > 95.86, 91.36, 94.25, 91.24, 93.67, 94.33, 97.42, 94.33, 97.42, 94, 94.99, > 94.32, 96.58, 94.02, 96.57, 94.19, 96.32, 94.05, 95.96, 94.21, 97.4, 94.21, > 97.28, 94.32, 96.72, 94.13, 97.43, 94.27, 95.95, 94.34, 97.82, 94.23, 97, > 94.25, 96.6, 94.15, 96.24, 94.01, 96.68, 94.09, 96.96, 94.31, 96.39, 94.35, > 96.95), type = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, > 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, > 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, > 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Max", "Med"), class > "factor")), class = "data.frame", row.names = c(NA, -58L)) > > med_max <- barchart(value ~ year, data=stage_heights, > panel = lattice.getOption("panel.barchart"), > default.prepanel > lattice.getOption("prepanel.default.barchart"), > box.ratio = 2, horizontal=FALSE, > auto.key=list(space='right', > > col=c('black', 'grey')), > groups=factor(type,labels=c('Median','Maximum')), > beside=TRUE, > col = c('grey','black'), > labels=list(c(1989,1990,1991,1992, 1993,1994, > > 1995,1996,1997,1998,1999,2000,2001, > > 2002,2003,2004,2005,2006,2007,2008, > > 2009,2010,2011,2012,2013,2014,2015, > 2016,2017,2018), > > scales=list(x=list(rot=90)), > main = 'Median and > Maximum Stage Heights', > ylab = 'Elevation > (masl)', xlab = 'Year') > print(med_max) > > > Rich > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
For the legend, you can use the full "key" argument for more control. The docs in ?xyplot for "key" Should answer your questions. "col" controls text color within the "text" component and rectangle color within the "rectangle" component , for example. I think this should work as an alternative to specifying the par.settings components, but I haven't checked. For the scales, again, the docs provide the answer: the "at" and "labels" components of "x" component of the scales lists can explicitly control the x -labels, e.g. scales = list( x = list( at = ..., labels = ...) etc. If you are uncomfortable with the R lattice help docs, and you intend to continue to use lattice plots (a good idea; ggplot is an alternative of course), Deepayan has written a book that you might wish to get: http://lmdvr.r-forge.r-project.org/figures/figures.html There are also numerous web tutorials. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Aug 24, 2018 at 12:38 PM Rich Shepard <rshepard at appl-ecosys.com> wrote:> On Wed, 22 Aug 2018, Rich Shepard wrote: > > > More when I have results. > > Almost there. I've read the auto.key section in ?barchart and looked at > examples from stackoverflow on the web without seeing my syntax errors. I > would like help on two issues: > > 1. What I want is to have the legend text in black and the colored > rectangles match the black and grey of the bars. Instead, I get the legend > text colored and have no idea where the default colors in the rectangles > got > there. > > 2. I've not found how to have the years (rather than the sequence of > years) as the x-axis labels. > > Here are the dput() output and the script: > > structure(list(year = c(1989L, 1989L, 1990L, 1990L, 1991L, 1991L, > 1993L, 1993L, 1994L, 1994L, 1995L, 1995L, 1996L, 1996L, 1997L, > 1997L, 1998L, 1998L, 1999L, 1999L, 2000L, 2000L, 2001L, 2001L, > 2002L, 2002L, 2003L, 2003L, 2004L, 2004L, 2005L, 2005L, 2006L, > 2006L, 2007L, 2007L, 2008L, 2008L, 2009L, 2009L, 2010L, 2010L, > 2011L, 2011L, 2012L, 2012L, 2013L, 2013L, 2014L, 2014L, 2015L, > 2015L, 2016L, 2016L, 2017L, 2017L, 2018L, 2018L), value = c(91.17, > 93.32, 91.22, 93.43, 91.24, 92.89, 91.14, 93.02, 93.92, 95.74, > 94.34, 96.85, 91.32, 95.86, 91.36, 94.25, 91.24, 93.67, 94.33, > 97.42, 94.33, 97.42, 94, 94.99, 94.32, 96.58, 94.02, 96.57, 94.19, > 96.32, 94.05, 95.96, 94.21, 97.4, 94.21, 97.28, 94.32, 96.72, > 94.13, 97.43, 94.27, 95.95, 94.34, 97.82, 94.23, 97, 94.25, 96.6, > 94.15, 96.24, 94.01, 96.68, 94.09, 96.96, 94.31, 96.39, 94.35, > 96.95), type = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, > 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, > 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, > 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, > 1L), .Label = c("Max", "Med"), class = "factor")), class = "data.frame", > row.names = c(NA, > -58L)) > > med_max <- barchart(value ~ year, data=stage_heights, > panel = lattice.getOption("panel.barchart"), > default.prepanel > lattice.getOption("prepanel.default.barchart"), > box.ratio = 2, horizontal=FALSE, > auto.key=list(space='right', > > col=c('black', 'grey')), > groups=factor(type,labels=c('Median','Maximum')), > beside=TRUE, > col = c('grey','black'), > labels=list(c(1989,1990,1991,1992, 1993,1994, > > 1995,1996,1997,1998,1999,2000,2001, > > 2002,2003,2004,2005,2006,2007,2008, > > 2009,2010,2011,2012,2013,2014,2015, > > 2016,2017,2018), > > scales=list(x=list(rot=90)), > main = 'Median > and Maximum Stage Heights', > ylab > 'Elevation (masl)', xlab = 'Year') > print(med_max) > > Rich > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
On Fri, 24 Aug 2018, Bert Gunter wrote:> For the legend, you can use the full "key" argument for more control.Bert, This I did.> For the scales, again, the docs provide the answer: the "at" and "labels" > components of "x" component of the scales lists can explicitly control the > x -labels, e.g.A bit of trial-and-error got this working, too. Now the plot command works as desired: barchart(value ~ year, data=stage_heights, panel = lattice.getOption("panel.barchart"), default.prepanel = lattice.getOption("prepanel.default.barchart"), box.ratio = 2, horizontal=FALSE, key=list(c(0.2,0.3), columns=2, text=list(c('Median','Maximum')), rect=list(col=c('black', 'grey'))), groups=factor(type,labels=c('Median','Maximum')), beside=TRUE, col = c('grey','black'), scales=list(x=list(at=rep(1:29), labels=rep(1989:2018),rot=90)), main = 'Median and Maximum Stage Heights', ylab = 'Elevation (masl)', xlab = 'Year') (Emacs w/ESS does the formatting). I suppose that the Maximum bar is plotted to the left because alphabetically it preceeds Medium. I can live with this. Deepayan's book was one of the first I bought years ago. I've not before had plots that required more in-depth knowledge of panels, keys, and scales so I do appreciate your patient mentoring. Best regards, Rich
On Fri, 24 Aug 2018, Richard M. Heiberger wrote:> color for the legend comes from trellis.par.get > You can control that for an individual plot with the par.settings argument. > tmp <- data.frame(y=sample(10), > group=rep(c("Median", "Maximum"), each=5), > year=factor(rep(1998:1999, length=10))) > > barchart(y ~ year, data=tmp, group=group, auto.key=TRUE, main="default legend", > col = c('grey','black')) > > barchart(y ~ year, data=tmp, group=group, auto.key=TRUE, main="what you want", > par.settings=list(superpose.polygon=list(col=c('grey','black')))) > > names(trellis.par.get()) > trellis.par.get()$superpose.polygonThanks, Richard! Before venturing into par.settings I worked off of Bert's advice and careful reading of the ?xyplot details allowed me to fix the two remaining issues. Your suggestions will definitely be of value when I have other complex lattice plots to properly display. Best regards, Rich