I have a dataframe and I want to plot with geom_tile
and fill according to the "ID" variable in the data, but I find that fill
is not working in my code. Thanks for any help.
我有一个数据框,我想用geom_tile绘图并根据数据中的“ID”变量填充,但我发现填充在我的代码中不起作用。谢谢你的帮助。
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")
1 个解决方案
#1
2
geom_tile
used without a height
and width
parameter will choose them automatically to create a grid.
没有高度和宽度参数的geom_tile会自动选择它们来创建网格。
In your case this grid is very narrow as your data is very sparse, so the tiles that you're trying to show are smaller than your resolution can show.
在您的情况下,由于您的数据非常稀疏,因此此网格非常窄,因此您尝试显示的切片小于分辨率可显示的切片。
We can show their positions by assessing a width and height to the data points (though we're not showing a grid anymore in this case!).
我们可以通过评估数据点的宽度和高度来显示它们的位置(尽管在这种情况下我们不再显示网格!)。
ggplot(Mydata) + geom_tile(aes(x = x, y = y, fill=ID),width=100000,height=100000)
Compare it with:
比较它:
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))
#1
2
geom_tile
used without a height
and width
parameter will choose them automatically to create a grid.
没有高度和宽度参数的geom_tile会自动选择它们来创建网格。
In your case this grid is very narrow as your data is very sparse, so the tiles that you're trying to show are smaller than your resolution can show.
在您的情况下,由于您的数据非常稀疏,因此此网格非常窄,因此您尝试显示的切片小于分辨率可显示的切片。
We can show their positions by assessing a width and height to the data points (though we're not showing a grid anymore in this case!).
我们可以通过评估数据点的宽度和高度来显示它们的位置(尽管在这种情况下我们不再显示网格!)。
ggplot(Mydata) + geom_tile(aes(x = x, y = y, fill=ID),width=100000,height=100000)
Compare it with:
比较它:
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))