在ggplot中使用geom_tile时没有任何单元格

时间:2019-05-30 05:36:17

标签: r ggplot2

我对此问题感到困惑。请看一下示例:

这是一个数据框:

df <- read.table(text='    
2945 5316676 4919208 -0.318315496
2946 5324676 4919208 -0.316043143
2947 4732676 4911208  0.095768489
2948 4740676 4911208 -0.000249839
2949 4748676 4911208 -0.127810694
2950 4756676 4911208 -0.075670759
2951 4764676 4911208 -0.074638618
2952 4772676 4911208 -0.189594448
2953 4780676 4911208 -0.197823095
2954 4788676 4911208 -0.165911603
2955 4796676 4911208 -0.198732861
2956 4804676 4911208 -0.149789006
2957 4812676 4911208 -0.142609068
2958 4820676 4911208 -0.218731066
2959 4828676 4911208 -0.218989030
2960 4836676 4911208 -0.139803756
2961 4844676 4911208 -0.262451188
2962 4852676 4911208 -0.089113979
2963 4860676 4911208 -0.222817726
2964 4876676 4911208 -0.118478222
2965 4884676 4911208 -0.201948104
2966 4892676 4911208 -0.138716087
2967 4900676 4911208 -0.141654332
2968 4908676 4911208 -0.131766707
2969 4916676 4911208 -0.162517493
2970 4924676 4911208 -0.156362956
2971 4932676 4911208 -0.057538337
2972 4940676 4911208 -0.016829993
2973 4948676 4911208  0.042319785
2974 4956676 4911208  0.045513068
2975 4964676 4911208 -0.030872974
2976 4972676 4911208 -0.042294013
2977 4980676 4911208 -0.086931466
2978 4988676 4911208 -0.088569548
2979 4996676 4911208  0.204320004
2980 5004676 4911208  0.099770825
2981 5012676 4911208  0.121779552
2982 5020676 4911208  0.123398083
2983 5028676 4911208 -0.105294391
2984 5036676 4911208  0.178033225
2985 5044676 4911208  0.061551025
2986 5052676 4911208  0.013276625
2987 5060676 4911208  0.312718483
2988 5068676 4911208  0.355601137
2989 5076676 4911208  0.144966243
2990 5084676 4911208  0.480114836
2991 5092676 4911208  0.418587256
2992 5116676 4911208 -0.123100023
2993 5124676 4911208  0.016093180
2994 5140676 4911208 -0.259877196
2995 5148676 4911208 -0.309331121
2996 5156676 4911208 -0.119554556
2997 5172676 4911208 -0.249161162
2998 5180676 4911208 -0.265029390
2999 5188676 4911208 -0.252866284
3000 5196676 4911208  0.015278095')

使用以下代码绘制栅格图:

colnames(df) <- c('row','x','y','relation')
#legend color
color_ls <- c('#1f78b4','#a6cee3','#33a02c','#b2df8a','#fdbf6f',
      '#ff7f00','#fb9a99','#e31a1c','#cab2d6','#6a3d9a','#4F0080')
##cut breaks
brk <- seq(-1,1,by=0.2)
##cut as breaks and plot
df$relation_fct <- cut(df$relation,breaks=brk)
ggplot(data=df,aes(x=x,y=y,fill=relation_fct))+
  geom_tile()+
  scale_fill_manual(values=color_ls,
                      na.value='grey' )

没有任何单元格显示。 但是,如果将df

的一部分切成薄片
df2 <- df[35:55,]
df2$relation_fct <- cut(df2$relation,breaks=brk)
ggplot(data=df2,aes(x=x,y=y,fill=relation_fct))+
  geom_tile()+
  scale_fill_manual(values=color_ls,
                      na.value='grey' )

做得好。 绘制df有什么关系?

有人测试了上面的代码就可以了。真奇怪。您可以下载this file,提取代码:dWPANf7f

运行代码如下:

df3 <- read.csv('df1.csv')
##silced `df3` is the table exhibited on top
df3 <- df3[2945:3000,]
brk <- seq(-1,1,by=0.2)
df3$relation_fct <- cut(df3$relation,breaks=brk)
###color_ls is in the above code
ggplot(data=df3,aes(x=x,y=y,fill=relation_fct))+
      geom_tile()+
      scale_fill_manual(values=color_ls,
                      na.value='grey' )

0 个答案:

没有答案