本文介绍了在ggplot2中进行stat_summary_hex图之间的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有两个A和B空间分布的字符Z,我希望能够使一个六进制数减去每个六进制数中字符的比例。在这里,我有两个理论人口的代码A和B library(hexbin) library(ggplot2) set.seed(2) xA set.seed(3) yA set.seed(4) zA hbinA A set.seed 5) xB set.seed(6) yB set.seed(7) zB hbinB B ggplot(A,aes(x,y, z = z))+ stat_summary_hex(fun = function(z)sum(z)/ length(z),alpha = 0.8)+ scale_fill_gradientn(colors = c(blue,red))+ guides(alpha = FALSE,size = FALSE) ggplot(B,aes(x,y,z = z))+ stat_summary_hex(fun = function(z)sum(z)/乐ngth(z),alpha = 0.8)+ scale_fill_gradientn(colors = c(blue,red))+ guides(alpha = FALSE,size = FALSE) 这里是两个结果图 我的目标是使用hexbins创建第三个图表,其中hexbins的值在相同的坐标上,但我甚至不知道如何开始这样做,我在光栅Package中做了类似的事情,但我需要它作为hexbins 非常感谢 解决方案您需要确保两个地块使用完全相同的分箱。为了实现这一点,我认为最好事先进行分箱,然后用stat_identity / geom_hex绘制结果。使用代码示例中的变量,您可以执行以下操作: ##找到完整数据的边界 xbnds< ; - 范围(c(A $ x,B $ x)) ybnds nbins #函数为geom_hex创建一个可以和stat_identity一起使用的data.frame makeHexData< - function(df){h< - hexbin(df $ x,df $ y,nbins ,xbnds = xbnds,ybnds = ybnds,IDs = TRUE) data.frame(hcell2xy(h),z = tapply(df $ z,h @ cID,FUN = function(z)sum(z )/长度(z)), cid = h @ cell)} Ahex< - makeHexData(A) Bhex< - makeHexData(B ) ##并非所有单元格都存在于每个分箱中,我们需要通过cellID byCell合并(Ahex,Bhex,by =cid,all = T) ##在计算差异空单元格时应计为0 byCell $ zx [is.na(byCell $ zx)] byCell $ zy [is .na(byCell $ zy)] ##创建一个差异data.frame Diff < - data.frame(x = ifelse(is.na(byCell $ xx),byCell $ xy,byCell $ xx),y = ifelse(is.na(byCell $ yx),byCell $ yy,byCell $ yx),z = byCell $ zx - byCell $ zy) ##绘制结果 ggplot(Ahex)+ geom_hex(aes (x = x,y = y,fill = z), stat =identity,alpha = 0.8)+ scale_fill_gradientn(colors = c(blue,red))+ guides(alpha = FALSE,size = FALSE) ggplot(Bhex)+ geom_hex(aes(x = x,y = y,fill = z),$ b $ (alpha = FALSE,size = FALSE)b $ stat =identity,alpha = 0.8)+ scale_fill_gradientn(colors = c(blue,red)) ggplot(Diff)+ geom_hex(aes(x = x,y = y,fill = z), stat =identity,alpha = 0.8)+ scale_fill_gradientn (color = c(blue,red))+ guides(alpha = FALSE,size = FALSE) I have two populations A and B distributed spatially with one character Z, I want to be able to make an hexbin substracting the proportion of the character in each hexbin. Here I have the code for two theoretical populations A and Blibrary(hexbin)library(ggplot2)set.seed(2)xA <- rnorm(1000)set.seed(3)yA <- rnorm(1000)set.seed(4)zA <- sample(c(1, 0), 20, replace = TRUE, prob = c(0.2, 0.8))hbinA <- hexbin(xA, yA, xbins = 40, IDs = TRUE)A <- data.frame(x = xA, y = yA, z = zA)set.seed(5)xB <- rnorm(1000)set.seed(6)yB <- rnorm(1000)set.seed(7)zB <- sample(c(1, 0), 20, replace = TRUE, prob = c(0.4, 0.6))hbinB <- hexbin(xB, yB, xbins = 40, IDs = TRUE)B <- data.frame(x = xB, y = yB, z = zB)ggplot(A, aes(x, y, z = z)) + stat_summary_hex(fun = function(z) sum(z)/length(z), alpha = 0.8) +scale_fill_gradientn(colours = c("blue","red")) +guides(alpha = FALSE, size = FALSE)ggplot(B, aes(x, y, z = z)) + stat_summary_hex(fun = function(z) sum(z)/length(z), alpha = 0.8) +scale_fill_gradientn (colours = c("blue","red")) +guides(alpha = FALSE, size = FALSE)here is the two resulting graphsMy goal is to make a third graph with hexbins with the values of the difference between hexbins at the same coordinates but I don't even know how to start to do it, I have done something similar in the raster Package, but I need it as hexbinsThanks a lot 解决方案 You need to make sure that both plots use the exact same binning. In order to achieve this, I think it is best to do the binning beforehand and then plot the results with stat_identity / geom_hex. With the variables from your code sample you ca do:## find the bounds for the complete data xbnds <- range(c(A$x, B$x))ybnds <- range(c(A$y, B$y))nbins <- 30# function to make a data.frame for geom_hex that can be used with stat_identitymakeHexData <- function(df) { h <- hexbin(df$x, df$y, nbins, xbnds = xbnds, ybnds = ybnds, IDs = TRUE) data.frame(hcell2xy(h), z = tapply(df$z, h@cID, FUN = function(z) sum(z)/length(z)), cid = h@cell)}Ahex <- makeHexData(A)Bhex <- makeHexData(B)## not all cells are present in each binning, we need to merge by cellIDbyCell <- merge(Ahex, Bhex, by = "cid", all = T)## when calculating the difference empty cells should count as 0byCell$z.x[is.na(byCell$z.x)] <- 0byCell$z.y[is.na(byCell$z.y)] <- 0## make a "difference" data.frameDiff <- data.frame(x = ifelse(is.na(byCell$x.x), byCell$x.y, byCell$x.x), y = ifelse(is.na(byCell$y.x), byCell$y.y, byCell$y.x), z = byCell$z.x - byCell$z.y)## plot the resultsggplot(Ahex) + geom_hex(aes(x = x, y = y, fill = z), stat = "identity", alpha = 0.8) + scale_fill_gradientn (colours = c("blue","red")) + guides(alpha = FALSE, size = FALSE)ggplot(Bhex) + geom_hex(aes(x = x, y = y, fill = z), stat = "identity", alpha = 0.8) + scale_fill_gradientn (colours = c("blue","red")) + guides(alpha = FALSE, size = FALSE)ggplot(Diff) + geom_hex(aes(x = x, y = y, fill = z), stat = "identity", alpha = 0.8) + scale_fill_gradientn (colours = c("blue","red")) + guides(alpha = FALSE, size = FALSE) 这篇关于在ggplot2中进行stat_summary_hex图之间的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 06:13