borders around heatmaps in ggplot2 with geom_tile

5,819 views
Skip to first unread message

Mark Dwain

unread,
Jan 22, 2014, 11:18:33 AM1/22/14
to ggp...@googlegroups.com
Hi all,

I've been trying to generate a heatmap in ggplot2 according to the code from this web page:

http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/


However, I would like to generate one in which the internal grid lines are white (separating cells from each other), but I want a black border around the outer edge of the heatmap (surrounding the cells, but not the x and y labels).  I've tried to change the final heatmap drawing command to the following (i.e. tried to use panel.background), but it doesn't seem to do anything.  Any suggestions?  Thanks!



(p <- ggplot(nba.m, aes(variable, Name)) + geom_tile(aes(fill = rescale),colour='white') + scale_fill_gradient(low = "white",high = "steelblue")) + opts(panel.background=element_rect(colour='black',size=10)); 

base_size <- 9;  

p + theme_grey(base_size = base_size) + labs(x = "",y = "") + scale_x_discrete(expand = c(0, 0)) +scale_y_discrete(expand = c(0, 0)) + opts(legend.position = "none",axis.ticks = theme_blank(), panel.background=element_rect(colour='black',size=10), axis.text.x = theme_text(size = base_size *0.8, angle = 330, hjust = 0, colour = "grey50"))

Ista Zahn

unread,
Jan 22, 2014, 12:05:17 PM1/22/14
to Mark Dwain, ggplot2
Hi Mark,

A few things:

1) Unless you have an old version of ggplot2 you should be using theme
instead of opt and element_* instead of theme_*
2) The rectangle is being drawn, but is covered by the plot itself
since you've set expand = (0,0) and panel.background is drawn under
the plot
3) The list of theme elements is documented in ?theme, which should
lead to you panel.border
4) You will still need to to coordinate the size of the border with
the x and y axis scale expansion--it can be helpful to use a
transparent border at first to get the size right

For example:

p + theme(panel.border=element_rect(fill = NA, colour=alpha('black',
.5),size=10))
## border covers substantial part of the plot, so:
p + scale_x_discrete(expand = c(.02, 0)) +
scale_y_discrete(expand = c(.02, 0)) +
theme(panel.border=element_rect(fill = NA, colour=alpha('black', .5),size=10))

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages