Stagger axis labels, new feature in ggplot2在x軸標籤追問:錯開軸標籤,新功能在GGPLOT2
我是新的R和@Sandy Muspratt的答案@spindoctor以下是有點嚇人。答案對於Y軸標籤非常有用。我嘗試了一些編輯。例如,我改變了:
index <- which(g$layout$name == "axis-l") # Which grob
到:
index <- which(g$layout$name == "axis-b") # Which grob
但X軸標籤仍然是他們,不交錯排列。您能否指出如何修改代碼以使其適用於x軸標籤?
# Get the grob
g <- ggplotGrob(out.plot)
# Get the y axis
index <- which(g$layout$name == "axis-l") # Which grob
yaxis <- g$grobs[[index]]
# Get the ticks (labels and marks)
ticks <- yaxis$children[[2]]
# Get the labels
ticksL <- ticks$grobs[[1]]
# Make the edit
ticksL$children[[1]]$x <- rep(unit.c(unit(c(1,0,-1),"npc")), 27)
# Put the edited labels back into the plot
ticks$grobs[[1]] <- ticksL
yaxis$children[[2]] <- ticks
g$grobs[[index]] <- yaxis
# Make the relevant column a little wider
g$widths[3] <- unit(2.5, "cm")
# Draw the plot
grid.newpage()
grid.draw(g)
的TableGrob的輸出如下所示:
>g
TableGrob (6 x 5) "layout": 8 grobs
z cells name grob
1 0 (1-6,1-5) background rect[plot.background..rect.507]
2 3 (3-3,3-3) axis-l absoluteGrob[GRID.absoluteGrob.498]
3 1 (4-4,3-3) spacer zeroGrob[NULL]
4 2 (3-3,4-4) panel gTree[GRID.gTree.484]
5 4 (4-4,4-4) axis-b absoluteGrob[GRID.absoluteGrob.491]
6 5 (5-5,4-4) xlab titleGrob[axis.title.x..titleGrob.501]
7 6 (3-3,2-2) ylab titleGrob[axis.title.y..titleGrob.504]
8 7 (2-2,4-4) title zeroGrob[plot.title..zeroGrob.505]
我試圖識別相關的X,Y軸的值,但導航這種結構有點陌生。任何建議或意見或資源,以避免浪費時間猜測,不勝感激。
@Sandy Muspratt可能對這個,所以我試圖讓他們通知輸入。如果有更好的方法,請告訴我。 – Jas
也許@spindoctor已經想出瞭如何做到這一點。 – Jas