Heat map with numbers in ggvis- R
In this article, we will study how to generate a heat map with numbers using the ggvis package in the R programming language.
ggvis package
The ggvis package in R is used to provide the data visualization. It is used to create visual interactive graphics tools for data plotting and representation. The package can be installed into the working space using the following command :
install.packages("ggvis")
Methods Used
ggvis() method
The ggvis method in the ggvis package is used to start ggvis graphical window. The ggvis method has the following syntax :
Syntax: ggvis( data , mp1, mp2.,)
Arguments :
data – The dataset to plot on heat map.
mp1, mp2,.. – The map variables to plot.
layer_rects() method
The layer_rects() method is used to specify any of the two arguments like height, y or y2, and two of the following arguments, width, x, or x2.
layer_text() method
The layer_text method in the ggvis package is equivalent to the geom_text() method. It is used to annotate the plot with text content. It takes as arguments the particular specifications for the x axis as well as the y axis. It can also be used to add text and color to the content added to the plot.
Syntax: layer_text (x , y = , text:= , fill:=)
Arguments :
x – The label of the x axis and its scale positioning.
y – The label of the y axis and its scale positioning.
text– The text to be added to the heatmap.
fill – The color to be added to the heatmap.
Scale_nominal() method
The scale can also be added to the ggvis plot using the scale_nominal() method which is used to specify a scale. The scale_nominal is preferably used if the scale values are factor type in nature.
Syntax: scale_nominal(type, padding)
Arguments :
type – The type of scale.
padding – The padding to be set between the blocks of the heatmap.
Plotting the heat map
To plot a heat map firstly we have to create a data frame using which heat map will be plotted after that we are plotting the heat map by using pipe operator with different methods including ggvis(), layer_rects(), layer_text(), and scale_nominal().
R
# Creating the data frame data_frame <- data.frame ( col1 = c ( "A" , "B" , "C" , "D" , "A" , "B" , "C" , "D" , "A" , "B" , "C" , "D" , "A" , "B" , "C" , "D" ), col2 = c ( "a" , "a" , "a" , "a" , "b" , "b" , "b" , "b" , "c" , "c" , "c" , "c" , "d" , "d" , "d" , "d" ), col3 = c (68,119,26,7,20,84,17,94, 14,54,14,10,5,29,15,16)) # Printing the data frame print ( "Data Frame" ) print (data_frame) # Plotting the heat map data_frame%>% ggvis (~col1, ~col2, fill=~col3) %>% layer_rects (width = band (), height = band ()) %>% layer_text ( x = prop ( "x" , ~col1, scale = "xcenter" ), y = prop ( "y" , ~col2, scale = "ycenter" ), text:=~col3, fill:= "white" ) %>% scale_nominal ( "x" , padding = 0, points = FALSE ) %>% scale_nominal ( "y" , padding = 0, points = FALSE ) |
Output:
[1] "Data Frame" > print(data_frame) col1 col2 col3 1 A a 68 2 B a 119 3 C a 26 4 D a 7 5 A b 20 6 B b 84 7 C b 17 8 D b 94 9 A c 14 10 B c 54 11 C c 14 12 D c 10 13 A d 5 14 B d 29 15 C d 15 16 D d 16

Customizing the Heat Map
The heatmap can be customized by specifying the attributes like the “fontSize” to increase/decrease the font size of the numbers on the heat map. Also, the “fill” argument can be used to change the color with which the numbers in the heat map are plotted in R.
The below code indicates the numbers written with a “yellow” color as indicated with the fill parameter.
R
# Creating the data frame data_frame <- data.frame ( col1 = c ( "A" , "B" , "C" , "D" , "A" , "B" , "C" , "D" , "A" , "B" , "C" , "D" , "A" , "B" , "C" , "D" ), col2 = c ( "a" , "a" , "a" , "a" , "b" , "b" , "b" , "b" , "c" , "c" , "c" , "c" , "d" , "d" , "d" , "d" ), col3 = c (68,119,26,7,20,84,17,94,14, 54,14,10,5,29,15,16)) # Plotting the heat map data_frame%>% ggvis (~col1, ~col2, fill=~col3) %>% layer_rects (width = band (), height = band ()) %>% layer_text ( x = prop ( "x" , ~col1, scale = "xcenter" ), y = prop ( "y" , ~col2, scale = "ycenter" ), text:=~col3, fontSize:= 25, fill:= "yellow" ) %>% scale_nominal ( "x" , padding = 0, points = FALSE ) %>% scale_nominal ( "y" , padding = 0, points = FALSE ) |
Output:

Please Login to comment...