在ggplot2()中概述stat_summary2d容器

时间:2014-03-31 22:07:16

标签: r ggplot2

我想以某种方式概述包含stat_summary_2d()图中数据的区域。

例如,我有以下数据:

my.test = data.frame(x = c(1,2,3),
                     y = c(1,2,3),
                     z = c(1,2,3))

我将其绘制如下:

p.test <- ggplot(data = my.test,
                 aes(x = x,
                     y = y,
                     z = z)) +
  stat_summary2d(breaks = list(x = seq(-.5,3.5,1),
                                y = seq(0.5,3.5,1)))  +
  scale_fill_gradient2(name = "z", 
                       high = "red", 
                       mid = "grey",
                       low = "blue",
                       midpoint = 2,
                       breaks = seq(0,3,1),
                       limits = c(0,4),
                       guide = "colourbar")

这给了我这个:

enter image description here

这没关系。但是,有时候,我更喜欢漂亮的白色中点,我喜欢在白色背景上绘制出版物的内容:

p.test2 <- p.test +
  scale_fill_gradient2(name = "z", 
                       high = "red", 
                       mid = "white",
                       low = "blue",
                       midpoint = 2,
                       breaks = seq(0,3,1),
                       limits = c(0,4),
                       guide = "colourbar")

ggsave(file = file.path("ptest2.png"),
       plot = p.test2 + theme_bw(base_size = 8),
       width=3, height=2, dpi=150)

所以我最终得到了这个:

enter image description here

......我的数据不可见。

我的问题:有没有办法在stat_summary2d()生成的方框周围添加细的彩色线?

1 个答案:

答案 0 :(得分:1)

它就像使用colour =参数一样简单。请记住,在ggplot2中,colour用于行和点,fill用于填充。线条粗细由size参数设置。

p.test <- ggplot(data = my.test, aes(x = x, y = y,z = z)) +
  stat_summary2d(colour = "black", #size = 0.5,
                 breaks = list(x = seq(-.5,3.5,1),
                               y = seq(0.5,3.5,1)))  +
  scale_fill_gradient2(name = "z", 
                       high = "red", 
                       mid = "white",
                       low = "blue",
                       midpoint = 2,
                       breaks = seq(0,3,1),
                       limits = c(0,4),
                       guide = "colourbar") + 
  theme_bw()
p.test

enter image description here