【问题标题】:Fix ggplot fill scale across multiple plots修复 ggplot 跨多个绘图的填充比例
【发布时间】:2019-06-11 08:31:38
【问题描述】:

基于这个question,我尝试绘制多年的彩色编码地图。考虑以下最小示例:

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)

情节有效,但规模已针对每个情节进行了调整。因此,跨图表比较颜色没有意义。我想在所有图表中都有一个固定的“数字到颜色”比例(我知道这样做可能会丢失图表中的一些细节)。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    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)
    

    【讨论】:

    • 我似乎在帮助页面中错过了这个选项!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    相关资源
    最近更新 更多