Rfanatic
Rfanatic

Reputation: 2290

Heatmap adjustment

I would like to create a heatmap with geom_raster() and to set the color manually (ex. blue and yellow). When doing this I receive an error like 'Error: Continuous value supplied to discrete scale'. Also, how can adjust the x-axis to delete the white space. Finally, is there a way to change the time from H:M:S to H:M and to set the breaks for every 2 hours? Many thanks

Sample code:

ggplot(df, aes(x=Time, y=Date, fill=`Converted kW_Raw_data`)) +  
  geom_raster() + 
  theme_bw()+
  #scale_manual(values=c("blue", "yellow"))+
  labs(x="", y="", title="", fill="") + 
  #facet_wrap(~Date)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1,family="Times", face="bold", size=12, color="black"), 
        axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
        axis.text.y = element_text(family="Times", face="bold", size=12, color="black"),
        axis.title.y = element_text(family="Times", face="bold", size=16, color="black"),
        strip.text = element_text(size=15, face="bold"),
        plot.title = element_text(size=20, face="bold"))+
  theme(legend.title =  element_text(family="Times", face="bold", size=16, color="black"),
        legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
        legend.position="right",
        legend.box = "vertical",
        plot.title = element_text(hjust = 0.5))

Plot:

enter image description here

Sample data:

df<-structure(list(Date = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Sunday", 
    "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday"
    ), class = "factor"), Time = structure(c(66660, 66720, 66780, 
    66840, 66900, 66960, 67020, 67080, 67140, 67200, 67260, 67320, 
    67380, 67440, 67500, 67560, 67620, 67680, 67740, 67800, 67860, 
    67920, 67980, 68040, 68100, 68160, 68220, 68280, 68340, 68400, 
    68460, 68520, 68580, 68640, 68700, 68760, 68820, 68880, 68940, 
    69000, 69060, 69120, 69180, 69240, 69300, 69360, 69420, 69480, 
    69540, 69600, 69660, 69720, 69780, 69840, 69900, 69960, 70020, 
    70080, 70140, 70200, 70260, 70320, 70380, 70440, 70500, 70560, 
    70620, 70680, 70740, 70800, 70860, 70920, 70980, 71040, 71100, 
    71160, 71220, 71280, 71340, 71400, 71460, 71520, 71580, 71640, 
    71700, 71760, 71820, 71880, 71940, 72000, 72060, 72120, 72180, 
    72240, 72300, 72360, 72420, 72480, 72540, 72600), class = c("hms", 
    "difftime"), units = "secs"), `Converted kW_Raw_data` = c(2.415, 
    1.341667, 2.146667, 2.683333, 2.683333, 2.683333, 2.415, 2.951667, 
    2.951667, 2.951667, 2.951667, 1.61, 1.341667, 3.488333, 3.756667, 
    3.22, 2.951667, 2.951667, 2.951667, 2.415, 2.415, 1.61, 1.341667, 
    2.683333, 2.683333, 2.683333, 2.415, 2.415, 2.415, 2.415, 2.415, 
    2.146667, 1.341667, 3.488333, 3.756667, 2.951667, 2.951667, 2.951667, 
    2.951667, 2.951667, 2.951667, 1.341667, 1.341667, 2.683333, 2.683333, 
    2.415, 2.415, 2.415, 2.415, 2.415, 2.415, 1.341667, 1.341667, 
    2.683333, 2.683333, 2.951667, 2.951667, 2.951667, 2.951667, 2.951667, 
    2.951667, 1.341667, 3.488333, 3.756667, 2.951667, 2.951667, 2.951667, 
    2.415, 2.415, 2.415, 1.61, 1.073333, 2.683333, 2.683333, 2.683333, 
    2.683333, 2.415, 2.415, 2.415, 3.488333, 1.878333, 1.341667, 
    2.683333, 3.756667, 3.22, 2.951667, 2.951667, 2.951667, 2.951667, 
    2.951667, 1.878333, 1.878333, 2.683333, 2.683333, 2.683333, 2.415, 
    2.415, 3.22, 3.488333, 3.22), `Converted kW_Raw_data_1` = c("0.002415", 
    "0.001341667", "0.002146667", "0.002683333", "0.002683333", "0.002683333", 
    "0.002415", "0.002951667", "0.002951667", "0.002951667", "0.002951667", 
    "0.00161", "0.001341667", "0.003488333", "0.003756667", "0.00322", 
    "0.002951667", "0.002951667", "0.002951667", "0.002415", "0.002415", 
    "0.00161", "0.001341667", "0.002683333", "0.002683333", "0.002683333", 
    "0.002415", "0.002415", "0.002415", "0.002415", "0.002415", "0.002146667", 
    "0.001341667", "0.003488333", "0.003756667", "0.002951667", "0.002951667", 
    "0.002951667", "0.002951667", "0.002951667", "0.002951667", "0.001341667", 
    "0.001341667", "0.002683333", "0.002683333", "0.002415", "0.002415", 
    "0.002415", "0.002415", "0.002415", "0.002415", "0.001341667", 
    "0.001341667", "0.002683333", "0.002683333", "0.002951667", "0.002951667", 
    "0.002951667", "0.002951667", "0.002951667", "0.002951667", "0.001341667", 
    "0.003488333", "0.003756667", "0.002951667", "0.002951667", "0.002951667", 
    "0.002415", "0.002415", "0.002415", "0.00161", "0.001073333", 
    "0.002683333", "0.002683333", "0.002683333", "0.002683333", "0.002415", 
    "0.002415", "0.002415", "0.003488333", "0.001878333", "0.001341667", 
    "0.002683333", "0.003756667", "0.00322", "0.002951667", "0.002951667", 
    "0.002951667", "0.002951667", "0.002951667", "0.001878333", "0.001878333", 
    "0.002683333", "0.002683333", "0.002683333", "0.002415", "0.002415", 
    "0.00322", "0.003488333", "0.00322")), spec = structure(list(
        cols = list(Date = structure(list(), class = c("collector_character", 
        "collector")), Time = structure(list(format = ""), class = c("collector_time", 
        "collector")), `Converted kW_Raw_data` = structure(list(), class = c("collector_double", 
        "collector")), `Converted kW_Raw_data_1` = structure(list(), class = c("collector_character", 
        "collector"))), default = structure(list(), class = c("collector_guess", 
        "collector")), skip = 1L), class = "col_spec"), row.names = c(NA, 
    100L), class = "data.frame")

Upvotes: 0

Views: 98

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389235

Try this -

  • Set color manually using scale_fill_gradientn.
  • Change time to POSIXct so that you can assign labels and breaks as per choice.
library(ggplot2)

ggplot(transform(df, Time = as.POSIXct(Time)),  
       aes(x=Time, y=Date, fill=`Converted.kW_Raw_data`)) +  
  geom_raster() + 
  theme_bw()+
  scale_fill_gradientn(colours=c("blue", "yellow")) + 
  labs(x="", y="", title="", fill="") + 
  scale_x_datetime(date_labels = '%H:%M', date_breaks = '2 hours') + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1,family="Times", face="bold", size=12, color="black"), 
        axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
        axis.text.y = element_text(family="Times", face="bold", size=12, color="black"),
        axis.title.y = element_text(family="Times", face="bold", size=16, color="black"),
        strip.text = element_text(size=15, face="bold"),
        plot.title = element_text(size=20, face="bold"))+
  theme(legend.title =  element_text(family="Times", face="bold", size=16, color="black"),
        legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
        legend.position="right",
        legend.box = "vertical",
        plot.title = element_text(hjust = 0.5))

Upvotes: 1

Related Questions