gpt4 book ai didi

r - 使用 ggplot geom_tile 绘图为空

转载 作者:行者123 更新时间:2023-12-05 03:00:52 27 4
gpt4 key购买 nike

我试过很多次,一个简单的光栅绘图是空的。但是可以绘制子数据框。所以我考虑了 ggplot 中的一些错误?数据框不是很大,大约 10k 行。

df <-
tibble::tribble(
~x, ~y, ~mean, ~k, ~p, ~k1,
5044676.13, 5567208.267, 76.17061754, -0.346729916, 0.006967871, "(-1,0]",
5076676.13, 5487208.267, 71.42755804, -0.430442239, 0.011792143, "(-1,0]",
5188676.13, 5463208.267, 77.64019292, 0.230009537, 0.072617934, "(0,1]",
5148676.13, 5447208.267, 71.11206476, -0.244530952, 0.055665191, "(-1,0]",
4932676.13, 5399208.267, 47.52124286, -0.196172453, 0.060010053, "(-1,0]",
5036676.13, 5351208.267, 69.77565423, 0.043384786, 0.366328498, "(0,1]",
4980676.13, 5343208.267, 65.96337177, -0.348177839, 8.43e-06, "(-1,0]",
5252676.13, 5295208.267, 116.3495365, 0.124572049, 0.584311077, "(0,1]",
4932676.13, 5279208.267, 65.1707162, 0.242013783, 0.114344889, "(0,1]",
5060676.13, 5271208.267, 66.02839503, 0.084724445, 0.264818634, "(0,1]",
5100676.13, 5271208.267, 154.3897871, -0.937553354, 0.000412151, "(-1,0]",
4820676.13, 5255208.267, 51.91877256, 0.098445837, 0.344049825, "(0,1]",
5140676.13, 5207208.267, 77.23553977, 0.156881858, 0.40217419, "(0,1]",
5108676.13, 5199208.267, 68.81369622, 0.178698519, 0.385465678, "(0,1]",
5436676.13, 5183208.267, 66.09671277, 0.692167267, 5.14e-05, "(0,1]",
5308676.13, 5159208.267, 80.44536398, 0.661491252, 8.71e-06, "(0,1]",
5156676.13, 5151208.267, 93.74818693, 0.258671139, 0.608011344, "(0,1]",
5540676.13, 5095208.267, 56.00412177, 0.099220449, 0.043643428, "(0,1]",
5148676.13, 5087208.267, 73.24156915, 0.358186618, 0.000969162, "(0,1]",
5004676.13, 5055208.267, 69.08699846, 0.034930502, 0.890667984, "(0,1]",
4956676.13, 5047208.267, 74.32986053, 0.352816479, 0.005968131, "(0,1]",
4932676.13, 5031208.267, 78.00311487, 0.193964017, 0.019122869, "(0,1]",
5156676.13, 5015208.267, 84.56156184, 0.666074789, 7.48e-06, "(0,1]",
5020676.13, 4999208.267, 71.300834, 0.361447053, 0.000128288, "(0,1]",
5092676.13, 4975208.267, 83.26818987, 0.169168888, 0.813224317, "(0,1]",
4828676.13, 4951208.267, 60.09398879, -0.037820435, 0.103691265, "(-1,0]",
5260676.13, 4951208.267, 81.2741034, 0.227406132, 0.002667465, "(0,1]",
5068676.13, 4943208.267, 86.64496894, 0.368699429, 0.029908722, "(0,1]",
4980676.13, 4935208.267, 77.90167945, 0.398615701, 0.00804898, "(0,1]",
5076676.13, 4911208.267, 84.79035869, 0.322434819, 0.072627115, "(0,1]",
4724676, 4903208, 61.15754063, 0.617237556, 5.46e-05, "(0,1]"
)

ggplot(data = df, aes(x = x, y = y, fill = k1))+
geom_tile()+
scale_fill_manual(
na.value = 'grey',
values = c(
'#1f78b4','#a6cee3','#33a02c','#b2df8a','#fdbf6f',
'#ff7f00','#fb9a99','#e31a1c','#cab2d6','#6a3d9a'
)
)

出图是空的,没有任何彩色单元格:enter image description here

但是 sub-df 可能是 plot:

ggplot(data=df[1:2999,],aes(x=x,y=y,fill=k1))+
geom_tile()+
scale_fill_manual(values=c('#1f78b4','#a6cee3','#33a02c','#b2df8a','#fdbf6f',
'#ff7f00','#fb9a99','#e31a1c','#cab2d6','#6a3d9a'),
na.value='grey' )

如果数据帧切片超过 3000 行,例如 df[1:3000,],绘图也为空。

最佳答案

这不起作用的原因是 x 和 y 的间隔不是均匀分布的,并且瓷砖在那里,它们非常非常小。您可以使用 geom_point()factor(x)factor(y)

进行测试
colors_k <- c(
"#1f78b4", "#a6cee3", "#33a02c", "#b2df8a", "#fdbf6f",
"#ff7f00", "#fb9a99", "#e31a1c", "#cab2d6", "#6a3d9a"
)

df %>%
slice(1:1000) %>%
ggplot(aes(x = x, y = y, color = k1)) +
geom_point(size = 1, alpha = 0.5) +
scale_color_manual(
na.value = "grey",
values = colors_k
)

df %>%
filter(x %in% c(5228676.13, 5100676.13, 5044676.13)) %>%
slice(1:30) %>%
ggplot(aes(x = factor(x), y = factor(y), fill = k1)) +
geom_tile() +
scale_fill_manual(
na.value = "grey",
values = colors_k
)

enter image description here

关于r - 使用 ggplot geom_tile 绘图为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56439711/

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