5

我做了一个doubleYScale情节:

library(lattice)
library(latticeExtra)

# Some data
foo <- list(x = 1:100, y = cumsum(rnorm(100)))

obj1 <- xyplot(y~ x, data=foo,xlab=list(cex=1.2), 
               main="TOtalProduktion VS SummaSkulder/TotaltKapital i procent",
               type = c("l","g"),col="black",
               lty=1,key = simpleKey(col=c('black'),
               text=c("Produktion"),cex=1.2,points=FALSE, lines=TRUE),
               scales=list(x=list(rot=90,tick.number=25,
               cex=1,axs="r")))

obj2 <- xyplot(y^2 ~ x,data= foo ,type = "o",col="black",
               lty=9,key = simpleKey(col=c('black'),
               text=c("Summa.skulder"),cex=1.2,lines=FALSE,points= TRUE))

doubleYScale(obj1, obj2, add.ylab2 = TRUE)

在此处输入图像描述

problem我无法更改 y 轴标签的文本大小(yy^2文本,我想让它更大)。obj1如果我只绘制或单独绘制,更改它是没有问题的obj2,但它不适用于doubleYScale...

另一方面,我可以通过以下方式更改 y 轴上数字的大小:

trellis.par.set(axis.text=list(cex=1))

有什么建议么?我找不到办法:(

4

3 回答 3

6

这是另一个直接使用latticeand的解决方案latticeExtra:您可以在其中独立设置所有轴标签的大小:

library(lattice)
library(latticeExtra)
foo <- list(x = 1:100, y = cumsum(rnorm(100)))

obj1 <- xyplot(y~ x, data=foo,
               xlab=list("Thing", fontsize = 22),
               ylab = list("Something", fontsize = 32),
               ylab.right = list("Anything", fontsize = 16),
               par.settings = simpleTheme(col = 1),
               type = c("l","g"),
               lty=1,
               scales=list(x=list(rot=90,tick.number=25,
                                  cex=1,axs="r")))

obj2 <- xyplot(y^2 ~ x,data= foo ,type = "o",col="black",
               lty=9)

doubleYScale(obj1, obj2)

在此处输入图像描述

于 2014-02-13T04:54:31.693 回答
4
    library(grid)
    ##  the text size of the 2 y-axic labels
    grid.edit(gPath='GRID.text',grep=T,global=T,gp =gpar(cex=3))

如果要设置不同的轴尺寸

    grobs <- sapply(grid.get(gPath='GRID.text',grep=T,global=T),'[')['name',]
    grid.edit(gPath=grobs[[1]],gp =gpar(cex=2))
    grid.edit(gPath=grobs[[2]],gp =gpar(cex=1.5))

在此处输入图像描述

于 2012-12-15T18:54:58.147 回答
3

这并不能直接回答您的问题,但是在一个轴上混合两个不同大小的图可能不是最好的方法,因为它可能会产生误导。也许使用多面图ggplot可以完成这项工作或更好?下面的所有元素都很容易调整。

刻面图

library(ggplot2)
library(reshape)

set.seed(123)
foo <- list(x = 1:100, y = cumsum(rnorm(100)))

foo <- as.data.frame(foo)
foo$z <- foo$y^2
mymelt <- melt(foo, id.var = 'x')
mymelt$label <- ifelse(mymelt$variable == 'y', "Produktion", "Summa.skulder")
mymelt$line.colour <- ifelse(mymelt$variable == 'y', "red", "blue") # specify colours here

ggplot(data = mymelt, aes(x = x, y = value)) +
    geom_line(aes(colour = mymelt$line.colour)) +
    facet_wrap(~ label, ncol = 1, scales = "free_y") +
    scale_colour_manual(values = unique(mymelt$line.colour)) +
    ggtitle("TOtalProduktion VS SummaSkulder/TotaltKapital i procent") +
    theme(strip.text.x = element_text(size = 12)) +
    theme(axis.text.x = element_text(size = 9)) +
    theme(axis.text.y = element_text(size = 9)) +
    theme(axis.title.x = element_text(size = 15)) +
    theme(axis.title.y = element_text(size = 15)) +
    theme(axis.title.x = element_blank()) + # comment out this line if you want an x axis title
    theme(axis.title.y = element_blank()) + # comment out this line if you want a y axis title
    theme(legend.position = "none")
于 2012-12-15T03:57:42.423 回答