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
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.