PeetsB
PeetsB

Reputation: 63

R ggplot2 heat map with date x-axis - remove grey areas

I am trying to display a heat map for electrical energy consumption of a tyoical household in Western Europe, on the X-axis are all days of a year on the Y-axis the hours per day, the heat map shows the electricity used in kWh per hour per day. Here's what I got so far: Heat map Energy consumption My problem is to scale the date X-axis such that the grey ribbons on the left and right of the plot are gone. Knowing how to expand(), i.e. confine data displays to min, max for numeric data, it proves difficult to find a solution for x-axis date.

Here's what I use to deliver above plot:

hm.palette <- colorRampPalette(rev(brewer.pal(11, 'Spectral')), space='Lab')
heatmap_print <- ggplot(MyData, aes(y=Time, x=Day, fill=Totaal)) + 
geom_tile(aes(height=1)) +
scale_fill_gradientn(colours = hm.palette(100)) 
print(heatmap_print + 
    scale_y_continuous(name = "Hour of day", breaks = seq(0, 23, by=1), expand = c(0, 0)) + 
    scale_x_date(name = "Month of year", 
                 breaks = seq(as.Date("2009-01-15"), 
                              as.Date("2009-12-15"), 
                              by = "1 month"), date_labels ="%B"))

Grateful, for any ideas.

Upvotes: 1

Views: 2050

Answers (1)

Harrison B. Pugh
Harrison B. Pugh

Reputation: 158

If you just want to get rid of the grey, try something with themes() like this:

hm.palette <- colorRampPalette(rev(brewer.pal(11, 'Spectral')), space='Lab')
heatmap_print <- ggplot(MyData, aes(y=Time, x=Day, fill=Totaal)) + 
geom_tile(aes(height=1)) +
scale_fill_gradientn(colours = hm.palette(100)) 
print(heatmap_print + 
    scale_y_continuous(name = "Hour of day", breaks = seq(0, 23, by=1), expand = c(0, 0)) + 
    scale_x_date(name = "Month of year", 
                 breaks = seq(as.Date("2009-01-15"), 
                              as.Date("2009-12-15"), 
                              by = "1 month"), date_labels ="%B")) +
    theme(panel.background = element_blank(),
          plot.background = element_blank())

Otherwise, you'll need to look into using either coord_cartesian(xlim = c(X, X), ylim = c(X, X)) or just plain xlim(X, X) + ylim(X, X)

Upvotes: 2

Related Questions