简体   繁体   中英

R: ggplot2 specify dataset for scale_fill_continuous

I am trying to create a scatterplot with a contour/heat overlay, but I would like to create a single plot that includes three different datasets, each with its own shading scheme (as in the plot below that I created by manually overlaying three individual plots).

在此处输入图片说明

I know I can specify a certain data frame with, for example, stat_density2d(data=df1 … However, when I try to specify the data frame to which filling of the density shading with a certain color should be applied, as with the following code:

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

I get the following error.

Error in continuous_scale("fill", "gradient", seq_gradient_pal(low, high, : >unused argument (data = list(x = c(...)

If I don't specify the dataset for any of the scale_fill_continuous() arguments, the last color scheme specified will be applied to all. Is there any way to include all three datasets in a single plot, each with a different shading scheme? Thank you.

Edit: I can use

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

to get:

在此处输入图片说明

and manually combine the individual dataset plots to get the combined plot, but I don't know how to create a single plot with multiple datasets as I tried to do in the original post where each of the datasets will have their own shading color. I either get the error message posted above if I try to specify the dataset to which the shading should be applied (as described above), or, if I don't specify the dataset, eg, with

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

I get a plot with shading all in one color (the last color scheme specified):

在此处输入图片说明

It is difficult to help without a reproducible example but it seems you always use a single colour to fill each datasets density. If this is the case delete all your fill=..level.. and scale_fill_continuous and specify fill inside stat_density2d but outside of aes() .

A small example:

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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM