I have an odd behavior in geom_raster. It does not plot place tiles in all the values for some reason. It should plot 1015 non-zero values in color, but I only get a few tiles. If you filter out the non-zero values with dat <- subset(dat, w!=0)
then it will plot all values, but that is not what I want! I need the whole big matrix.
Here is a complete working example including all the data.
Thank you!!
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))
My session and tidyverse Info:
> 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
I think this is because your data's 0's in w
(in white) are much more common and occlude the other values whenever they appear later in the source data, which makes them plot later (on top).
> table(dat$w)
# 0 1 2 3 4
#216449 557 383 74 1
You could arrange your data to plot the zero's first, and the higher w
's after:
ggplot(dat %>% arrange(w), aes(x,y, fill=w))+
....
Or you might plot without the zeros and define the plotting range with coord_cartesian
to show the whole range, which seems to get a pretty similar result.
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))