使用双 Y 轴绘图更改 y 轴文字大小

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)

enter image description here

问题在于我无法改变y轴标签(y和y^2文本)的文本大小,我想把它变大。如果我只绘制obj1或obj2,则更改它没有问题,但对于doubleYScale则不起作用...

另一方面,我可以使用以下方法更改y轴上数字的大小:

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

有什么建议吗?我找不到方法:(
3个回答

6
这里有另一种使用latticelatticeExtra直接解决问题的方法:您可以独立设置所有轴标签的大小。
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)

enter image description here


如何在ylab2中应用cex?在您展示3种不同大小的labs的绘图中,我需要在ylab2中放置cex=1.5。 - Jean Karlos
1
你可以随意调整 fontsize 的大小,并根据你的需要进行指定。 - sparrow

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))

enter image description here


谢谢,不错!我会接受你的答案,因为我不知道网格解决方案!:) 但是,使用latticeExtra有没有办法呢?你知道吗? - user1665355
我在下面添加了一个解决方案,没有使用 grid。我意识到这个问题已经被问了两年以上,但对于其他寻找解决方案的人可能有帮助... - sparrow
@sparrow +1!但是它不超过2年前(1年和几个月):) 是的,这很有帮助,但通常不建议使用双轴,因为可读性不太好。 - agstudy

3

这并不是直接回答你的问题,但在一个轴上混合两个不同大小的图表可能不是最好的方法,因为它可能会产生误导。也许使用ggplot创建分面图会更好?下面的所有元素都可以轻松调整。

faceted plot

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")

1
谢谢!我知道...但是我的雇主想要一个双y轴图,所以...:) 但是你知道latticeExtra有没有解决方案吗?最好的问候 - user1665355
使用lattice,Extra我不知道,但是另一个答案使用grid看起来很有用。 - SlowLearner
是的,谢谢!再次感谢您的答案,我也告诉员工使用ggplot更好,但是...祝你晚上愉快! - user1665355

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接