ggplot2::geom_tile 不能指定填充颜色?

ggplot2::geom_tile can not assign fill color?

我有一个数据框,我想用 geom_tile 绘图并根据数据中的 "ID" 变量进行填充,但我发现 fill 在我的代码中不起作用.感谢您的帮助。

set.seed(1234)
x <- sample(x=-2288200:3076160, size = 1000,replace = F)
y <- sample(x=353334.1:4803914.0, size = 1000,replace = F)
ID <- sample(x=1:4, size = 1000,replace = T)
Mydata <- data.frame(x=x,y=y,ID=factor(ID))

group.colors <- c("1"="red","2"="yellow","3"="blue","4"="green")
ggplot() + geom_tile(data = Mydata, aes(x = x, y = y, fill=ID)) + 
  scale_fill_manual(values = group.colors,name = "group")

在没有 heightwidth 参数的情况下使用

geom_tile 将自动选择它们来创建网格。

在你的情况下,这个网格非常窄,因为你的数据非常稀疏,所以你试图显示的图块比你的分辨率可以显示的要小。

我们可以通过评估数据点的宽度和高度来显示它们的位置(尽管在这种情况下我们不再显示网格!)。

ggplot(Mydata) + geom_tile(aes(x = x, y = y, fill=ID),width=100000,height=100000)

比较:

Mydata2 <- data.frame(x=c(1,2,2.5,3),y=c(1,2,2.5,3),ID=1:4)
ggplot(Mydata2) + geom_tile(aes(x = x, y = y, fill=ID))