1

I have made some graphics using doubleYscale command from latticeExtra. I'd like to reduce or even completely remove the white space between the vertical axes and coloured area -I mean, I want data lines to start and end immediatly next to axes-. My script is like this:

# -----------------------------------------------------------------------------#

library("xts")           ## se usa para manipular y graficar series temporales
library("lubridate")     ## se usa para manipular fechas
library("lattice")       ## se usa para graficar las series
library("latticeExtra")  ## se usa para pintar áreas en los gráficos
# -----------------------------------------------------------------------------#

fechas <- seq(as.Date('2017-05-22'), length.out = 365, by = 'days')

serie.0 <- xts::xts(x = runif(n = length(fechas)), order.by = fechas)
serie.5 <- xts::xts(x = runif(n = length(fechas)), order.by = fechas)

titulo <- list(label = 'Título', col = 'black', cex = 1.2)

umbrales.y <- c(0, 0.1, 0.3, 0.5, 0.7, 0.9, 1, 1.1)
etiq.der <- c("PMP", "10%AU", "30%AU", "50%AU", "70%AU", "90%AU", "CC", "")

marcas.x <- fechas[which(day(fechas) == 1)]
labels.x <- substr(as.character(month(marcas.x, label = T)), 1, 3)
scales.x <- list(at = marcas.x + 15, tck = 0, labels = labels.x, cex = 1.1, rot = 0, col = "black")
scales.y     <- list(at = umbrales.y, col = "black", rot = 0, labels = c())
scales.y.der <- list(at = umbrales.y, rot = 0, col = "black", labels = etiq.der)

plot.0 <- lattice::xyplot(serie.0 ~ fechas, panel = function(x, y) {

  panel.xyarea(x, y = umbrales.y[8], col = "cadetblue",
               border = NULL)
  panel.xyarea(x, y = umbrales.y[7], col = "darkgreen",
               border = NULL)
  panel.xyarea(x, y = umbrales.y[6], col = "green4",
               border = NULL)
  panel.xyarea(x, y = umbrales.y[5], col = "chartreuse3",
               border = NULL)
  panel.xyarea(x, y = umbrales.y[4], col = "darkolivegreen1",
               border = NULL)
  panel.xyarea(x, y = umbrales.y[3], col = "lightgoldenrod1",
               border = NULL)
  panel.xyarea(x, y = umbrales.y[2], col = "chocolate3",
               border = NULL)
  panel.xyarea(x, y = umbrales.y[1], col = "brown4",
               border = NULL)

  panel.abline(v = marcas.x, col = "darkgrey",
               lwd = 0.05)
  panel.abline(h = umbrales.y[4], col = "black",
               lwd = 2)
  panel.xyplot(x, y, type = "l", col = "darkblue",
               lwd = 2)},

  xlab = NULL,
  ylab = list('Almacenaje', cex = 1, col = 'black'),
  main = titulo,  
  ylim = c(- 0.1, 1),
  xlim = range(fechas),
  scales = list(relation = "free", col = "black", x = scales.x, y = scales.y),
  key = list(text = list(c("Último año", "Decil 5")),
             lines = list(col = c("darkblue", "black"),
                          lty = c(1,2), lwd = c(2,1.9))),
  par.settings = list(simpleTheme(col = 1),
                      layout.widths = list(ylab.axis.padding = -3)))

plot.1 <- lattice::xyplot(serie.5 ~ fechas, type = "l",
                          col = "black", lwd = 1.9, lty = 2,
                          ylim = c(- 0.1, 1),
                          xlim = range(fechas),
                          scales = list(relation = "free", x = scales.x, y = scales.y.der))

#png(filename = 'C:/Eugenia/graf.png', width = 600, height = 480)
print(latticeExtra::doubleYScale(plot.0, plot.1, style1 = 0, style2 = 0))
#dev.off()

An the result: My graph

I've tried changing the layout.widths parameters, but didn't find the way. Thank you in advance for any help!

Tung
  • 26,371
  • 7
  • 91
  • 115
eugemeb
  • 11
  • 1
  • You might want to switch to `ggplot` so that you will have greater support from the community. Here is a pretty good source https://learnr.wordpress.com/2009/07/15/ggplot2-version-of-figures-in-lattice-multivariate-data-visualization-with-r-part-5/ – Tung Aug 27 '18 at 22:56
  • Another one https://www.fromthebottomoftheheap.net/2013/10/23/time-series-plots-with-lattice-and-ggplot/ – Tung Aug 27 '18 at 22:57
  • Plotting in R: Intro to base, lattice and ggplot2 http://www.jvcasillas.com/base_lattice_ggplot/ – Tung Aug 27 '18 at 22:58

0 Answers0