gpt4 book ai didi

r - 使用scale_fill_manual手动分配颜色仅适用于某些六边形尺寸

转载 作者:行者123 更新时间:2023-12-04 04:37:02 24 4
gpt4 key购买 nike

我正在尝试创建一个散点图,该散点图由计数的六边形箱汇总。我希望用户能够定义色标的计数间隔。我正在使用scale_fill_manual()进行此工作。然而,奇怪的是,它仅在某些时候有效。在下面的MWE中,使用给定的种子值(如果为xbins=10),会导致出现如下所示的问题:

enter image description here

但是,例如,如果使用xbins=2040,则绘图似乎没有问题:

enter image description here

我的MWE如下:

library(ggplot2)
library(hexbin)
library(RColorBrewer)

set.seed(1)
xbins <- 20

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

我的目标是调整此代码,以使绘图不会出现问题(突然某些六角形的大小和形状与其余六边形不同),而与分配给xbins的值无关。但是,我对某些xbins值可能导致此问题的原因感到困惑。任何建议将不胜感激。

编辑:

考虑到@bdemarest和@Axeman的评论后,我正在更新示例代码。我在@Axeman建议的链接中遵循了最流行的答案,并认为当您在整数矢量上使用 scale_fill_continuous()时,此方法会更有用。在这里,我正在研究因子向量上的 scale_fill_manual()。结果,我仍然无法实现这个目标。谢谢你。
library(ggplot2)
library(hexbin)
library(RColorBrewer)

set.seed(1)
xbins <- 10

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

最佳答案

您可以在'geom'中定义颜色,而不是修改图比例的'scale':

ggplot(hexdf, aes(x = x, y = y)) + 
geom_hex(stat = "identity",fill =hexdf$countColor)

关于r - 使用scale_fill_manual手动分配颜色仅适用于某些六边形尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45157057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com