本文介绍了用scale_fill_manual手动分配颜色只适用于某些六角大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试创建一个由六边形计数区域总结的散点图。我希望用户能够定义色阶的计数中断。我有这个工作,使用 scale_fill_manual()。然而,奇怪的是,它有时只能起作用。在下面的MWE中,如果使用给定的种子值,如果 xbins = 10 ,则会出现如下情节: 我的MWE如下: library(ggplot2) library(hexbin) library(RColorBrewer) $ b $ set.seed(1) xbins< - 20 x y minVal maxVal maxRange buffer< - (maxRange [2] --maxRange [1])/(xbins / 2)h< -hexbin(x = x,y = y,xbins = xbins,shape = 1,IDs = TRUE, xbnds = maxRange,ybnds = maxRange) hexdf< - data。帧(hcell2xy(h),hexID = h @ cell,counts = h_count) my_breaks clrs< - brewer .pal(长度(my_breaks)+3,Blues) clrs< - clrs [3:长度(clrs)] hexdf $ countColor< - cut(hexdf $ counts,breaks = c (0,my_breaks,Inf), labels = rev(clrs)) ggplot(hexdf,aes(x = x,y = y,hexID = hexID,fill = countColor)) + scale_fill_manual(values = levels(hexdf $ countColor))+ geom_hex(stat =identity)+ geom_abline(截距= 0,color =red,size = 0.25) + coord_fixed(xlim = c(-0.5,(maxRange [2] + buffer)), ylim = c(-0.5,(maxRange [2] + buffer)))+ 主题(aspect.ratio = 1) 我的目标是调整此代码,使得情节不会有问题(突然某些六边形的地方)不管分配给xbins的值如何,s都是不同的大小和形状)。但是,我对某些xbins值可能导致这个问题感到困惑。任何意见将不胜感激。 编辑: 我在考虑注释后更新示例代码由@bdemarest和@Axeman。我遵循@Axeman推荐的链接中最受欢迎的答案,并且认为当您在整数向量上使用 scale_fill_continuous()时它更有用。在这里,我正在处理因子向量上的 scale_fill_manual()。结果,我仍然无法实现这个目标。 库(ggplot2)库(hexbin)库(RColorBrewer) set.seed(1) xbins x y minVal maxVal maxRange buffer< ; - (maxRange [2] - maxRange [1])/(xbins / 2) bindata = data.frame(x = x,y = y,factor = as.factor(1)) h< - hexbin(bindata,xbins = xbins,IDs = TRUE,xbnds = maxRange,ybnds = maxRange) counts< - hexTapply(h,bindata $ factor,table)$ (计数)计数< )计数$因子= as.factor(计数$因子) hexdf< - data.frame(hcell2xy(h),ID = h @ cell) hexdf< ; - merge(counts,hexdf) my_breaks< - c(2,4,6,8,20,1000) clrs< - brewer.pal(length(my_breaks) + 3,蓝调) clrs< - clrs [3:1 (clrs)] hexdf $ countColor< - cut(hexdf $ counts,breaks = c(0,my_breaks,Inf),labels = rev(clrs)) ggplot(hexdf ,aes(x = x,y = y,fill = countColor))+ scale_fill_manual(values = levels(hexdf $ countColor))+ geom_hex(stat =identity)+ gent_abline(截距= 0,color =red,size = 0.25)+ coord_cartesian(xlim = c(-0.5,maxRange [2] + buffer),ylim = c(-0.5,maxRange [2] +缓冲区))+主题(aspect.ratio = 1) 解决方案您可以在'geom'中定义颜色,而不是'scale'来修改绘图的比例: ggplot(hexdf,aes (x = x,y = y))+ geom_hex(stat =identity,fill = hexdf $ countColor) I am trying to create a scatterplot that is summarized by hexagon bins of counts. I would like the user to be able to define the count breaks for the color scale. I have this working, using scale_fill_manual(). Oddly, however, it only works sometimes. In the MWE below, using the given seed value, if xbins=10, there are issues resulting in a plot as follows:However, if xbins=20 or 40, for example, the plot doesn't seem to have problems:My MWE is as follows:library(ggplot2)library(hexbin)library(RColorBrewer)set.seed(1)xbins <- 20x <- abs(rnorm(10000))y <- abs(rnorm(10000))minVal <- min(x, y)maxVal <- max(x, y)maxRange <- c(minVal, maxVal)buffer <- (maxRange[2] - maxRange[1]) / (xbins / 2)h <- hexbin(x = x, y = y, xbins = xbins, shape = 1, IDs = TRUE, xbnds = maxRange, ybnds = maxRange)hexdf <- data.frame (hcell2xy(h), hexID = h@cell, counts = h@count)my_breaks <- c(2, 4, 6, 8, 20, 1000)clrs <- brewer.pal(length(my_breaks) + 3, "Blues")clrs <- clrs[3:length(clrs)]hexdf$countColor <- cut(hexdf$counts, breaks = c(0, my_breaks, Inf), labels = rev(clrs))ggplot(hexdf, aes(x = x, y = y, hexID = hexID, fill = countColor)) + scale_fill_manual(values = levels(hexdf$countColor)) + geom_hex(stat = "identity") + geom_abline(intercept = 0, color = "red", size = 0.25) + coord_fixed(xlim = c(-0.5, (maxRange[2] + buffer)), ylim = c(-0.5, (maxRange[2] + buffer))) + theme(aspect.ratio=1)My goal is to tweak this code so that the plot does not have problems (where suddenly certain hexagons are different sizes and shapes than the rest) regardless of the value assigned to xbins. However, I am puzzled what may be causing this problem for certain xbins values. Any advice would be greatly appreciated.EDIT:I am updating the example code after taking into account comments by @bdemarest and @Axeman. I followed the most popular answer in the link @Axeman recommends, and believe it is more useful when you are working with scale_fill_continuous() on an integer vector. Here, I am working on scale_fill_manual() on a factor vector. As a result, I am still unable to get this goal to work. Thank you.library(ggplot2)library(hexbin)library(RColorBrewer)set.seed(1)xbins <- 10x <- abs(rnorm(10000))y <- abs(rnorm(10000))minVal <- min(x, y)maxVal <- max(x, y)maxRange <- c(minVal, maxVal)buffer <- (maxRange[2] - maxRange[1]) / (xbins / 2)bindata = data.frame(x=x,y=y,factor=as.factor(1))h <- hexbin(bindata, xbins = xbins, IDs = TRUE, xbnds = maxRange, ybnds = maxRange)counts <- hexTapply (h, bindata$factor, table)counts <- t (simplify2array (counts))counts <- melt (counts)colnames (counts) <- c ("factor", "ID", "counts")counts$factor =as.factor(counts$factor)hexdf <- data.frame (hcell2xy (h), ID = h@cell)hexdf <- merge (counts, hexdf)my_breaks <- c(2, 4, 6, 8, 20, 1000)clrs <- brewer.pal(length(my_breaks) + 3, "Blues")clrs <- clrs[3:length(clrs)]hexdf$countColor <- cut(hexdf$counts, breaks = c(0, my_breaks, Inf), labels = rev(clrs))ggplot(hexdf, aes(x = x, y = y, fill = countColor)) + scale_fill_manual(values = levels(hexdf$countColor)) + geom_hex(stat = "identity") + geom_abline(intercept = 0, color = "red", size = 0.25) + coord_cartesian(xlim = c(-0.5, maxRange[2]+buffer), ylim = c(-0.5, maxRange[2]+ buffer)) + theme(aspect.ratio=1) 解决方案 you can define colors in 'geom' instead of 'scale' that modifies the scale of plot:ggplot(hexdf, aes(x = x, y = y)) + geom_hex(stat = "identity",fill =hexdf$countColor) 这篇关于用scale_fill_manual手动分配颜色只适用于某些六角大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 09:40