dabsingh
dabsingh

Reputation: 290

R ggplot geom_tile without fill color

I am trying to add a geom_tile layer to a plot without the filled color (just the outline). Is there a way to get a transparent tile where only the boundary is visible?

Thanks

Upvotes: 6

Views: 14147

Answers (2)

mnel
mnel

Reputation: 115382

If you only want the outlines as a single colour you can set fill = NA, and then set the na.value to NA

.data <- cbind( 
           expand.grid(x = 1:10, y = 1:10), z = runif(100))[sample(1:100,75), ]



ggplot(.data, aes(x = x, y = y)) + theme_bw() + 
   geom_tile(fill = NA, color = 'black', na.value = NA) 

Upvotes: 5

daroczig
daroczig

Reputation: 28622

I think you are after alpha parameter. Minimal example:

  1. Create a plot with dummy data where you set color (for "boundary") and no fill:

    p <- ggplot(pp(20)[sample(20*20, size=200), ], aes(x = x, y = y, color = z))
    
  2. Add geom_tile() with alpha set to zero:

    p <- geom_tile(alpha=0)
    
  3. Add theme_bw() as transparent tiles look lame with a dark gray background :)

    p + theme_bw() 
    

enter image description here

Upvotes: 14

Related Questions