質問

Hi everybody I am trying to solve a little problem with a lattice graphic in R. I build a double y axis plot with lattice. It works awesome. I add the code and dput version of my data (xx list of data frames in the final part):

library(lattice)
library(latticeExtra)
#First graph
#Format
comma_fomatter <- function (lim, logsc = FALSE, at = NULL, ...) 
{
  ans <- yscale.components.default(lim = lim, logsc = logsc, 
                                   at = at, ...)
  xx = as.numeric(ans$left$labels$labels)
  ans$left$labels$labels <- formatC(xx, format="fg",big.mark = ",")
  ans
}
#Plot
DD=barchart(a1 ~ a5,xx, yscale.components =comma_fomatter)
#Second graph
#Format
percent1 <- function(x, digits = 2, format = "f", ...)
{
  paste(formatC(100 * x, format = format, digits = digits, ...), "%", sep = "")
}
percent <- function (lim, logsc = FALSE, at = NULL, ...) 
{
  ans <- yscale.components.default(lim = lim, logsc = logsc, 
                                   at = at, ...)
  xx = as.numeric(ans$left$labels$labels)
  ans$left$labels$labels <- percent1(xx)
  ans
}
#Second plot
D3=xyplot(a3+a4 ~ a5, xx, type = "b",yscale.components=percent)
#Final graph
DF=doubleYScale(DD, D3,style1 = 3, style2 = 0, add.ylab2 = FALSE,text = c("a3", "a4","a1"),main="Nuevo Gráfico")

All works perfect but when I plot DF I got this: enter image description here

How you can see the left side y axis doesn't show its values and I think this fact is due to the size of square that has all plots. My question is if it is any way to reduce the size of that square and showing the complete values inside of y axis scale. The dput() version of my list of data frames is the next:

xx=structure(list(a1 = c(560492.29, 1433973.37, 3016748.66, 4241217.73, 
6251742.27, 6757161.24, 7408081.05, 7899980.33), a2 = 1:8, a3 = c(0, 
0.00793734405263048, 0.0129172080248807, 0.0324335034787285, 
0.0397094648625047, 0.0555107413716237, 0.0521541081141384, 0.0515512600016815
), a4 = c(0.0142731668976214, 0.010460445301017, 0.0928151568317925, 
0.0707344020275045, 0.0303915279604129, 0.0517968992552855, 0.0202481585970229, 
0.0253165187311296), a5 = structure(1:8, .Label = c("abr 2013", 
"may 2013", "jun 2013", "jul 2013", "ago 2013", "sep 2013", "oct 2013", 
"nov 2013"), class = "factor")), .Names = c("a1", "a2", "a3", 
"a4", "a5"))

I have looked for any advice at lattice graphics book but I can't solve this element. Many thanks for your help.

役に立ちましたか?

解決

Not sure why it didn't work for you. I've taken the liberty to reformat your code, other than that nothing important has changed. Anyway, here's the plot:

enter image description here

EDIT: The problem seems to be related to the settings of lattice (see ?trellis.par.get). I've updated the source accordingly, that should solve the problem.

And here's the source:

library(lattice)
library(latticeExtra)

# xx <- ... paste your dataset here

# Adapt these to your needs:
parSettings <- list(layout.widths=list(left.padding=5))

#First graph
comma_formatter <- function (lim, logsc = FALSE, at = NULL, ...)  {
  ans <- yscale.components.default(lim = lim, logsc = logsc, at = at, ...)
  xxPrime <- as.numeric(ans$left$labels$labels)
  ans$left$labels$labels <- formatC(xxPrime, format = "fg", big.mark = ",")
  ans
}
#Plot
DD <- barchart(a1 ~ a5, xx, yscale.components = comma_formatter, par.settings = parSettings)

#Second graph
percent <- function (lim, logsc = FALSE, at = NULL, ...) {
  # Helper function (never needed elsewhere, so we can declare it here)
  percent1 <- function(x, digits = 2, format = "f", ...)
    paste(formatC(100 * x, format = format, digits = digits, ...), "%", sep = "")

  ans <- yscale.components.default(lim = lim, logsc = logsc, at = at, ...)
  xxPrime <- as.numeric(ans$left$labels$labels)
  ans$left$labels$labels <- percent1(xxPrime)
  ans
}
#Plot
D3 <- xyplot(a3+a4 ~ a5, xx, type = "b", yscale.components = percent, par.settings = parSettings)

#Final graph
DF <- doubleYScale(DD, D3, style1 = 3, style2 = 0, add.ylab2 = FALSE,
                   text = c("a3", "a4", "a1"), main = "Nuevo Gráfico")
plot(DF)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top