R 在保持高度不变的情况下排列多个对象

R 在保持高度不变的情况下排列多个对象,r,ggplot2,R,Ggplot2,我正在尝试将美国地图拆分为多个窗口(其中一些窗口包含两次相同的州)。我希望比例是恒定的(这样贴图就不会扭曲),但也可以最小化贴图之间的空间。我不能使用facet_wrap(由于区域的重叠性质——而且无论如何,facet_wrap不能同时具有固定的比例和每个窗口具有不同的xlim)。关于如何改进结果的间距,有什么建议吗 require(data.table) require(ggplot2) require(maps) require(gridExtra) all_states <- as.

我正在尝试将美国地图拆分为多个窗口(其中一些窗口包含两次相同的州)。我希望比例是恒定的(这样贴图就不会扭曲),但也可以最小化贴图之间的空间。我不能使用facet_wrap(由于区域的重叠性质——而且无论如何,facet_wrap不能同时具有固定的比例和每个窗口具有不同的xlim)。关于如何改进结果的间距,有什么建议吗

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))
require(data.table)
需要(ggplot2)
需要(地图)
需要(额外)

所有州让我们澄清几件事

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


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

  • 固定纵横比通过相对空网格单元编码在GTTable中,您可以随时检查



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

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

这里有一个解决方案,使用
facet\u grid()
和一些模糊的设置
主题(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)

您可以将
widths=unit(c(1,2,3),“null”)
传递给
arrangeGrob
,并手动调整相关因子。这并不能解决arrangeGrob在第一个图形周围填充空白的问题。我不知道这是如何解决这个问题的。如果可以的话,你能演示一下怎么做吗?我提供了完全可用的示例代码…它以扭曲贴图为代价减少了空白。我想要一个解决方案,它可以在每个图(x:y=1:1)和图之间(每个图上一个单位的长度在屏幕上是相同的空间)保持地图的比例。这几乎是完美的——我想要一些状态在多个区域中,但这很容易通过复制和RBIND来实现(例如,华盛顿有两排,一排在weco,一排在west)。非常感谢!我真的开始相信这是不可能的。延伸的州问题只是因为地图没有使用投影坐标,这不是什么大问题。
+coord_map(“lambert”,lat0=25,lat1=50)
投影地图(在本例中,输入兰伯特)
g = ggplotGrob(p.weco)
g[["heights"]][3] # 2.37008405379269 is the aspect ratio for that plot panel
# 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)