Reputation: 2514
Based on this question, I try to plot color coded maps for multiple years. Consider the following minimal example:
library(ggplot2)
states <- map_data("state")
var <- data.frame(table(states$region))
var$x1 = runif(49, 5.0, 7.5)
var$x2 = 2+runif(49, 5.0, 7.5)
states$x1 <- var$x1[match(states$region,var$Var1)]
states$x2 <- var$x2[match(states$region,var$Var1)]
## first plot
map <- ggplot(states, aes(x=long, y=lat,fill=x1,group=group))+ geom_polygon()
map = map + scale_fill_gradient(low='white', high='grey20')
print(map)
## second plot
map <- ggplot(states, aes(x=long, y=lat,fill=x2,group=group))+ geom_polygon()
map = map + scale_fill_gradient(low='white', high='grey20')
print(map)
The plots work, yet the scale is adjusted to each plot. therefore comparing color across graphs is not meaningful. I would like to have a fixed "number to color" scale across all graphs (I am aware that I might lose some details in my graphs by doing so).
Upvotes: 3
Views: 1157
Reputation: 3438
Add limits to scale_fill_gradient
:
map <- ggplot(states, aes(x=long, y=lat,fill=x1,group=group))+ geom_polygon()
map = map + scale_fill_gradient(low='white', high='grey20', limits=c(1, 10))
print(map)
map <- ggplot(states, aes(x=long, y=lat,fill=x2,group=group))+ geom_polygon()
map = map + scale_fill_gradient(low='white', high='grey20', limits=c(1, 10))
print(map)
Upvotes: 4