ggplot2图例颜色渐变调整

时间:2017-02-09 04:20:18

标签: r ggplot2 legend

在下面的代码中,我想更改图例的各个方面,以便颜色渐变为" log10_p_value"在" plot_size"中表示。

enter image description here

所以白色气泡代表" plot_size"也代表" log10_size"的颜色。

可以在此处加载数据:

one.data <-
  structure(list(plot_X = c(5.902, 1.184, 5.354, -3.513, 2.183, 
                            2.217, -4.548, 6.466, -7.38, -6.19,
                            -6.374, 2.294, -3.653),
                 plot_Y = c(3.903, 7.13, -3.64, -5.64, 1.534, -4.031,
                            5.709, 0.066, 0.531, -3.234, 2.502, 6.686, 6.26),
                 plot_size = c(7.284, 5.413, 7.612, 4.725,5.633, 5.232,
                               4.547, 6.474, 5.567, 6.277, 6.874, 5.277, 4.394),
                 log10_p_value = c(-2.5229, -2.1549, -1.4949, -3, -1.5376,
                                   -1.3565, -2.5229, -1.5229, -1.4949, -2.1549,
                                   -1.3665, -2.1549, -1.3565)),
            row.names = c(NA, 13L),
            class = "data.frame",
            .Names = c("plot_X", "plot_Y", "plot_size", "log10_p_value"))

生成图表的代码是:

p1 <-
  ggplot(one.data
         , aes( plot_X
                , plot_Y
                , fill = log10_p_value
                , size = plot_size)) +
  geom_point(
    alpha = 0.6
    , shape = 21) +
  scale_size( range=c(0, 30)) +
  scale_fill_gradientn( colours = c("blue", "green", "yellow", "red")) +
  theme_bw() +
  coord_cartesian(xlim = range(one.data$plot_X) + diff(range(one.data$plot_X))*c(-0.1, 0.1)
                  , ylim = range(one.data$plot_Y) + diff(range(one.data$plot_Y))*c(-0.1, 0.1))


p1

1 个答案:

答案 0 :(得分:0)

您是否只想使用色标中的颜色更改plot_size图例中的空填充?

如果是这样,这将有效:

p1 + guides(size = guide_legend(override.aes = list(fill = "orange", alpha = 0.6)))

生成此

enter image description here

这恰好超越了这个传奇的默认美学(这通常是一个“安全”值,以避免与其他美学混淆)。

你可以让它们各自成为不同的颜色,但这是一个非常非常糟糕的主意,因为颜色与你想要的地图尺寸不符:

p1 + guides(size = guide_legend(override.aes = list(fill = c("blue", "green", "red"), alpha = 0.6)))