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.
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 :
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.,)
data – The dataset to plot on heat map.
mp1, mp2,.. – The map variables to plot.
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.
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:=)
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.
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)
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().
 "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.
Please Login to comment...