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


summary , info of problem

i have 2 big datasets (as in matrix-layout keeping them df-s) 100 columns , 11640 rows. each row represent time hourly , each column specific depth. 2 data sets (named ucco2 , rcco2), time same depths differs (i.e. different specific depth). range of ucco2 0 12 , rcco2 0 ~30 please me create 1 legend both of them. manage create plots different legends (notice: i'm having them on separated windows)

presenting data sets

below summary of data after melt() command. i've after commad melt() added dates again (date_dates) , last column seen here.

in plot later: x-axis is: "date_dates" --> hourly measurement time

the y-axis is: "variable" --> depths , can seen in code further down, fill 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 df-s like

an example how matrix looks before command "melt()"

df before melt, rows: 11640, columns: 100 if excluding date columns (note in r date 1 column df before melt, rows: 11640, columns: 100 if excluding date columns (note in r date 1 column

an example of df after melt()

df after, <code>date</code> used factor melt with, <code>variable</code> depth (the column names), <code>value</code> value @ time , depth, <code>date_dates</code> column add after , used x-axis df after, date used factor melt with, variable depth (the column names), value value @ time , depth, date_dates column add after , used x-axis

method - rcode

description of code: i'm using ggplot2 create heatmaps. after melt , step add date column (to use x-axis values (don't know if necessary otherwise x-axis values weird)). apply ggplot several additional commands make bit neater:

colours=rev(c("black","red","yellow","green","blue", "blue2"))    #       colour scheme 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 titles , stuff p <- p + labs(title = "carbon concentration below watertable @ upslope station", x = "month - year", y = "depth (cm)")     #axis.text.x x axis 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 how plots looking , can see, colour scheme represent different values in 2 plots: the colours used in both same notice scale different; frustrating!

notice colour same legend different.


question nr 2 "bracket"-question if knows quick answer

(do notice y-axis terrible uggly! if know easy way maybe show every second value appreciate also, know maybe question. y-axis values used factors , iƤve tried change them integer/values makes weird horizontal gray strips everywhere in plot)


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

... + scale_fill_gradientn(...,limits=range(ucco2_m$value,rcco2_m$value)) + ... 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -