geom_raster 不绘制所有值

geom_raster does not plot all values

我在 geom_raster 中有一个奇怪的行为。由于某种原因,它不会在所有值中绘制位置图块。它应该绘制 1015 个非零颜色值,但我只得到了几个图块。如果您使用 dat <- subset(dat, w!=0) 过滤掉非零值,那么它将绘制所有值,但这不是我想要的!我需要整个大矩阵。

这是一个包含所有数据的完整工作示例。

谢谢!!

library(tidyverse)
dat <- read.csv("https://www.dropbox.com/s/x25znfxb1kyvvo8/geom_raster_data.csv?dl=1")
names(dat) <- c('x','y','w')
nrow(subset(dat, w!=0)) # there are 1015 non-zero values to plot
# dat <- subset(dat, w!=0) # for debugging but not for the end result
ggplot(dat, aes(x,y, fill=w))+
geom_raster()+
  scale_fill_gradient2(low = "white", mid='blue', high = "red",midpoint = 2, limit = c(0,4))+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())+
  scale_x_continuous(breaks=seq(1,max(dat$x),10))+
  scale_y_continuous(breaks=seq(1,max(dat$y),10))

我的会话和 tidyverse 信息:

> library(tidyverse)
── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
✔ ggplot2 3.1.0     ✔ purrr   0.2.5
✔ tibble  1.4.2     ✔ dplyr   0.7.8
✔ tidyr   0.8.2     ✔ stringr 1.3.1
✔ readr   1.1.1     ✔ forcats 0.3.0
── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS

Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] forcats_0.3.0   stringr_1.3.1   dplyr_0.7.8     purrr_0.2.5     readr_1.1.1     tidyr_0.8.2     tibble_1.4.2    ggplot2_3.1.0  
[9] tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0       cellranger_1.1.0 pillar_1.3.0     compiler_3.4.4   plyr_1.8.4       bindr_0.1.1      tools_3.4.4      digest_0.6.12   
 [9] lubridate_1.7.1  jsonlite_1.5     nlme_3.1-137     gtable_0.2.0     lattice_0.20-38  pkgconfig_2.0.1  rlang_0.3.0.1    psych_1.7.8     
[17] cli_1.0.0        rstudioapi_0.7   yaml_2.2.0       parallel_3.4.4   haven_1.1.0      bindrcpp_0.2.2   withr_2.1.1      xml2_1.1.1      
[25] httr_1.3.1       hms_0.3          grid_3.4.4       tidyselect_0.2.5 glue_1.3.0       R6_2.2.2         readxl_1.0.0     session_1.0.3   
[33] foreign_0.8-70   modelr_0.1.1     reshape2_1.4.2   magrittr_1.5     scales_0.5.0     rvest_0.3.2      assertthat_0.2.0 mnormt_1.5-5    
[41] colorspace_1.3-2 labeling_0.3     stringi_1.2.4    lazyeval_0.2.0   munsell_0.4.3    broom_0.4.3      crayon_1.3.4    

我认为这是因为您的数据在 w 中的 0(白色)更为常见,并且只要它们稍后出现在源数据中就会遮挡其他值,这使得它们稍后绘制(在顶部) .

> table(dat$w)
#     0      1      2      3      4 
#216449    557    383     74      1 

您可以安排您的数据先绘制零,然后绘制更高的 w

ggplot(dat %>% arrange(w), aes(x,y, fill=w))+
  ....

或者您可以在没有零的情况下进行绘图,并使用 coord_cartesian 定义绘图范围以显示整个范围,这似乎得到了非常相似的结果。

ggplot(dat %>% filter(w != 0), aes(x,y, fill=w))+
  geom_raster()+
  scale_fill_gradient2(low = "white", mid='blue', high = "red",midpoint = 2, limit = c(0,4))+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())+
  coord_cartesian(xlim = range(dat$x), ylim = range(dat$y)) +
  scale_x_continuous(breaks=seq(1,max(dat$x),10))+
  scale_y_continuous(breaks=seq(1,max(dat$y),10))