Place 1 heatmap on another with transparency in R

后端 未结 1 796
说谎
说谎 2020-12-11 13:56

I\'m new to R and have the following challenge;

I want to create a visualization that basically combines 2 kind of \'heatmaps\' in order to visualize at what times

相关标签:
1条回答
  • 2020-12-11 14:43

    So from what I understood, there are basically two questions:

    Data organization

    The easiest would be, if you'd have all data in one data.frame in long format. I.e. for each combination of time and date you have one row, with additional columns for the moon and sun intensity.

    So we start with melting and fixing the moon data:

    library(reshape2)
    moon$time <- row.names(moon)
    moon <- melt(moon, id.vars="time", variable.name="date", value.name="moon" )
    moon$date <- sub("X(.*)", "\\1", moon$date)
    moon$moon <- 1 - as.numeric(sub("%", "", moon$moon)) /100
    

    Now we bring the sun data to an comparable form, by at least give them the same identifier for the date:

    sun$Day <- paste( sun$Day, "9.12", sep  ="." )
    

    Next step is to merge the data by the date resp. Day and to set a comparable column for the sun intensity as is given already for the moon intensity. This can be done by casting the times to a time format and compare Sunrise and Sunset with the actual time:

    mdf <- merge( moon, sun, by.x = "date", by.y = "Day" )
    mdf$time.tmp <- strptime(mdf$time, format="%H:%M")
    mdf$Sunrise  <- round(strptime(mdf$Sunrise, format="%H:%M"), units = "hours")
    mdf$Sunset   <- round(strptime(mdf$Sunset, format="%H:%M"), units = "hours")
    mdf$sun <- ifelse( mdf$Sunrise <= mdf$time.tmp & mdf$Sunset >= mdf$time.tmp, 1, 0 )
    mdf <- mdf[c("date", "time", "moon", "sun")]
    
    mdf[ 5:10, ]
      date    time moon sun
    1.9.12 4:00:00    0   0
    1.9.12 5:00:00    0   0
    1.9.12 6:00:00    0   0
    1.9.12 7:00:00    0   1
    1.9.12 8:00:00    1   1
    1.9.12 9:00:00    1   1
    

    Plotting

    Adding multiple layers with different transparencies begs literally for ggplot2. In order to use this in a proper way, there is one more data manipulation necessary, which ensures the proper order on the axes: date and time have to be converted to factors with factor levels ordered not lexically, but by time:

    mdf <- within( mdf, {
      date <- factor( date, levels=unique(date)[ order(as.Date( unique(date), "%d.%m.%y" ) ) ] )
      time <- factor( time,  levels=unique(time)[ order(strptime( time, format="%H:%M:%S"), decreasing=TRUE ) ] )
    } )
    

    This can be plot now:

    library( ggplot2 )
    ggplot( data = mdf, aes(x = date, y = time )  ) + 
      geom_tile( aes( alpha = sun  ), fill = "goldenrod1"  ) +
      geom_tile( aes( alpha = moon ), fill = "dodgerblue3" ) +
      scale_alpha_continuous( "moon", range=c(0,0.5) ) +
      theme_bw() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
    

    Which gives you the following result

    enter image description here

    0 讨论(0)
提交回复
热议问题