2012-12-14 43 views
5

我做了一個doubleYScale情節:與doubleYScale情節更改y軸文字大小

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

problem的是,我不能設法把Y-axic標籤的文字大小( yy^2文字,我想讓它變大)。這是改變它,如果我只繪製obj1obj2分開沒有問題,但它並不適用於doubleYScale工作...

我可以另一方面改變號碼的大小在y axices有:

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

有什麼建議嗎?我無法找到一種方法:(

回答

3
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

+0

謝謝,好的!會接受你的答案,因爲我不知道網格解決方案!:)但有沒有與latticeExtra的方式,你知道嗎? – user1665355

+0

我在下面添加了一個解決方案,而不使用'grid'。我意識到這個問題是在兩年多前提出的,但它可能對尋求解決方案的其他人有幫助。 – sparrow

+0

@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") 
+0

感謝!我知道......但是我的員工想要一個兩個Y軸的情節,所以.. :)但是有沒有與latticeExtra的解決方案,你知道嗎?最好的問候 – user1665355

+0

使用格,額外我不知道,但使用網格的其他答案看起來很有用 – SlowLearner

+0

是的,謝謝!再次感謝您的回答,我也告訴員工ggplot更好,但是...祝您有個愉快的夜晚! – user1665355

4

這裏是直接在另一解決方案與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