在地图上独立移动2个图例ggplot2 [英] independently move 2 legends ggplot2 on a map

查看:129
本文介绍了在地图上独立移动2个图例ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是数据:

  ## INST..SUB.TYPE.DESCRIPTION招生经历
## 1 CHARTER SCHOOL 274 42.66439 -73.76993
## 2 PUBLIC学校中心525 42.62502 -74.13756
## 3公立学校中央高中不适用40.67473 -73.69987
## 4公立学校城市328 42.68278 -73.80083
## 5公立学校城市中心288 42.15746 - 78.74158
## 6公立学校通用不适用43.73225 -74.73682
## 7公立学校独立中心284 42.60522 -73.87008
## 8公立学校独立联盟免费337 42.74593 -73.69018
## 9公立学校特别法75 42.14680 -78.98159
## 10公立学校联盟免费256 42.6 8424 -73.73292

我在这篇文章中看到,你可以移动两个独立的传说,但是当我尝试传奇时,不要走到我想要的地方(左上角,如 e1 情节,右中间, e2 情节)。



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



最终的期望输出将与另一个网格图合并,因此我需要能够以某种方式将它指定为grob。我想知道如何实际移动传说,因为其他职位为他们工作,它不能解释发生了什么。



这是代码I '尝试:

  library(ggplot2);库(地图);库(网格);库(gridExtra);库(gtable)
ny < - 子集(map_data(county),%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(color = INST..SUB.TYPE.DESCRIPTION,
size = Enrollment),alpha = .3)+
geom_point( )+
主题(legend.position = c(.2,.81),
legend.key = element_blank(),
legend.background = element_blank())+
(size = FALSE,color = 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(color = INST..SUB .TYPE.DESCRIPTION,
size = Enrollment),alpha = .3)+
geom_point()+
主题(legend.position = c(.88,.5),
legend.key = element_blank(),
传说。 (ggplot_build(e2)),guide-box)
(b)
(e3 < - p3 + geom_point(aes(color = INST..SUB.TYPE.DESCRIPTION,
size = Enrollment),alpha = .3)+
geom_point()+
guides(color = 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(单元(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_c ustom(grob = leg1,xmin = -80,xmax = -76,ymin = 43.7,ymax = 45)





  dat2 < -  
结构(列表(INST..SUB.TYPE.DESCRIPTION = c(CHARTER SCHOOL,
公立学校中心,公立学校中心高中,
公立学校城市,公立学校城市中心 ,公立学校公共学校,
公立学校独立中心,公立学校独立联盟免费,
公立学校特殊法案,公立学校联盟免费),注册= 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)

$ b

期望的输出:

解决方案

视口可以精确定位。在下面的例子中,两个图例被提取出来,然后放置在它们自己的视口内。视口包含在彩色矩形内以显示其位置。另外,我在视口中放置了地图和散点图。正确获取文本大小和点的大小,以便将左上角的图例挤入可用空间,这有点儿小提琴。

  library(ggplot2);库(地图);库(网格);库(gridExtra);库(gtable)
ny < - 子集(map_data(county),%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))$ (颜色= INST..SUB.TYPE.DESCRIPTION,
尺寸=注册),alpha =。 3)+
geom_point()+ theme_gray(9)+
guides(size = FALSE,color = 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)

#获取尺寸图例
(e2< ; - p3 + geom_point(aes(color = INST..SUB.TYPE.DESCRIPTION,
size = Enrollment),alpha = .3)+
geom_point()+ theme_gray(9)+
指南(彩色= gbp_build(e2 = FALSE)+
theme(legend.key = element_blank(),
legend.background = element_blank()))

leg2 < - gtable_filter(ggplot_gtable )),guide-box)

#获取第一个底图 - 地图
(e3 < - p3 + geom_point(aes(color = INST..SUB.TYPE.DESCRIPTION ,
size = Enrollment),alpha = .3)+
geom_point()+
guides(color = FALSE,size = FALSE))

# y轴余量的大小
gt < - ggplot_gtable(ggplot_build(e3))

#获取第二个基本图 - 散点图
plot2 < - ggplot(mtcars ,aes(mpg,hp))+ geom_point()


#png(p.png,600,700,units =px)
grid.newpage ()
#两个视口:地图和散点图
pushViewport(viewport(layout = grid.layout(2,1)))

#先映射
pushViewport( viewport(layout.pos.row = 1))
grid.draw(ggplotGrob(e3))

#position size legend
pushViewport(viewport(x = unit(1, npc) - 单位(1,lines),
y = unit(.5,npc),
w = leg2 $ widths,h = .4,
just = c(right, )
grid.draw(leg2)
grid.rect(gp = gpar(col =red,fill =NA))
popViewport()

#position color legend
pushViewport(viewport(x = sum(gt $ widths [1:3]),
y = unit(1,npc) - unit(1, ()),
w = leg1 $ widths,h = .33,
just = c(left,top)))
grid.draw(leg1)
grid.rect(gp = gpar(col =red,fill =NA))
popViewport(2)
$ b#散点图第二个
pushViewport(viewport(layout。 )
grid.draw(ggplotGrob(plot2))
popViewport()
#dev.off()


I want to independently move two legends on a map to save save and make the presentation nicer.

Here is the data:

##              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

I saw in this post you can move two legends independent but when I try the legends don't go where I want (upper left corner, as in e1 plot, and right middle, as is e2 plot).

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

The final desired output will be merged with another grid plot so I need to be able to assign it as a grob somehow. I'd like to understand how to actually move the legends as the other post worked for them it doesn't explain what's happening.

Here is the code I'm Trying:

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)

## I have also tied:

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 data:

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")

Desired output:

解决方案

Viewports can be positioned with some precision. In the example below, the two legends are extracted then placed within their own viewports. The viewports are contained within coloured rectangles to show their positioning. Also, I placed the map and the scatterplot within viewports. Getting the text size and dot size right so that the upper left legend squeezed into the available space was a bit of a fiddle.

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))

# For getting the size of the y-axis margin
gt <- ggplot_gtable(ggplot_build(e3))  

# Get second base plot - the scatterplot
plot2 <- ggplot(mtcars, aes(mpg, hp)) + geom_point()


# png("p.png", 600, 700, units = "px")
grid.newpage()
# Two viewport: map and scatterplot
pushViewport(viewport(layout = grid.layout(2, 1))) 

# Map first
pushViewport(viewport(layout.pos.row = 1))
grid.draw(ggplotGrob(e3))

# position size legend
pushViewport(viewport(x = unit(1, "npc") - unit(1, "lines"), 
                      y = unit(.5, "npc"), 
                      w = leg2$widths, h = .4,
                      just = c("right", "centre")))
grid.draw(leg2)
grid.rect(gp=gpar(col = "red", fill = "NA"))
popViewport()

# position colour legend
pushViewport(viewport(x = sum(gt$widths[1:3]),
                      y = unit(1, "npc") - unit(1, "lines"), 
                      w = leg1$widths, h = .33,
                      just = c("left", "top")))
grid.draw(leg1)
grid.rect(gp=gpar(col = "red", fill = "NA"))
popViewport(2)

# Scatterplot second
pushViewport(viewport(layout.pos.row = 2))
grid.draw(ggplotGrob(plot2))
popViewport()
# dev.off()

这篇关于在地图上独立移动2个图例ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆