Total members 11895 |It is currently Wed Dec 18, 2024 9:49 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





In this example we show how to make a good looking heatmap using the ggplot2 R package. Following is the code example of using ggplot2 to plot the data as Heatmap.
php code
projectName="XYZ"
RecallData <- read.csv("RankRecall_5_.csv",head=TRUE );
ApproachValue="BM25Fext"
msg=paste(projectName,ApproachValue,': Recall rate for each   training data',sep='')
 
#head(RecallData)

 

RecallData$month_name2 <- factor(RecallData$month_name,levels=c('1/2009' , '2/2009' ,'3/2009', '4/2009' ,'5/2009' ,'6/2009', '7/2009' ,'8/2009','9/2009','10/2009','11/2009','12/2009'));
# here comes the RColorBrewer package, now if you ask me why did you choose this palette colour 
#I would say look at your battery charge indicator of your mobile for example your shaver,
# won't be red when gets low? and back to green when charged? This was the inspiration to choose this colour set.
ggplot(RecallData, aes(month_name2,distance)) +ylab("Training data by month")+xlab("Month/ Year") +
  geom_tile(aes(fill= (recall)),colour="black") + guides(colour = guide_legend(override.aes = list(size=0.3),ncol=1))+
  scale_fill_gradient2(na.value = "red", limits=c(0.35, 0.63), midpoint=median(0.51),guide="colorbar",low =  ("red"), mid = "yellow", high = "green", name="Recall \n Rate")+
  theme(legend.key.width=unit(0.2,"cm"),legend.margin=unit(-0.0,"cm"), legend.key.size=unit(0.4,"cm"),panel.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),plot.title = element_text(size=4), plot.margin=unit(c(0,0,0,0),"mm"),axis.text.x=element_text(angle=-90,vjust= 0.0 ),axis.text = element_text(colour = "BLACK"),axis.text.x=element_text(colour="black"), text = element_text(size=10))

 
 






The script starts by creating a variable "ApproachValue" and assigns it the string "BM25Fext". Then it creates a variable "msg" which is a concatenation of the "projectName" variable, "ApproachValue" variable and some string, separated by empty space. This message is used later in the script. The script then performs some data manipulation on a dataframe called "RecallData". It adds a new column to the dataframe called "month_name2" which is a factor variable created by transforming the "month_name" column and setting the levels to a specific list of months. The script then uses the ggplot2 library to create a heatmap of the "RecallData" dataframe. It uses the ggplot() function to create the basic plot and specifies that the x-axis should be the "month_name2" column and the y-axis should be the "distance" column. It also adds labels for the x and y-axes. The script then adds a geom_tile() layer to the plot, which creates a tile plot of the data. It maps the fill aesthetic to the "recall" column of the dataframe. The script also adds a color guide to the plot with a specific color palette and legend. The script then uses the scale_fill_gradient2() function to create a color gradient for the tiles in the plot, with different colors for different recall rates. The script also sets a colorbar, and a specific title. The script uses the theme() function to customize the appearance of the plot, including the size and color of the legend, the background color of the plot, and the text properties of the plot. The generated HeatMap from this snippet is using the traffic light colors. Theme information for this heatmap is controlled in theme function.



The script uses the ggplot2 package, which is a powerful data visualization library in R. ggplot2 is built on the grammar of graphics, which is a set of rules for creating plots. The basic idea behind the grammar of graphics is that you can create complex plots by adding different layers, each of which describes a different aspect of the data. In this script, the basic plot is created with the ggplot() function, which takes the data as the first argument and the mapping of the x and y-axes as the second argument. The script then adds layers to the plot to customize the appearance. The first layer added is the geom_tile() function, which creates a tile plot of the data. It maps the fill aesthetic to the "recall" column of the dataframe. The script also adds a color guide to the plot with a specific color palette and legend. The next step is adding the scale_fill_gradient2() function, it helps to create a color gradient for the tiles in the plot, with different colors for different recall rates. The function also set a colorbar and title. Finally, the script uses the theme() function to customize the appearance of the plot, including the size and color of the legend, the background color of the plot, and the text properties of the plot.

The script also uses the ylab() and xlab() functions to add labels to the y and x-axes, respectively. The script also uses the guides() function to add a color legend to the plot. The theme() function is used to customize the appearance of the plot, including the size and color of the legend, the background color of the plot, and the text properties of the plot. The script also uses the angle=-90 and vjust= 0.0 argument in axis.text.x=element_text() function to rotate the x-axis labels by 90 degrees, and to align them at the bottom of the plot.




Attachments:
Rplot03.png
Rplot03.png [ 3.65 KiB | Viewed 10382 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Good looking HeatMap example in R using ggplot2
 ggplot2 for many boxplots in one figure example     -  
 Why is Using Gamification Good for Your Business?     -  
 here are 2 realy good books for sql     -  
 good books of jsp and jdbc     -  
 Good looking selection box with image per option     -  
 project in java.....plz suggest good ones........     -  
 need a good topic for project in java     -  
 How does one get good code for embeded video     -  
 Convert link and button to custom button with good style     -  



Topic Tags

R Analysis
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com