简体   繁体   English

ggmap,使用coord_cartesian将所有点推向北方

[英]ggmap, using coord_cartesian pushes all points to the north

As the title says when I add coord_cartesian to my ggmap it moves all of my points up. 就像标题所说的,当我在coord_cartesian中添加coord_cartesian ,它会将所有点向上移动。 Here's some data. 这是一些数据。

pricedata<-structure(list(nodename = c("CIN.WABRIVR.2", "CIN.WHEATCTG1", 
                                       "CONS.ADA", "CONS.ALCONA", "CONS.CADILAC", "CONS.CROTON", "CONS.GAYLORD1", 
                                       "CONS.GRATIOT1", "CONS.GRAYLGY2", "CONS.GRAYLNG", "CONS.HARDY", 
                                       "CONS.HILLMAN", "CONS.HODENPYL", "CONS.HOLL", "CONS.KALK", "CONS.KARN1", 
                                       "CONS.KENCNTY1", "CONS.LANS", "CONS.LUDINGTN1", "CONS.MIPOWER1", 
                                       "CONS.RENAIGEN1", "CONS.STRAITS", "CONS.TUSCOLA1", "CONS.VKLINCOLN", 
                                       "CONS.VKMCBAIN1", "CONS.ZEELAND1A"), 
                          lat = c(39.922328, 39.53, 42.962672, 44.561961, 44.26169, 43.437322, 45.0306, 43.433889, 
                                  43.408056, 44.604921, 43.486618, 45.0688, 44.36286, 42.7925, 44.6889, 43.644996, 
                                  42.949575, 42.719722, 43.8942, 43.9375, 43.1864, 45.766859, 43.525278, 44.68, 44.204, 42.8067), 
                          lon = c(-87.446358, -87.4247, -85.494071, -83.804505, -85.435224, -85.664462, -84.7039, -84.4975, -84.462222, 
                                  -84.690578, -85.629866, -83.8932, -85.819968, -86.092222, -85.2019, -83.840074, -85.693209, -84.551667, 
                                  -86.4447, -86.425, -84.8429, -84.756601, -83.65, -83.4167, -85.2206, -86.0558), 
                          price = c(30.3, 32.08, 36.71, 35.78, 36.12, 36.33, 35.58, 35.16, 36.12, 36.12, 35.9, 35.8, 36.05, 36.38, 
                                    35.98, 23.18, 36.06, 34.55, 34.87, 34.6, 34.6, 38.49, 34.23, 35.64, 35.43, 36.33), 
                          pricecut = structure(c(7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
                                                 8L, 8L, 5L, 8L, 8L, 8L, 8L, 8L, 9L, 8L, 8L, 8L, 8L), 
                         .Label = c("(-10,0]", "(0,6]", "(6,14]", "(14,20]", "(20,26]", "(26,30]", "(30,34]", 
                                    "(34,38]", "(38,42]", "(42,46]", "(46,50]", "(50,56]", "(56,62]", "(62,68]", 
                                    "(68,76]", "(76,82]", "(82,90]", "(90,100]", "(100,115]", "(115,125]", "(125,150]", 
                                    "(150,200]", "(200,250]", "(250,300]", "(300,400]", "(400,500]", 
                                    "(500,600]", "(600,800]", "(800,1e+03]"), 
                         class = c("ordered", "factor"))), .Names = c("nodename", "lat", "lon", "price", "pricecut"), row.names = 75:100, class = "data.frame")

Here's my code plus the result 这是我的代码加上结果

m<-get_map(location=c(lon=-89.6,lat=41.8),zoom=5)
base<-ggmap(m,extent='device') 
base+geom_point(aes(x=lon,y=lat, colour=pricecut), size=6, alpha=.7, data=pricedata)

在此处输入图片说明

That is the result I expect 那是我期望的结果

However, when I add coord_cartesian things get strange 但是,当我添加coord_cartesian事情变得很奇怪

base+geom_point(aes(x=lon,y=lat, colour=pricecut), size=6, alpha=.7, data=pricedata)+coord_cartesian(xlim=c(-95,-80), ylim=c(38,50))

在此处输入图片说明

Instead of using coord_cartesian you can probably better set the limits with scale_x_continuous and scale_y_continuous as follows: 代替使用coord_cartesian您可以更好地使用scale_x_continuousscale_y_continuous设置限制,如下所示:

ggmap(m) +
  geom_point(aes(x=lon,y=lat, colour=pricecut), size=6, alpha=.7, data=pricedata) +
  scale_x_continuous(limits = c(-95, -80), expand = c(0, 0)) +
  scale_y_continuous(limits = c(38, 50), expand = c(0, 0))

which gives the following map: 给出以下地图:

在此处输入图片说明

Note : I omitted extent='device' from the ggmap call so you can see what the boundaries are in this plot. 注意 :我从ggmap调用中省略了ggmap extent='device' ,因此您可以看到此图中的边界。


As with regard to the effect of using coord_cartesian , it seems that coord_cartesian somehow messes with the ratios of the map. 至于使用coord_cartesian的效果,似乎coord_cartesian某种程度上与地图的比例coord_cartesian了。 Let's start with just the map: 让我们从地图开始:

ggmap(m)

gives: 得到:

在此处输入图片说明

When you slice this map with scale_y_continuous : 当您使用scale_y_continuous对该地图进行scale_y_continuous

ggmap(m) +
  geom_blank() +
  scale_y_continuous(limits = c(38, 50), expand = c(0, 0))

you get: 你得到:

在此处输入图片说明

However when doing a similar slice with coord_cartesian : 但是,当使用coord_cartesian进行类似切片时:

ggmap(m) +
  geom_blank() +
  coord_cartesian(ylim=c(38,50))

you get: 你得到:

在此处输入图片说明

As you can see, the map gets stretched horizontally while at the same time maintaining the same height. 如您所见,地图在保持相同高度的同时被水平拉伸。 This causes the map to shift vertically. 这将导致地图垂直移动。 When using scale_y_continuous the map keeps the correct ratio. 使用scale_y_continuous ,地图会保持正确的比率。 It's therefore not the points that get shifted upwards, but the map that gets shifted downwards. 因此,不是点向上偏移,而是地图向下偏移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM