通过使用ggplot geom_tile绘制为空

时间:2019-06-04 08:02:45

标签: r ggplot2

我多次尝试过,简单的光栅图是空的。但是可以绘制子数据帧。因此,我认为ggplot中存在一些错误?数据帧不是很大,大约1万行。

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可以是图:

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,],则图也为空。

1 个答案:

答案 0 :(得分:1)

之所以不起作用,是因为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