1

我正在尝试将多个图例显示为表格。例如,

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

我怎样才能画出这样的传奇?

4

1 回答 1

1

这是一个例子:

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.

于 2013-07-02T13:29:37.697 回答