Home > Blockchain >  Adjust grid lines in ggplot geom_tile (heatmap) or geom_raster
Adjust grid lines in ggplot geom_tile (heatmap) or geom_raster

Time:08-02

This heatmap has a grid builtin, which I am failing to find the way to customize.

I want to preserve horizontal lines in the grid, if possible increase thickness, and disable vertical lines. Each row should look as a continuous time-serie where data is present and blank where it is not.

Either adding vertical/horizontal lines on-top would possibly cover some data, because of that grid lines, or controlled gaps between tiny rectangles, is preferable.

heatmap with geom_tile

Alternativelly, geom_raster doesn't shows any grid at all. With which I would need to add the horizontal lines of the grid.

heatmap with geom_raster

I tried changing linetype, the geom_tile argument, which does seem to change the type or allow to fully disable it with linetype=0, fully disabling the grid, but it wouldn't allow to preserve horizontal grid-lines. I didn't saw any changes by modifying the size argument.

This is the code generating the plot as above:

ggplot( DF, aes( x=rows, y=name, fill = value) )     
    #geom_raster( )  
    geom_tile( colour = 'white' )  
    scale_fill_gradient(low="steelblue", high="black", 
                        na.value = "white") 
    theme_minimal()  
    theme( 
      legend.position = "none",
      plot.margin=margin(grid::unit(0, "cm")),
      #line = element_blank(),
      #panel.grid = element_blank(),
      panel.border = element_blank(),
      panel.grid = element_blank(),
      panel.spacing = element_blank(),
      #panel.grid = element_line(color="black"),
      #panel.grid.minor = element_blank(),
      plot.caption = element_text(hjust=0, size=8, face = "italic"),
      plot.subtitle = element_text(hjust=0, size=8),
      plot.title   = element_text(hjust=0, size=12, face="bold"))  
    labs( x = "", y = "",
      #caption= "FUENTE: propia",
      fill = "Legend Title",
      #subtitle = "Spaces without any data (missing, filtered, etc)",
      title = "Time GAPs"
    )

I tried to attach DF %>% dput but I get Body is limited to 30000 characters; you entered 203304. If anyone is familiar with a similar Dataset, please advise.

dataset screenshot

Additionally,

  • There are 2 gaps at left&right of the plot area, one is seen inbetween the y-axis, and at the right you can see the X-axis outbounding, and are not controlled by a plot.margin argument.
  • I would want to set the grid to a thicker line when month changes.

CodePudding user response:

The following data set has the same names and essential structure as your own, and will suffice for an example:

set.seed(1)

DF <- data.frame(
  name = rep(replicate(35, paste0(sample(0:9, 10, T), collapse = "")), 100),
  value = runif(3500),
  rows = rep(1:100, each = 35)
)

Let us recreate your plot with your own code, using the geom_raster version:

library(ggplot2)

p <- ggplot( DF, aes( x=rows, y=name, fill = value) )     
    geom_raster( )  
    scale_fill_gradient(low="steelblue", high="black", 
                        na.value = "white")  
    theme_minimal()  
    theme( 
      legend.position = "none",
      plot.margin=margin(grid::unit(0, "cm")),
      panel.border = element_blank(),
      panel.grid = element_blank(),
      panel.spacing = element_blank(),
      plot.caption = element_text(hjust=0, size=8, face = "italic"),
      plot.subtitle = element_text(hjust=0, size=8),
      plot.title   = element_text(hjust=0, size=12, face="bold"))  
    labs( x = "", y = "", fill = "Legend Title", title = "Time GAPs")

p

The key here is to realize that discrete axes are "actually" numeric axes "under the hood", with the discrete ticks being placed at integer values, and factor level names being substituted for those integers on the axis. That means we can draw separating white lines using geom_hline, with values at 0.5, 1.5, 2.5, etc:

p   geom_hline(yintercept = 0.5   0:35, colour = "white", size = 1.5) 

To change the thickness of the lines, simply change the size parameter.

Created on 2022-08-01 by the reprex package (v2.0.1)

  • Related