我正在安排由ggplot2生成的圖。我必須使用print來打印出圖表和grid.draw來顯示圖例。如何更改grid.draw的位置
示例代碼:
p0 <- ggplot(data = iris, geom = 'blank',
aes(y = Petal.Width, x = Petal.Length, color = Species)) + geom_point() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = "none")
p1 <- ggplot(data = iris, geom = 'blank',
aes(y = Petal.Length, x = Petal.Width, color = Species)) + geom_point() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = "none")
g_legend <- function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
p <- ggplot(data = iris, geom = 'blank',
aes(y = Petal.Width, x = Petal.Length, color = Species)) + geom_point()
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 4)))
print(p0,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p0,vp = viewport(layout.pos.row = 1, layout.pos.col = 2:3))
print(p1,vp = viewport(layout.pos.row = 2, layout.pos.col = 2:3))
grid.text("This is x label",gp=gpar(fontsize = 14), vjust = 11,
vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
grid.text("This is y label",gp=gpar(fontsize = 14), vjust = -11, rot = 90,
![enter image description here][1]vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
grid.draw(g_legend(p))
我想提出的傳說在第四列。我該怎麼做?謝謝。
我已經回滾你的問題,因爲你的上次編輯提出了一個新問題。 – Andrie