Alex
Alex

Reputation: 2780

geom_raster(): Simple example

I don't have a problem with geom_raster() when following the old faithful example, but I have not been successful at making my own plot from simulated data. For example, given the following data

R>true

x1     x2    risk
.82    .80   2.44
.87    -.72  -.57
-.42   .97   1.52
.66    .89   2.44
.28   -.83   -1.38

I build a ggplot with the following:

R> p <- ggplot(true, aes(x1, x2)) +
 geom_raster(aes(fill = risk))

R> p

This gives me a blank plot. I am not sure why this is.

This is the output as requested:

> dput(true)
structure(list(x1 = c(0.829612086992711, 0.874150826595724, -0.427720930427313, 
0.660895252134651, 0.283491037786007, 0.0381918982602656, 0.473176629282534, 
-0.730666805524379, 0.313984580803663, 0.410129568073899, -0.0845164475031197, 
0.438224503304809, 0.869344494305551, -0.489142351318151, -0.0754143549129367, 
0.880029045511037, 0.956452856771648, -0.765025276690722, -0.0500058368779719, 
0.120665492489934), x2 = c(0.808062774594873, -0.722579664550722, 
0.977783457841724, 0.89333646511659, -0.835124883800745, 0.0284235686995089, 
-0.219593065790832, 0.811476261820644, -0.10606074379757, 0.672008519992232, 
0.475191235542297, 0.622110282536596, -0.22378343436867, 0.370339458808303, 
-0.992103322409093, 0.665832160506397, -0.985331706237048, -0.584682054352015, 
0.813202815596014, 0.223557286895812), x3 = c(-0.240881518926471, 
-0.128456830047071, -0.925137934274971, 0.947079827543348, -0.136497502215207, 
0.915153193287551, 0.775509811006486, 0.279957538936287, 0.941933220718056, 
0.237676414661109, -0.333145577460527, -0.306503503583372, -0.20302917715162, 
0.569385551381856, -0.922127017751336, 0.497590772341937, 0.35455366037786, 
-0.657471339218318, -0.477824072353542, 0.0288258693180978), 
    x4 = c(0.351214549038559, 0.965634395834059, 0.519088535103947, 
    0.132976848166436, 0.699379437137395, -0.62105212919414, 
    -0.457426770590246, 0.656316970475018, 0.386409640777856, 
    -0.518910520710051, -0.914022407960147, -0.719041811767966, 
    -0.567229169886559, -0.041202871594578, -0.605179315432906, 
    0.438711675349623, -0.984230522532016, -0.249020070768893, 
    0.0288154166191816, -0.996858891565353), x5 = c(0.163208005018532, 
    -0.684189583640546, -0.281943388283253, 0.291263756807894, 
    0.551646725274622, 0.127293683122844, -0.532593202777207, 
    -0.820038967300206, -0.828775870148093, -0.389563261065632, 
    0.334853029344231, -0.999522206839174, -0.582860086113214, 
    0.866068254690617, 0.851289497222751, 0.468188602011651, 
    -0.333856033161283, 0.0301266596652567, 0.487949292641133, 
    0.238318480085582), risk = c(2.44573763618246, -0.57100850250572, 
    1.52784598525614, 2.44756818236783, -1.38675872981548, 0.0950390356592834, 
    0.03399049770087, 0.892285718116909, 0.101863093208522, 1.75414660805836, 
    0.865866023581475, 1.682445068378, 0.421777625568211, 0.251536566298455, 
    -2.05962099973112, 2.21169336652383, -1.01421055570245, -1.93438938539475, 
    1.57639979431406, 0.567780066281557), time = c(12.9241509784045, 
    0.754603959518346, 0.677189139459449, 2.07339828770144, 0.109777657032991, 
    1.39996365414718, 0.167462659974731, 1.09586349933275, 0.463151062910805, 
    2.32239195497809, 1.52799525778206, 13.5096449131547, 2.39962724414737, 
    3.03968330607599, 0.1532054718408, 5.79336751789379, 0.633832566535867, 
    0.0149323120147296, 2.12249155520952, 1.74580444108114), 
    is_censored = c(0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 
    0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L)), .Names = c("x1", "x2", 
"x3", "x4", "x5", "risk", "time", "is_censored"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L))
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] gridExtra_2.2.1 dplyr_0.5.0     purrr_0.2.2     readr_1.0.0     tidyr_0.6.1     tibble_1.2     
[7] ggplot2_2.2.1   tidyverse_1.1.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.9      plyr_1.8.4       forcats_0.2.0    tools_3.3.1      digest_0.6.12    evaluate_0.10   
 [7] jsonlite_1.2     lubridate_1.6.0  gtable_0.2.0     nlme_3.1-131     lattice_0.20-34  psych_1.6.12    
[13] DBI_0.5-1        yaml_2.1.14      parallel_3.3.1   haven_1.0.0      stringr_1.2.0    httr_1.2.1      
[19] knitr_1.15.1     xml2_1.1.1       hms_0.3          rprojroot_1.2    R6_2.2.0         readxl_0.1.1    
[25] rmarkdown_1.3    foreign_0.8-67   reshape2_1.4.2   modelr_0.1.0     magrittr_1.5     backports_1.0.5 
[31] htmltools_0.3.5  scales_0.4.1     assertthat_0.1   mnormt_1.5-5     rvest_0.3.2      colorspace_1.3-2
[37] labeling_0.3     stringi_1.1.2    lazyeval_0.2.0   munsell_0.4.3    broom_0.4.2 

The following code reproduces the problem better than the previous example:

true <- list(x1 = c(0.829612086992711, 0.874150826595724, -0.427720930427313, 
0.660895252134651, 0.283491037786007, 0.0381918982602656, 0.473176629282534, 
-0.730666805524379, 0.313984580803663, 0.410129568073899, -0.0845164475031197, 
0.438224503304809, 0.869344494305551, -0.489142351318151, -0.0754143549129367, 
0.880029045511037, 0.956452856771648, -0.765025276690722, -0.0500058368779719, 
0.120665492489934), x2 = c(0.808062774594873, -0.722579664550722, 
0.977783457841724, 0.89333646511659, -0.835124883800745, 0.0284235686995089, 
-0.219593065790832, 0.811476261820644, -0.10606074379757, 0.672008519992232, 
0.475191235542297, 0.622110282536596, -0.22378343436867, 0.370339458808303, 
-0.992103322409093, 0.665832160506397, -0.985331706237048, -0.584682054352015, 
0.813202815596014, 0.223557286895812), risk = c(2.44573763618246, -0.57100850250572, 
    1.52784598525614, 2.44756818236783, -1.38675872981548, 0.0950390356592834, 
    0.03399049770087, 0.892285718116909, 0.101863093208522, 1.75414660805836, 
    0.865866023581475, 1.682445068378, 0.421777625568211, 0.251536566298455, 
    -2.05962099973112, 2.21169336652383, -1.01421055570245, -1.93438938539475, 
    1.57639979431406, 0.567780066281557))
true <- as_tibble(true)
true

p6 <- ggplot(true, aes(x1, x2)) +
 geom_raster(aes(fill = risk))
p6

Upvotes: 0

Views: 1603

Answers (1)

Miha
Miha

Reputation: 2874

Your plot is not blank, because of small numbers in x1 and x2 and ggplot axes you cant see them.

So in order to see them in plot I've rounded all numbers in x1 and x2 to two decimals.

Firstly I've converted true to data frame and then rounded all numbers.

true <-  as.data.frame(true)
number <- sapply(true, is.numeric)
true[number ] <- lapply(true[number ], round, 2)

Then I've ploted using your code

p6 <- ggplot(true, aes(x1, x2)) +
  geom_raster(aes(fill = risk)) 
p6

And the output:

enter image description here

Note if you don't like to round up your numbers you could use scale_x_continuous(limits = c()) and scale_y_continuous(limits = c()) in order your data would become visible.

Upvotes: 1

Related Questions