【问题标题】:Arranging ggplot multiple objects maintaining while constant height安排ggplot多个对象保持不变的高度
【发布时间】:2013-07-31 21:36:48
【问题描述】:

我正在尝试将美国地图拆分为多个窗口(其中一些窗口包含两次相同的州)。我希望比例保持不变(这样地图不会扭曲),但也要尽量减少地图之间的空间。我不能使用 facet_wrap (由于区域的重叠性质——无论如何, facet_wrap 不能有固定的比例并且每个窗口都有不同的 xlims)。关于如何改进结果间距的任何建议?

require(data.table)
require(ggplot2)
require(maps)
require(gridExtra)
all_states <- as.data.table(map_data("state"))

setnames(all_states,"region","state")

##define regions with overlapping states
weco.states <- c("oregon","washington","california")
west.states <- c("washington","montana", "idaho","utah","nevada","arizona","new mexico",
    "wyoming","colorado","south dakota","texas")
east.states <- c(setdiff(unique(all_states$state), union(weco.states,west.states)),
    "texas","south dakota")
all_states[,c("weco","west","east"):=FALSE]
all_states[state%in% weco.states, weco:=TRUE]
all_states[state%in% west.states, west:=TRUE]
all_states[state%in% east.states, east:=TRUE]


p.regbase <- ggplot() + coord_equal() +ylim(c(25,50))
p.weco <- p.regbase + geom_polygon(data=all_states[(weco),], aes(x=long, y=lat, group = group),colour="white", fill="grey" ) 
p.west <- p.regbase + geom_polygon(data=all_states[(west),], aes(x=long, y=lat, group = group),colour="white", fill="grey" )  
p.east <- p.regbase + geom_polygon(data=all_states[(east),], aes(x=long, y=lat, group = group),colour="white", fill="grey" )  

print(arrangeGrob(p.weco,p.west,p.east,ncol=3,nrow=1))

根据我在 Windows GUI 中调整图形窗口大小的方式,结果要么不好(比例不同)

或体面(相同高度)但空间太大:我怎样才能摆脱多余的空间?

【问题讨论】:

  • 您可以将widths = unit(c(1, 2, 3), "null") 传递给arrangeGrob,并手动调整相关因素。
  • 这并没有解决arrangeGrob在第一张图周围填充空白的事实。
  • 我没有看到这是如何解决这个问题的。如果可以,你能演示一下吗?我提供了完全可用的示例代码...
  • 它以扭曲地图为代价减少空白。我想要一个解决方案,可以保持每个地块内 (x:y=1:1) 和地块之间的地图比例(每张地图上的一个单位长是屏幕上的相同空间)。

标签: r ggplot2


【解决方案1】:

这是一个使用facet_grid() 和有点晦涩的设置theme(aspect.ratio=1) 的解决方案。情节并不完美,但我希望它能给你大部分你需要的东西。请注意,这些状态看起来比它们应该的要宽一些。显然 1 度纬度与美国 1 度经度的距离明显不同。

# Create a new factor column for faceting.
newfactor = ifelse(all_states$weco, "weco", 
                           ifelse(all_states$west, "west", "east"))

# Manually specify sort order of factor levels.
newfactor = factor(as.character(newfactor), levels=c("weco", "west", "east"))

all_states$region = newfactor

plot_1 = ggplot(all_states, aes(x=long, y=lat, group=group)) + 
         geom_polygon(colour="white", fill="grey") + 
         facet_grid(. ~ region, space="free", scales="free") + 
         theme(aspect.ratio=1)

ggsave("plot_1.png", plot=plot_1, height=4, width=8, dpi=150)

【讨论】:

  • 这几乎是完美的——我希望某些州位于多个区域中,但这很容易通过复制和绑定它们来完成(例如,有两组华盛顿行——一个在韦科,一个在西部)。万分感谢!我真的开始相信这是不可能的。延伸状态问题只是因为地图没有使用投影坐标,这没什么大不了的。
  • + coord_map("lambert", lat0=25, lat1=50) 投影地图(在本例中为兰伯特)
【解决方案2】:

让我们澄清一些事情。

  • grid.arrange 不会“添加填充”,它只是将 grobs 并排放置在网格布局中。您可以更改行中每个单元格的宽度,

grid.newpage()
pushViewport(viewport(width=5, height=2.5, default.units="in")); 
gt = grobTree(rectGrob(), circleGrob()) 
grid.arrange(gt, gt, gt, nrow=1, newpage=FALSE,
             widths = unit(c(1, 2, 3), "null"))
upViewport()

  • 当图没有固定的纵横比时,对齐图很容易,例如使用 gtable:::cbind_gtable

  • 固定纵横比通过相对空网格单元在 gtable 中进行编码,您可以轻松检查,


g = ggplotGrob(p.weco)
g[["heights"]][3] # 2.37008405379269 is the aspect ratio for that plot panel

  • 设置宽度在固定纵横比 ggplot2 下效果不佳的原因有两个:i) 很难猜测我们应该分配什么相对宽度,因为这取决于根据数据范围; ii) 设备的宽度和高度也应该根据三个纵横比设置,以避免额外的空白(保持正确的纵横比所必需的)

这些问题have been discussed here;我不知道一个优雅而通用的解决方案。

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 2017-02-02
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多