对齐ggplot choropleth的边缘(图例标题各不相同) [英] Align edges of ggplot choropleth (legend title varies)
本文介绍了对齐ggplot choropleth的边缘(图例标题各不相同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用此对齐4 ggplot等值线地图的左右边缘方法。我无法做到这一点。
原始图:
库(GGPLOT2);库(gridExtra)
罪行< - data.frame(state = tolower(rownames(USArrests)),USArrests)
states_map< - map_data(state)
plot1< ggplot(crime,aes(map_id = state))+
geom_map(aes(fill =谋杀),map = states_map)+
expand_limits(x = states_map $ long,y = states_map $ lat)+
scale_fill_gradient(low =white,high =darkgreen,name =Really Long Name 1)
plot2 < - plot1 + scale_fill_gradient(name =Really长名称2然后一些)
plot3< - plot1 + scale_fill_gradient(name =Short 3)
plot4< - plot1 + scale_fill_gradient(name =真正的长名称4)
grid.arrange(plot1,plot3,plot2,plot4,ncol = 2)
试图对齐绘制边缘(相同的结果):
p1 < - ggplotGrob(plot1)
p2 < - ggplotGrob(plot2)
p3 < - ggplotGrob(plot3)
p4 < - ggplotGrob(plot4)
maxWidth < - grid: :unit.pmax(P1 $宽度[2:3], p2 $ widths [2:3],p3 $ widths [2:3],p4 $ widths [2:3])
p1 $ widths [2:3]< - as.list(maxWidth)
p2 $ widths [2:3]< - as.list(maxWidth)
p3 $ widths [2:3]< - as.list(maxWidth)
p4 $ widths [2: 3]< - as.list(maxWidth)
grid.arrange(p1,p3,p2,p4,ncol = 2)
PS:假设我需要使用网格排列,并且图例实际上并不是相同的比例,所以 facet_grid
不在等。
解决方案
下面是一个例子:
库(gtable)
grid.draw(cbind(rbind(p1,p2,size =last),rbind(p3,p4,size =last),size =first))
更新
这是一个不好的破解,所以我不建议使用。
可能这在将来不会起作用。
gt < - cbind(rbind(p1,p2,size = $ last),rbind(p3,p4,size =last),size =first)
for(其中(gt $ layout $ name ==guide-box)){
gt $ grobs [[i]]< - gt $ grobs [[i]] $ grobs [[1]]
}
grid.draw(gt)
I am attempting to align the left and right edges of 4 ggplot choropleth maps using this method. I am unable to do this though.
Original plot:
library(ggplot2); library(gridExtra)
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
states_map <- map_data("state")
plot1 <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat) +
scale_fill_gradient(low="white", high="darkgreen", name="Really Long Name 1")
plot2 <- plot1 + scale_fill_gradient(name="Really Long Name 2 and then some")
plot3 <- plot1 + scale_fill_gradient(name="Short 3")
plot4 <- plot1 + scale_fill_gradient(name="Really Long Name 4")
grid.arrange(plot1, plot3, plot2, plot4, ncol = 2)
Attempt to align plot edges (same result):
p1 <- ggplotGrob(plot1)
p2 <- ggplotGrob(plot2)
p3 <- ggplotGrob(plot3)
p4 <- ggplotGrob(plot4)
maxWidth <- grid::unit.pmax(p1$widths[2:3], p2$widths[2:3], p3$widths[2:3], p4$widths[2:3])
p1$widths[2:3] <- as.list(maxWidth)
p2$widths[2:3] <- as.list(maxWidth)
p3$widths[2:3] <- as.list(maxWidth)
p4$widths[2:3] <- as.list(maxWidth)
grid.arrange(p1, p3, p2, p4, ncol = 2)
PS: Assume I need to use grid arrange and the legends are not actually the same scale so facet_grid
is out etc.
解决方案
Here is an example:
library(gtable)
grid.draw(cbind(rbind(p1, p2, size="last"), rbind(p3, p4, size="last"), size = "first"))
Updated
This is a bad hack so I don't recommend to use. Probably this will not work in future.
gt <- cbind(rbind(p1, p2, size="last"), rbind(p3, p4, size="last"), size = "first")
for (i in which(gt$layout$name == "guide-box")) {
gt$grobs[[i]] <- gt$grobs[[i]]$grobs[[1]]
}
grid.draw(gt)
这篇关于对齐ggplot choropleth的边缘(图例标题各不相同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文