大家好我想用R中的lattice
图形来解决一个小问题。我用lattice
构建一个双y轴图。它很棒。我在最后部分添加了数据的代码和dput
版本(xx
数据框列表):
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")
一切都很完美,但是当我绘制DF
时,我得到了这个:
如何看到左侧y轴没有显示其值,我认为这个事实是由于包含所有绘图的正方形的大小。我的问题是,是否有任何方法可以减小该方块的大小并显示y轴刻度内的完整值。我的数据框列表的dput()
版本是下一个:
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"))
我在格子图形书上寻找任何建议,但我无法解决这个问题。非常感谢你的帮助。
答案 0 :(得分:1)
不确定为什么它不适合你。我冒昧地重新格式化你的代码,除了没有重要的改变。无论如何,这是情节:
编辑:问题似乎与格子的设置有关(请参阅?trellis.par.get
)。我相应地更新了源码,这应该可以解决问题。
这是来源:
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)