ggplot2 中面的旋转

Rotation of facets in ggplot2

我想我有一个棘手的案例。我正在使用 geom_raster 及时绘制植物病害水平的演变:x 和 y 是任意字段坐标,z 是在几个时间点测量的病害水平,我想将每个日期绘制在不同的位置方面。 到目前为止,没问题。下面是一个模拟数据集和代码:

library(ggplot2)
data <- data_frame(month=factor(rep(c("march","april","may","june"), each=100), levels=c("march","april","may","june")),
              x=rep(rep(1:10, each=10), 4),
              y=rep(rep(1:10, 10), 4),
              z=c(rnorm(100, 0.5, 1), rnorm(100, 3, 1.5), rnorm(100, 6, 2), rnorm(100, 9, 1)))
ggplot(data, aes(x=x, y=y, fill=z)) +
  geom_raster(color="white") +
  scale_fill_gradient2(low="white", mid=mean(range(dat$z)), high="red") +
  scale_x_discrete(limit=1:10, expand = c(0, 0)) +
  scale_y_discrete(limit=1:10, expand = c(0, 0)) +
  coord_equal() +
  facet_wrap(~month)

但我真正想要的是让每个面都旋转一定角度(例如 15°),以反映我的视场没有完全朝北(即顶部是不是北,底部不是南)。 ggplot2 或任何与网格相关的工具是否有可能自动执行此操作?即使是将各个方面保存到图像、旋转它们并在新页面上打印旋转图像的自动方法也足以满足我的需要。这是我想要获取的图像示例(在图像编辑器中将小平面旋转 15°): http://imgur.com/RYJ3EaR

这是一种独立旋转小平面的方法。我们为 month 的每个级别创建一个包含单独旋转图的列表,然后使用 grid.arrange 将四个图一起布置。我还从各个地块中删除了图例并分别绘制了图例。下面的代码包含一个用于提取图例的辅助函数。

我在下面的 lapply 函数中将图例对象提取到全局环境中(更不用说多次重复提取)。可能有更好的方法,但这种方法很快。

library(gridExtra)

# Helper function to extract the legend from a ggplot
# Source: 
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]]
  legend
}

# Create a list containing a rotated plot for each level of month
pl = lapply(unique(data$month), function(m) {

  # Create a plot for the current level of month
  p1 = ggplot(data[data$month==m,], aes(x=x, y=y, fill=z)) +
    geom_raster(color="white") +
    scale_fill_gradient2(low="white", high="red", 
                         limits=c(floor(min(data$z)), ceiling(max(data$z)))) +
    scale_x_discrete(limit=1:10, expand = c(0, 0)) +
    scale_y_discrete(limit=1:10, expand = c(0, 0)) +
    coord_equal() +
    facet_wrap(~month) 

  # Extract legend into global environment
  leg <<- g_legend(p1)

  # Remove legend from plot
  p1 = p1 + guides(fill=FALSE)

  # Return rotated plot
  editGrob(ggplotGrob(p1), vp=viewport(angle=-20, width=unit(0.85,"npc"), 
                                       height=unit(0.85,"npc")))                    
})

# Lay out the rotated plots and the legend and save to a png file
png("rotated.png", 1100, 1000)
grid.arrange(do.call(arrangeGrob, c(pl, ncol=2)),
             leg, ncol=2, widths=c(0.9,0.1))
dev.off()