我正在尝试将多个图例显示为表格。例如,
library(ggplot2)
dat <- data.frame(
x = rep(1:4, 4),
y = c(1:4, 2:5, 3:6, 4:7),
a = rep(rep(c("a1", "a2"), each=4), 2),
b = rep(c("b1", "b2"), each=8))
ggplot(dat, aes(x=x, y=y, colour=b, shape=a)) +
geom_point()+ facet_wrap(~ b)
我可以获得不同颜色和不同形状的多个传奇。但我想展示我的传奇,如
b1 | b2
--------------
a1 | o | o
a2 | ^ | ^
我怎样画这样的传说?
答案 0 :(得分:0)
以下是一个例子:
p <- ggplotGrob(ggplot(dat, aes(x=x, y=y, colour=b, shape=a)) +
geom_point()+ facet_wrap(~ b) + theme(legend.position = "none"))
leg <- ggplotGrob(ggplot(unique(subset(dat, select = a:b)), aes(a, b, colour=b, shape=a)) + geom_point() +
coord_equal() +
theme_minimal() +
theme(legend.position = "none", axis.ticks = element_blank(), axis.title = element_blank()))
library(gtable)
grid.newpage()
pushViewport(vp = viewport(width = 0.8, x = 0.4))
grid.draw(p)
popViewport()
pushViewport(vp = viewport(width = 0.2, x = 1-0.1))
grid.draw(leg)
popViewport()
您可以通过自定义theme
来调整外观。