Elin
Elin

Reputation: 81

Have same heat legend for two different heatmap plots, ggplot2, Rstudio

Summary and info of Problem

I have two big datasets (as in a matrix-layout but keeping them as df-s) with 100 columns and 11640 rows. Each row represent time hourly and each column a specific depth. For the two data sets (named UCCO2 and RCCO2), the time are the same BUT the depths differs (i.e. different specific depth). The range of UCCO2 is 0 to 12 and for RCCO2 it is 0 to ~30 Please help me create ONE legend for both of them. I only manage to create the plots with different legends (Notice: I'm having them on separated windows)

Presenting Data Sets

Below is a summary of the data that is After the melt() command. I've after commad melt() added dates again (date_dates) and are the last column seen here.

In the plot later: The x-axis is: "date_dates" --> The hourly measurement time

The y-axis is: "variable" --> This is depths and as can be seen in the code further down, Fill is the value column.

Dataset name: UCCO2_m & RCCO2_m

summary(UCCO2_m)         

         DATE            variable           value       
     2014-10-26 02:00:00:    200   124    :  11640   Min.   : 1.2    
     2014-06-14 00:00:00:    100   123    :  11640   1st Qu.: 6.8    
     2014-06-14 01:00:00:    100   121    :  11640   Median : 8.4    
     2014-06-14 02:00:00:    100   120    :  11640   Mean   : 8.1    
     2014-06-14 03:00:00:    100   119    :  11640   3rd Qu.: 9.6    
     2014-06-14 04:00:00:    100   118    :  11640   Max.   :12.1    
     (Other)            :1163300   (Other):1094160   NA's   :657399  
       date_dates                 
     Min.   :2014-06-14 00:00:00  
     1st Qu.:2014-10-13 05:45:00  
     Median :2015-02-11 10:30:00  
     Mean   :2015-02-11 10:29:59  
     3rd Qu.:2015-06-12 17:15:00  
     Max.   :2015-10-11 23:00:00 


> summary(RCCO2_m)
                  DATE            variable           value       
 2014-10-26 02:00:00:    200   60     :  11640   Min.   : 1.14   
 2014-06-14 00:00:00:    100   59     :  11640   1st Qu.:10.82   
 2014-06-14 01:00:00:    100   59.1   :  11640   Median :14.51   
 2014-06-14 02:00:00:    100   58     :  11640   Mean   :13.98   
 2014-06-14 03:00:00:    100   58.1   :  11640   3rd Qu.:17.37   
 2014-06-14 04:00:00:    100   57     :  11640   Max.   :27.64   
 (Other)            :1163300   (Other):1094160   NA's   :221208  
   date_dates                 
 Min.   :2014-06-14 00:00:00  
 1st Qu.:2014-10-13 05:45:00  
 Median :2015-02-11 10:30:00  
 Mean   :2015-02-11 10:29:59  
 3rd Qu.:2015-06-12 17:15:00  
 Max.   :2015-10-11 23:00:00  

How the df-s look like

An example how the matrix looks like before command "melt()"

df before melt, rows: 11640, columns: 100 if excluding date columns (note that in R date are only ONE column df before melt, rows: 11640, columns: 100 if excluding date columns (note that in R date are only ONE column

An example of df AFTER melt()

df after, where <code>DATE</code> was used as the factor to melt with, <code>variable</code> is depth (the column names), <code>value</code> is the value at that time and depth, <code>date_dates</code> is the column I add after and are used as x-axis df after, where DATE was used as the factor to melt with, variable is depth (the column names), value is the value at that time and depth, date_dates is the column I add after and are used as x-axis

Method - Rcode

Description of code: I'm using ggplot2 to create the heatmaps. After melt and the step where I add another date column (to use for x-axis values (don't know if this is necessary but otherwise the x-axis values look weird)). Then I apply ggplot with several additional commands to make it a bit neater:

colours=rev(c("black","red","yellow","green","blue", "blue2"))    #       Colour scheme for plot

p <- ggplot(UCCO2_m, aes(date_dates, variable)) +
  geom_tile(aes(fill = UCCO2_m$value)) +
  scale_fill_gradientn(guide = "colourbar",
                       colours = colours,
                       na.value = "white",
                       expression(Carbon-Concentration[mg/l]))
#    Add some titles and stuff
p <- p + labs(title = "Carbon Concentration below Watertable at the Upslope Station", x = "Month - Year", y = "Depth (cm)")
    #axis.text.x for x axis only
p<-p + theme(axis.line=element_blank(),
             axis.text.x= element_text(angle=45, vjust=0.5),
             panel.grid.major=element_blank(),
             panel.grid.minor=element_blank(),
             panel.border=element_blank(),
             plot.background=element_blank())
#Fix axis
p <- p + scale_x_datetime(breaks = date_breaks("months"), labels = date_format("%b"))
windows()  
plot(p)`

My results

This is how My plots are looking and as anyone can see, the colour scheme represent different values in the two plots: The colours used in both are the same but Notice the scale is different; Frustrating!

Notice that the colour are the same BUT the legend is different.


Question nr two that is just a "bracket"-question if someone knows a quick answer

(Do notice also that my y-axis is terrible uggly! If someone know an easy way to maybe just show every second value I would very much appreciate that also, but I know that is maybe another question. The y-axis values used now are factors and Iäve tried to change them to integer/values but the makes weird horizontal gray strips everywhere in the plot)


Upvotes: 0

Views: 1063

Answers (1)

Marat Talipov
Marat Talipov

Reputation: 13314

You can explicitly set data range of the scale, e.g.:

... + scale_fill_gradientn(...,limits=range(UCCO2_m$value,RCCO2_m$value)) + ...

Upvotes: 0

Related Questions