基于此question ,多年来我一直在尝试绘制彩色编码 map 。考虑以下最小示例:
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)
绘图有效,但比例会根据每个绘图进行调整。因此,跨图表比较颜色没有意义。我想在所有图形中都有一个固定的“颜色数字”比例(我知道这样做可能会丢失图形中的一些细节)。
最佳答案
为 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)
关于r - 修复多个绘图中的 ggplot 填充比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56539558/