I have generated following 2D plot to show how the mean of z variable (z_mean) changes with variables x and y :
Code to generate the above figure is below:
test = data.frame(group = factor(c(rep(1, 100), rep(2, 100), rep(3, 100))),
x = sample(20, 300, replace =T),
y = sample(50, 300, replace =T),
z_mean = sample(5, 300, replace =T),
z_sd = runif(300))
ggplot(data=test, aes(x=x, y=y, z=z_mean) ) +
stat_summary2d(fun = function(x) {mean(x)}, binwidth = c(2, 5))+
# stat_bin2d(binwidth = c(2, 5))+
scale_fill_gradientn(colours=c("green", "yellow", "red"), name = "")+
facet_wrap(~group) +
ggtitle("z_mean")
Now I want to put the number of data points inside each cell in text. Is there any way to do that, may be extracting values from stat_bin2d. Is there any way to extract the values extracted by stat_summary2d or stat_bin2d?
Also, I have another figure to present the standard deviation of variable z (z_sd). See below:
Is there a creative way to combine these two figures in one. Maybe splitting the rectangles or the size of a circle inside the rectangle. I could not find a way to do that in ggplot.