0
我有一個奇怪的情況,當我試圖繪製我可以找到的數據集上的熱圖here。HeatMap無法正確顯示使用ggplot()
我使用下面的代碼繪製熱圖:
xaxis<-c('density')
midrange<-range(red[,xaxis])
xaxis <- c(xaxis,'quality')
molten<-melt(red[,xaxis],'quality')
p <- ggplot(molten, aes(x = value, y = quality))
p <- p + geom_tile(aes(fill = value), colour = "white")
p <- p + theme_minimal()
# turn y-axis text 90 degrees (optional, saves space)
p <- p + theme(axis.text.y = element_text(angle = 90, hjust = 0.5))
# remove axis titles, tick marks, and grid
p <- p + theme(axis.title = element_blank())
p <- p + theme(axis.ticks = element_blank())
p <- p + theme(panel.grid = element_blank())
p <- p + scale_y_discrete(expand = c(0, 0))
# optionally remove row labels (not useful depending on molten)
p <- p + theme(axis.text.x = element_blank())
# get diverging color scale from colorbrewer
# #008837 is green, #7b3294 is purple
palette <- c("#008837", "#b7f7f4", "#b7f7f4", "#7b3294")
if(midrange[1] == midrange[2]) {
# use a 3 color gradient instead
p <- p + scale_fill_gradient2(low = palette[1], mid = palette[2], high = palette[4], midpoint = midrange[1]) +
xlim(midrange[1],midrange[2])
}else{
# use a 4 color gradient (with a swath of white in the middle)
p <- p + scale_fill_gradientn(colours = palette, values = c(0, midrange[1], midrange[2], 1)) +
xlim(midrange[1],midrange[2])
}
p
我試圖繪製的變量Density
的熱圖,並想用變量quality
的分離,我的熱圖。當我使用上面的代碼,我得到以下情節:
它可以清楚地看到,這是一個空白圖像。發生這種情況是因爲變量Density
的範圍非常低,如果將變量更改爲具有更寬範圍的變量(例如,pH
),則不會發生此情況。 ggplot應該自動調整到這個?如果沒有,我怎樣才能讓ggplot顯示真實的情節?
在這方面的任何幫助將不勝感激。