How to show legend in heatmap in R?
A heat map is a graphical representation of data where each data value is represented in terms of color value. Heatmap is created using heatmap() function in R. Legend associated with histogram makes it easy to understand what the color values mean. Legend is shown with histogram using legend() function in R.
- heatmap() function in R Language is used to plot a heatmap. Heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix.
data: It represent matrix data, such as values of rows and columns
Return: This function draws a heatmap
- legend() function in R Language is used to add legends to an existing Plot. A legend is defined as an area of the graph plot describing each of the parts of the plot. The legend plot is used to show statistical data in graphical form.
legend(x, y, legend, fill, col, bg, lty, cex, title, text.font, bg)
x and y: These are co-ordinates to be used to position the legend
legend: Text of the legend
fill: Colors to use for filling the boxes with legend text
col: Colors of lines
bg: It defines background color for the legend box.
title: Legend title (optional)
text.font: An integer specifying the font style of the legend (optional)
Returns: Legend plot
- Create data matrix
- Plot heatmap using heatmap() method
- Provide appropriate attributes with respective values
- Associate legend with heatmap using legend() method
- Run code to display plot