[R] lattice: double y - problem changing axis color after doubleYScale

Duncan Mackay dulcalma at bigpond.com
Tue Sep 24 11:32:33 CEST 2013


Hi Anna

I have never used doubleYScale and I am not sure what you want but try this

You need to do this first if columns not correct saves doing later - I
forgot to mention it for Week but as you have given the order for the
factors do it as well

SummPdata[,1] <- as.numeric(SummPdata[,1])
SummNdata[,1] <- as.numeric(SummNdata[,1])
SummPdata[,3] <- factor(SummPdata[,3], levels = c('B1', 'H2', 'H3', 'H4'))
SummNdata[,3] <- factor(SummNdata[,3], levels = c('B1', 'H2', 'H3', 'H4'))

As the data has a similar structure combine data

summData = rbind(SummPdata, SummNdata)
summData$element <- rep(c("P","N"), each = 32)

Just quickly cut and pasted the colours etc

useOuterStrips(strip      = strip.custom(#factor.levels = ,
                                         par.strip.text =
list(fontfamily="serif" )),
               strip.left = strip.custom(#factor.levels = ,
                                         par.strip.text =
list(fontfamily="serif" )),
               xyplot(mean ~ Week | Station*interaction(element,fyear),
data= summData,
                cex=1.1,
                par.settings = list(strip.background = list(col =
"transparent"),
                                    add.text = list(fontfamily = "serif")),
                #strip      = strip.custom(#factor.levels = ,
                #                         par.strip.text =
list(fontfamily="serif" )),
               scales = list(alternating = c(3,3),
                              fontfamily = "serif"),
                ylab = list(text = "Pc", fontfamily = "serif"),
                xlab=list(text = "Week", fontfamily = "serif"),
                key  = list(text = list(labels = c("P","N")),
                            points = list(pch = c(2,20), col =
c("red","blue")),
                            lines  = list(lty = c(1,2)), col =
c("red","blue")),
                type = "o",
                ylim = c(0,70),
                subscripts = TRUE,
                panel = function(x,y, subscripts, ...){

                          pnl = panel.number()

                          panel.xyplot(x,y, type = "o", col =
c("red","blue")[ifelse(pnl <9, 1,2)], lty = c(1,2)[ifelse(pnl <9, 1,2)], pch
= c(2,20)[ifelse(pnl <9, 1,2)])
                          # sd
                          # upper
                          panel.arrows(x0 = x, x1 = x,
                                       y0 = y, y1 = y+ summData[subscripts,
"sd"]/2, angle = 90, length = 0.04, col = c("red","blue")[ifelse(pnl <9,
1,2)])
                          # lower
                          panel.arrows(x0 = x, x1 = x,
                                       y0 = y, y1 = y- summData[subscripts,
"sd"]/2, angle = 90, length = 0.04, col = c("red","blue")[ifelse(pnl <9,
1,2)])

                        }# panel
                )#
) ## useOuterStrips

You can fill in the LHS strip with a vector of names as required

If you want to amend the ylimits  see ?combineLimits  in the latticeExtra
package

Duncan


-----Original Message-----
From: Anna Zakrisson Braeunlich [mailto:anna.zakrisson at su.se] 
Sent: Tuesday, 24 September 2013 18:28
To: Duncan Mackay
Subject: RE: [R] lattice: double y - problem changing axis color after
doubleYScale

OK, now the code works - thank you. The error bars are added beautifully!

As I am using doubleYScale, I am having the problem that the first y-axis is
covered by the left hand strip - how can I solve this? I really much prefer
using outer.strip() and have one strip vertically on the plot. It saves plot
space.

I am still having the problem of colourful y-axis when using doubleYScale()
- how can I make them black?
I have the same problem with the error bars - they are also default colour
no matter what I tell them to be.
Is there a way around this except Adobe Photoshop?

Here is the working script with dummy data (you can just run the lot if you
have time). I really appreciate your code - thanks! ...and sorry for being a
bit daft and sending a reply before thinking it through properly.

Ndata <- data.frame(
  Ncellpercent = rnorm(400, mean = rep(c(14, 18, 65), each = 40),
                       sd = rep(c(1, 3, 6), each = 40)),
  fyear = rep(c('2007', '2008'), each = 100*2),
  Station = sample(c('B1', 'H2', 'H3', 'H4'), 400, replace = TRUE),
  Week = sample(c('19', '21', '23', '25'), 400, replace = TRUE))

Pdata <- data.frame(
  Ppercentcell = rnorm(400, mean = rep(c(4, 17, 22), each = 40),
                       sd = rep(c(0.1, 0.2, 0.4), each = 40)),
  fyear = rep(c('2007', '2008'), each = 100*2),
  Station = sample(c('B1', 'H2', 'H3', 'H4'), 400, replace = TRUE),
  Week = sample(c('19', '21', '23', '25'), 400, replace = TRUE))

SummNdata <- ddply(Ndata, .(Week, fyear, Station), summarise, 
                   mean = mean(Ncellpercent), 
                   sd = sd(Ncellpercent))
names(Pdata)
SummPdata <- ddply(Pdata, .(Week, fyear, Station), summarise, 
                   mean = mean(Ppercentcell), 
                   sd = sd(Ppercentcell)) SummPdata

plotNtest <- useOuterStrips(strip      = strip.custom(factor.levels =
c("B1", "H2", "H3", "H4"),
  par.strip.text =
    list(fontfamily="serif" )),
               strip.left = strip.custom(factor.levels = c("2007", "2008",
"2009"),
                 par.strip.text =
                   list(fontfamily="serif" )),
               xyplot(mean ~ Week | Station*fyear, data= SummNdata,
                      col="black",
                      pch=1,
                      cex=1.1,
                      lty=1,
                      par.settings = list(strip.background = list(col =
 
"transparent"),
                                          add.text = list(fontfamily =
"serif"
                                          )),
                      strip      = strip.custom(factor.levels = c("B1",
"H2", "H3", "H4"),
                                               par.strip.text =
                      list(fontfamily="serif" )),
               scales = list(alternating = c(3,3),
                             fontfamily = "serif"),
               ylab = list(text = "Nc", fontfamily = "serif"),
               xlab=list(text = "Week", fontfamily = "serif"),
               key  = list(text = list(labels = c("Pc","Nc")),
                           points = list(pch = c(1,2)),
                           lines  = list(lty = c(1,2))),
               type = "o",
               ylim = c(0,70),
               subscripts = TRUE,
               panel = function(x,y, subscripts, ...){
                 panel.xyplot(x,y, ...)
                 # sd
                 # upper
                 panel.arrows(x0 = x, x1 = x,
                              y0 = y, y1 = y+ SummNdata[subscripts,
                                                        "sd"]/2, angle = 90,
length = 0.04)
                 # lower
                 panel.arrows(x0 = x, x1 = x,
                              y0 = y, y1 = y- SummNdata[subscripts,
                                                        "sd"]/2, angle = 90,
length = 0.04)
                 
                 panel.xyplot(x+0.1, SummNdata[subscripts,"mean"],
                              type ="o",pch = 20, lty =1, col = "black")
                 # sd
                 # upper
                 panel.arrows(x0 = x+0.1, x1 = x+0.1,
                              y0 = SummNdata[subscripts,"mean"], y1
                              = SummNdata[subscripts,"mean"] +
SummNdata[subscripts, "sd"]/2, angle = 90,
                              length = 0.04)
                 # lower
                 panel.arrows(x0 = x+0.1, x1 = x+0.1,
                              y0 = SummNdata[subscripts,"mean"], y1
                              = SummNdata[subscripts,"mean"]-
SummNdata[subscripts, "sd"]/2, angle = 90,
                              length = 0.04)
                 
               }# panel
)#
) ## useOuterStrips

plotPtest <- useOuterStrips(strip      = strip.custom(factor.levels =
c("B1", "H2", "H3", "H4"),
                                                 par.strip.text =
                                                   list(fontfamily="serif"
)),
                       strip.left = strip.custom(factor.levels = c("2007",
"2008", "2009"),
                                                 par.strip.text =
                                                   list(fontfamily="serif"
)),
                       xyplot(mean ~ Week | Station*fyear, data= SummPdata,
                              col="black",
                              pch=2,
                              cex=1.1,
                              lty=2,
                              par.settings = list(strip.background =
list(col =
 
"transparent"),
                                                  add.text = list(fontfamily
= "serif"
                                                  )),
                              strip      = strip.custom(factor.levels =
c("B1", "H2", "H3", "H4"),
                                                        par.strip.text =
 
list(fontfamily="serif" )),
                              scales = list(alternating = c(3,3),
                                            fontfamily = "serif"),
                              ylab = list(text = "Pc", fontfamily =
"serif"),
                              xlab=list(text = "Week", fontfamily =
"serif"),
                              type = "o",
                              ylim = c(0,70),
                              subscripts = TRUE,
                              panel = function(x,y, subscripts, ...){
                                panel.xyplot(x,y, ...)
                                # sd
                                # upper
                                panel.arrows(x0 = x, x1 = x,
                                             y0 = y, y1 = y+
SummPdata[subscripts,
 
"sd"]/2, angle = 90, length = 0.04)
                                # lower
                                panel.arrows(x0 = x, x1 = x,
                                             y0 = y, y1 = y-
SummPdata[subscripts,
 
"sd"]/2, angle = 90, length = 0.04)
                                
                                panel.xyplot(x+0.1,
SummPdata[subscripts,"mean"],
                                             type ="o",pch = 20, lty =1, col
= "black")
                                # sd
                                # upper
                                panel.arrows(x0 = x+0.1, x1 = x+0.1,
                                             y0 =
SummPdata[subscripts,"mean"], y1
                                             = SummPdata[subscripts,"mean"]
+ SummPdata[subscripts, "sd"]/2, angle = 90,
                                             length = 0.04)
                                # lower
                                panel.arrows(x0 = x+0.1, x1 = x+0.1,
                                             y0 =
SummPdata[subscripts,"mean"], y1
                                             = SummPdata[subscripts,"mean"]-
SummPdata[subscripts, "sd"]/2, angle = 90,
                                             length = 0.04)
                                
                              }# panel
                       )#
) ## useOuterStrips


plotNtest
plotPtest
doubleYScale(plotNtest, plotPtest, add.ylab2 = TRUE)

Can I make the
Anna Zakrisson Braeunlich
PhD student

Department of Ecology, Environment and Plant Sciences Stockholm University
Svante Arrheniusv. 21A
SE-106 91 Stockholm
Sweden/Sverige

Lives in Berlin.
For paper mail:
Katzbachstr. 21
D-10965, Berlin - Kreuzberg
Germany/Deutschland

E-mail: anna.zakrisson at su.se
Tel work: +49-(0)3091541281
Mobile: +49-(0)15777374888
LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b

><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• `•. .><((((º>`•. . • 
>`•. .• `•. .><((((º>

________________________________________
From: Duncan Mackay [dulcalma at bigpond.com]
Sent: 22 September 2013 05:00
To: Anna Zakrisson Braeunlich
Cc: R
Subject: RE: [R] lattice: double y - problem changing axis color after
doubleYScale

Hi  Anna

I am not sure what you want but the following should get you part of the way

I prefer to keep all my parameters within the function rather than use
themes.

useOuterStrips(strip      = strip.custom(#factor.levels = ,
                                         par.strip.text =
list(fontfamily="serif" )),
               strip.left = strip.custom(#factor.levels = ,
                                         par.strip.text =
list(fontfamily="serif" )), xyplot(mean ~ Week | Station*fyear, data=
SummPdata,
                col="black",
                pch=2,
                cex=1.1,
                lty=2,
                par.settings = list(strip.background = list(col =
"transparent"),
                                    add.text = list(fontfamily = "serif"
)),
                #strip      = strip.custom(#factor.levels = ,
                #                         par.strip.text =
list(fontfamily="serif" )),
               scales = list(alternating = c(3,3),
                              fontfamily = "serif"),
                ylab = list(text = "Pc", fontfamily = "serif"),
                xlab=list(text = "Week", fontfamily = "serif"),
                key  = list(text = list(labels = c("P","N")),
                            points = list(pch = c(2,20)),
                            lines  = list(lty = c(1,2))),
                type = "o",
                ylim = c(0,70),
                subscripts = TRUE,
                panel = function(x,y, subscripts, ...){
                          panel.xyplot(x,y, ...)
                          # sd
                          # upper
                          panel.arrows(x0 = x, x1 = x,
                                       y0 = y, y1 = y+ SummPdata[subscripts,
"sd"]/2, angle = 90, length = 0.04)
                          # lower
                          panel.arrows(x0 = x, x1 = x,
                                       y0 = y, y1 = y- SummPdata[subscripts,
"sd"]/2, angle = 90, length = 0.04)

                          panel.xyplot(x+0.1, SummNdata[subscripts,"mean"],
type ="o",pch = 20, lty =1, col = "black")
                          # sd
                          # upper
                          panel.arrows(x0 = x+0.1, x1 = x+0.1,
                                      y0 = SummNdata[subscripts,"mean"], y1
= SummNdata[subscripts,"mean"] + SummNdata[subscripts, "sd"]/2, angle = 90,
length = 0.04)
                          # lower
                          panel.arrows(x0 = x+0.1, x1 = x+0.1,
                                       y0 = SummNdata[subscripts,"mean"], y1
= SummNdata[subscripts,"mean"]- SummNdata[subscripts, "sd"]/2, angle = 90,
length = 0.04)

                        }# panel
                )#
) ## useOuterStrips

Note that I offset the N data so that the error bars are more legible

For sd I just divided them by 2 so you will have to work out what you
require

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Anna Zakrisson Braeunlich
Sent: Thursday, 19 September 2013 20:21
To: r-help at r-project.org
Subject: [R] lattice: double y - problem changing axis color after
doubleYScale

Hi,

I have had some troubles using doubleYScale. No matter what I try, I cant
manage to change the color of the y-axis in the end. I have to produce a
black and white plot. There is also something I do not understand regarding
fontfamilyj="serif" when using it in:
strip=strip.custom()

Maybe someone has a better idea for defining which line and dots belong to
which y-axis when not using a colorcode than the one I had.

I have annotated my questions in the code below.

Thank you for your time!


Here is some dummy data:

Ndata <- data.frame(
  Ncellpercent = rnorm(400, mean = rep(c(14, 18, 65), each = 40),
               sd = rep(c(1, 3, 6), each = 40)),
  fyear = rep(c('2007', '2008'), each = 100*2),
  Station = sample(c('B1', 'H2', 'H3', 'H4'), 400, replace = TRUE),
  Week = sample(c('19', '21', '23', '25'), 400, replace = TRUE))

Pdata <- data.frame(
  Ppercentcell = rnorm(400, mean = rep(c(4, 17, 22), each = 40),
               sd = rep(c(0.1, 0.2, 0.4), each = 40)),
  fyear = rep(c('2007', '2008'), each = 100*2),
  Station = sample(c('B1', 'H2', 'H3', 'H4'), 400, replace = TRUE),
  Week = sample(c('19', '21', '23', '25'), 400, replace = TRUE))

SummNdata <- ddply(Ndata, .(Week, fyear, Station), summarise,
                   mean = mean(Ncellpercent),
                   sd = sd(Ncellpercent))
names(Pdata)
SummPdata <- ddply(Pdata, .(Week, fyear, Station), summarise,
                   mean = mean(Ppercentcell),
                   sd = sd(Ppercentcell))
library(lattice)
library(latticeExtra)
library(HH)

font.settings <- list( font = 1, cex = 1.2, fontfamily = "serif")

my.theme <- list(
  par.xlab.text = font.settings,
  par.ylab.text = font.settings,
  axis.text = font.settings,
  par.sub=font.settings)

plotN <- xyplot(mean ~ Week | Station*fyear,
                col="black",
                pch=1,
                cex=1.1,
                lty=1,
                strip = strip.custom(bg = 'white', style=1), # why can I not
use fontfamily="serif" here ???
                key=list(text=list(c(""),
                                   col=c("black")),
                         points=list(pch=1, lty=1, cex=1.5,
                                     col=c("black")),
                         columns=1, border=F,
                         x = 0.02, y = 0.55, corner = c(2, 2),
                         title="", cex.title=1.3),
                ylab = ("Nc"),
                xlab="Week",
                data= SummNdata,type="o",
                par.settings = my.theme) plotN # I would like to add the
standard deviations (sd) to the plot. I have tried some stuff, # but for
some reason, it does not seem to work. How would I go about this?


plotP <- xyplot(mean ~ Week | Station*fyear,
                col="black",
                pch=2,
                cex=1.1,
                lty=2,
                strip = strip.custom(bg = 'white', style=1), # why can I not
use fontfamily="serif" here ???
                key=list(text=list(c(""),
                                   col=c("black")),
                         points=list(pch=1, lty=1, cex=1.5,
                                     col=c("black")),
                         columns=1, border=F,
                         x = 0.2, y = 0.2, corner = c(2, 2),
                         title="", cex.title=1.3),
                ylab = ("Pc"),
                xlab="Week",
                data= SummPdata,type="o",
                par.settings = my.theme) plotP

doubleYScale(plotN, plotP, add.ylab2 = TRUE)  #Why can I not change the axis
color by adding to this argument?

# I want the y1 and y2 axes to be defined not by color, but by shape and
linetype.
# I have managed to draw the shapes (defined by Nc and Pc) by the y1 and y2
axes, but I do not manage to get the lines # though the shape - ideas?
# Alternative ways that are not based on color (I have to do this black and
white).
# Is there possible to add shapes to the axis text? such as:
# --O-- Nc  on the left y-axis (but with lty=1: I could not do a non-dotted
line on the keybord).

with kind regards

Anna Zakrisson Braeunlich
PhD student

Department of Ecology, Environment and Plant Sciences Stockholm University
Svante Arrheniusv. 21A
SE-106 91 Stockholm
Sweden/Sverige

Lives in Berlin.
For paper mail:
Katzbachstr. 21
D-10965, Berlin - Kreuzberg
Germany/Deutschland

E-mail: anna.zakrisson at su.se
Tel work: +49-(0)3091541281
Mobile: +49-(0)15777374888
LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b

><((((:>`?. . ? `?. .? `?. . ><((((:>`?. . ? `?. .? `?. .><((((:>`?. . ?
>`?. .? `?. .><((((:>

        [[alternative HTML version deleted]]



More information about the R-help mailing list