R:ggplot2指定scale_fill_continuous的数据集

时间:2017-01-18 16:11:59

标签: r ggplot2

我正在尝试创建一个带有轮廓/热量叠加的散点图,但我想创建一个包含三个不同数据集的图,每个数据集都有自己的着色方案(如下图所示,我通过手动叠加创建)三个单独的地块)。

enter image description here

我知道我可以指定某个数据框,例如stat_density2d(data=df1 ...但是,当我尝试指定应该使用某种颜色填充密度着色的数据框时,如同以下代码:

ggplot(data=df,aes(x,y)) + geom_vline(xintercept = -0.977723605) + geom_hline(yintercept = 1.365281958) + stat_density2d(aes(fill=..level..,alpha=..level..),geom='polygon',colour='yellow') + scale_fill_continuous(low="yellow",high="yellow") + guides(alpha="none") + geom_point(shape = 21, colour = "grey", fill = "yellow", size = 1, stroke = 0.1) + commonTheme + stat_density2d(data=df1,aes(fill=..level..,alpha=..level..), geom='polygon',colour='blue') + scale_fill_continuous(data=df1,low="blue",high="blue") + geom_point(data=df1, shape = 21, colour = "grey", fill = "blue", size = 1, stroke = 0.1) + stat_density2d(data=df2,aes(fill=..level..,alpha=..level..),geom='polygon',colour='red') + scale_fill_continuous(data=df2,low="red",high="red") + geom_point(data=df2, shape = 21, colour = "grey", fill = "red", size = 1, stroke = 0.1) + scale_y_continuous(limits=c(0, 4)) + scale_x_continuous(limits=c(-2, 0))

我收到以下错误。

  

continuous_scale中的错误(“填充”,“渐变”,seq_gradient_pal(低,高,:>未使用的参数(data = list(x = c(...)

如果我没有为任何scale_fill_continuous()参数指定数据集,则指定的最后一个颜色方案将应用于所有参数。有没有办法在一个图中包含所有三个数据集,每个数据集都有不同的着色方案?谢谢。

编辑: 我可以用

ggplot(data=df1,aes(x,y)) + geom_vline(xintercept = -0.977723605) + geom_hline(yintercept = 1.365281958) + stat_density2d(aes(fill=..level..,alpha=..level..),geom='polygon',colour='blue') + 
  scale_fill_continuous(low="blue",high="blue") + 
  guides(alpha="none") +
  geom_point(shape = 21, colour = "black", fill = "blue", size = 1, stroke = 0.1) + commonTheme + scale_y_continuous(limits=c(0, 4)) + scale_x_continuous(limits=c(-2, 0))

得到:

enter image description here

并手动组合各个数据集图以获得组合图,但我不知道如何使用多个数据集创建单个图,就像我在原始帖子中尝试的那样,每个数据集都有自己的阴影颜色。如果我尝试指定应该应用着色的数据集(如上所述),或者如果我没有指定数据集,例如,使用

,我会得到上面发布的错误消息
ggplot(data=df,aes(x,y)) + geom_vline(xintercept = -0.977723605) + geom_hline(yintercept = 1.365281958) + stat_density2d(aes(fill=..level..,alpha=..level..),geom='polygon',colour='yellow') + scale_fill_continuous(low="yellow",high="yellow") + guides(alpha="none") + geom_point(shape = 21, colour = "grey", fill = "yellow", size = 1, stroke = 0.1) + commonTheme + stat_density2d(data=df1,aes(fill=..level..,alpha=..level..), geom='polygon',colour='blue') + scale_fill_continuous(low="blue",high="blue") + geom_point(data=df1, shape = 21, colour = "grey", fill = "blue", size = 1, stroke = 0.1) + stat_density2d(data=df2,aes(fill=..level..,alpha=..level..),geom='polygon',colour='red') + scale_fill_continuous(low="red",high="red") + geom_point(data=df2, shape = 21, colour = "grey", fill = "red", size = 1, stroke = 0.1) + scale_y_continuous(limits=c(0, 4)) + scale_x_continuous(limits=c(-2, 0))

我得到一个阴影全部用一种颜色(指定的最后颜色方案):

enter image description here

1 个答案:

答案 0 :(得分:0)

如果没有可重复的示例,很难提供帮助,但似乎您总是使用单一颜色来填充每个数据集密度。如果是这种情况,请删除所有fill=..level..scale_fill_continuous,并在fill内指定stat_density2d,但在aes()之外。

一个小例子:

N <- 10000
set.seed(1243)
pts <- seq(0,1,.1)
df <- data.frame(x = sample(pts, N, replace = TRUE, prob = pts/sum(pts)))
df$y <- rnorm(N, 0,0.5 + abs(df$x))
df1 <- -df+.5
ggplot(data=df,aes(x,y)) +
  stat_density2d(aes(alpha=..level..), geom='polygon', colour='yellow', fill = "yellow") +
  stat_density2d(data = df1, aes(alpha=..level..), geom='polygon', colour='blue', fill = 'blue') +
  guides(alpha="none")

enter image description here