R ggplot2热图与日期x轴 - 删除灰色区域

时间:2018-03-01 20:51:17

标签: r ggplot2 heatmap

我正在尝试显示西欧一个家庭用电能耗的热图,在X轴上是Y轴每年的所有日子,每天的小时数,热图显示电力以每小时每小时的千瓦时使用。这是我到目前为止所得到的: Heat map Energy consumption 我的问题是缩放日期X轴,使得图表左侧和右侧的灰色条带消失。 知道如何扩展(),即将数据显示限制为数字数据的最小值,最大值,证明很难找到x轴日期的解决方案。

以下是我用于提供以上情节的内容:

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"))

感激,任何想法。

1 个答案:

答案 0 :(得分:1)

如果您只是想摆脱灰色,请尝试使用themes()这样的内容:

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())

否则,您需要考虑使用coord_cartesian(xlim = c(X, X), ylim = c(X, X))或仅使用xlim(X, X) + ylim(X, X)