为什么我的ggplot geom_tile传奇比我想要的更高?

时间:2017-08-03 18:37:59

标签: r ggplot2 scale gradient facet-grid

所以我为某些数据创建了一张热图,我将其显示按小时和星期几汇总的车辆数量。然而,当我制作地图时,传说似乎不起作用,因此,瓷砖不会达到最暗的红色"。有谁知道发生了什么?谢谢你:

Hourplus <- c(Hour, Hour, Hour, Hour, Hour, Hour, Hour, Hour)
facetgrid <- data.frame(DayofWeek, Totals5, Totals6, Totals3, Totals4, Totals1, Totals2, Totals7, Totals8)
names(facetgrid) <- c("DayofWeek", "Marine Dr NB", "Marine Dr SB", "Carman NB", "Carman SB", "Glisan NB", "Glisan SB", "Stafford NB", "Stafford SB")


facetgrid <- melt(facetgrid)
facetgrid$DayofWeek <- factor(facetgrid$DayofWeek, levels=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
facetgrid
levels(facetgrid$DayofWeek) <- c("M","Tu","W","Th","F","Sa","Su")
gg <- ggplot(facetgrid, aes(x=Hourplus, y=DayofWeek, fill=value)) + geom_tile(color="white", size=0.1) + facet_wrap(~variable, ncol = 2, scales='free')
gg <- gg + coord_equal()
gg <- gg + labs(x=NULL, y=NULL, title ="May 2017: Volume of Vehicles Greater than 20ft in Length")
gg <- gg + theme_tufte()
gg <- gg + theme(plot.title=element_text(hjust=0.5))
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text=element_text(size=7))
gg <- gg + theme(legend.title=element_text(size=8))
gg <- gg + theme(legend.text=element_text(size=6))
gg <- gg + scale_fill_gradient(low = "white",high = "red", name="Volume")
gg <- gg + scale_x_continuous(breaks=seq(0,24,4), limits=c(0,25))
ggsave("8stationsvol.png", height =8, width = 12)

这就是地图现在的样子(没有一块是最暗的红色) Fig

1 个答案:

答案 0 :(得分:0)

Ggplot确实为大多数连续性比例增加了“摆动”空间,与X和Y轴一样。 要设置严格(或自定义)限制,请在scale_fill_gradient中使用“限制”功能。在你的情况下,给它一个具有下限和上限的向量。

<cfset totalWeight = 5.01>
<cfset breakPoints = 5.00>
<cfset bagCost     = 18.00>
<cfset totalWeight = ceiling(totalWeight)>

<cfif totalWeight LTE 5.00 >
    <cfset totalBags = 1 >
<cfelse>
    <cfset totalBags = totalWeight / breakpoints >
</cfif>
<cfset totalCost = totalBags * bagCost>

<cfoutput>
#totalweight#<br/>
#totalBags#
<hr/>
#totalCost#<br/>
<hr/>

</cfoutput>