gpt4 book ai didi

r - 在 map 上独立移动 2 个图例 ggplot2

转载 作者:行者123 更新时间:2023-12-03 20:23:00 25 4
gpt4 key购买 nike

我想在 map 上独立移动两个图例以保存保存并使演示文稿更好。

这是数据:

##              INST..SUB.TYPE.DESCRIPTION Enrollment      lat       lng
## 1 CHARTER SCHOOL 274 42.66439 -73.76993
## 2 PUBLIC SCHOOL CENTRAL 525 42.62502 -74.13756
## 3 PUBLIC SCHOOL CENTRAL HIGH SCHOOL NA 40.67473 -73.69987
## 4 PUBLIC SCHOOL CITY 328 42.68278 -73.80083
## 5 PUBLIC SCHOOL CITY CENTRAL 288 42.15746 -78.74158
## 6 PUBLIC SCHOOL COMMON NA 43.73225 -74.73682
## 7 PUBLIC SCHOOL INDEPENDENT CENTRAL 284 42.60522 -73.87008
## 8 PUBLIC SCHOOL INDEPENDENT UNION FREE 337 42.74593 -73.69018
## 9 PUBLIC SCHOOL SPECIAL ACT 75 42.14680 -78.98159
## 10 PUBLIC SCHOOL UNION FREE 256 42.68424 -73.73292

我在这篇文章中看到,您可以独立移动两个图例,但是当我尝试时,图例不会移动到我想要的位置(左上角,如 e1 情节,以及右中间,如 e2 情节)。

https://stackoverflow.com/a/13327793/1000343

最终所需的输出将与另一个网格图合并,因此我需要能够以某种方式将其分配为 grob。我想了解如何实际移动传说,因为另一篇文章为他们工作,它没有解释发生了什么。

这是我正在尝试的代码:
library(ggplot2); library(maps); library(grid); library(gridExtra); library(gtable)
ny <- subset(map_data("county"), region %in% c("new york"))
ny$region <- ny$subregion

p3 <- ggplot(dat2, aes(x=lng, y=lat)) +
geom_polygon(data=ny, aes(x=long, y=lat, group = group))

(e1 <- p3 + geom_point(aes(colour=INST..SUB.TYPE.DESCRIPTION,
size = Enrollment), alpha = .3) +
geom_point() +
theme(legend.position = c( .2, .81),
legend.key = element_blank(),
legend.background = element_blank()) +
guides(size=FALSE, colour = guide_legend(title=NULL,
override.aes = list(alpha = 1, size=5))))


leg1 <- gtable_filter(ggplot_gtable(ggplot_build(e1)), "guide-box")



(e2 <- p3 + geom_point(aes(colour=INST..SUB.TYPE.DESCRIPTION,
size = Enrollment), alpha = .3) +
geom_point() +
theme(legend.position = c( .88, .5),
legend.key = element_blank(),
legend.background = element_blank()) +
guides(colour=FALSE))

leg2 <- gtable_filter(ggplot_gtable(ggplot_build(e2)), "guide-box")

(e3 <- p3 + geom_point(aes(colour=INST..SUB.TYPE.DESCRIPTION,
size = Enrollment), alpha = .3) +
geom_point() +
guides(colour=FALSE, size=FALSE))



plotNew <- arrangeGrob(leg1, e3,
heights = unit.c(leg1$height, unit(1, "npc") - leg1$height), ncol = 1)

plotNew <- arrangeGrob(plotNew, leg2,
widths = unit.c(unit(1, "npc") - leg2$width, leg2$width), nrow = 1)

grid.newpage()
plot1 <- grid.draw(plotNew)


plot2 <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
grid.arrange(plot1, plot2)

##我也绑了:
e3 + 
annotation_custom(grob = leg2, xmin = -74, xmax = -72.5, ymin = 41, ymax = 42.5) +
annotation_custom(grob = leg1, xmin = -80, xmax = -76, ymin = 43.7, ymax = 45)

## dput 数据:
dat2 <- 
structure(list(INST..SUB.TYPE.DESCRIPTION = c("CHARTER SCHOOL",
"PUBLIC SCHOOL CENTRAL", "PUBLIC SCHOOL CENTRAL HIGH SCHOOL",
"PUBLIC SCHOOL CITY", "PUBLIC SCHOOL CITY CENTRAL", "PUBLIC SCHOOL COMMON",
"PUBLIC SCHOOL INDEPENDENT CENTRAL", "PUBLIC SCHOOL INDEPENDENT UNION FREE",
"PUBLIC SCHOOL SPECIAL ACT", "PUBLIC SCHOOL UNION FREE"), Enrollment = c(274,
525, NA, 328, 288, NA, 284, 337, 75, 256), lat = c(42.6643890904276,
42.6250153712452, 40.6747307730359, 42.6827826714356, 42.1574638634531,
43.732253, 42.60522, 42.7459287878497, 42.146804, 42.6842408825698
), lng = c(-73.769926191186, -74.1375573966339, -73.6998654715486,
-73.800826733851, -78.7415828275227, -74.73682, -73.87008, -73.6901801893874,
-78.981588, -73.7329216476674)), .Names = c("INST..SUB.TYPE.DESCRIPTION",
"Enrollment", "lat", "lng"), row.names = c(NA, -10L), class = "data.frame")

所需输出:

enter image description here

最佳答案

顺便说一句,可以使用多个 annotation_custom :

library(ggplot2); library(maps); library(grid); library(gridExtra); library(gtable)
ny <- subset(map_data("county"), region %in% c("new york"))
ny$region <- ny$subregion

p3 <- ggplot(dat2, aes(x = lng, y = lat)) +
geom_polygon(data=ny, aes(x = long, y = lat, group = group))

# Get the colour legend
(e1 <- p3 + geom_point(aes(colour = INST..SUB.TYPE.DESCRIPTION,
size = Enrollment), alpha = .3) +
geom_point() + theme_gray(9) +
guides(size = FALSE, colour = guide_legend(title = NULL,
override.aes = list(alpha = 1, size = 3))) +
theme(legend.key.size = unit(.35, "cm"),
legend.key = element_blank(),
legend.background = element_blank()))

leg1 <- gtable_filter(ggplot_gtable(ggplot_build(e1)), "guide-box")

# Get the size legend
(e2 <- p3 + geom_point(aes(colour=INST..SUB.TYPE.DESCRIPTION,
size = Enrollment), alpha = .3) +
geom_point() + theme_gray(9) +
guides(colour = FALSE) +
theme(legend.key = element_blank(),
legend.background = element_blank()))

leg2 <- gtable_filter(ggplot_gtable(ggplot_build(e2)), "guide-box")

# Get first base plot - the map
(e3 <- p3 + geom_point(aes(colour = INST..SUB.TYPE.DESCRIPTION,
size = Enrollment), alpha = .3) +
geom_point() +
guides(colour = FALSE, size = FALSE))


leg2Grob <- grobTree(leg2)
leg3Grob <- grobTree(leg2)
leg4Grob <- grobTree(leg2)
leg5Grob <- grobTree(leg2)
leg1Grob <- grobTree(leg1)

p = e3 +
annotation_custom(leg2Grob, xmin=-73.5, xmax=Inf, ymin=41, ymax=43) +
annotation_custom(leg1Grob, xmin=-Inf, xmax=-76.5, ymin=43.5, ymax=Inf) +
annotation_custom(leg3Grob, xmin = -Inf, xmax = -79, ymin = -Inf, ymax = 41.5) +
annotation_custom(leg4Grob, xmin = -78, xmax = -76, ymin = 40.5, ymax = 42) +
annotation_custom(leg5Grob, xmin=-73.5, xmax=-72, ymin=43.5, ymax=Inf)
p

enter image description here

关于r - 在 map 上独立移动 2 个图例 ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21798364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com