ggmap,使用coord_cartesian将所有点推到北

时间:2015-10-22 05:28:37

标签: r ggplot2 ggmap

正如标题所说,当我向我的ggmap添加coord_cartesian时,它会移动我的所有点。这是一些数据。

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

这是我的代码加上结果

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)

enter image description here

这是我期望的结果

然而,当我添加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))

enter image description here

1 个答案:

答案 0 :(得分:6)

您可以更好地使用coord_cartesianscale_x_continuous设置限制,而不是使用scale_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))

给出了以下地图:

enter image description here

注意:我从extent='device'调用中省略了ggmap,因此您可以看到此图中的边界。

关于使用coord_cartesian的效果,似乎coord_cartesian以某种方式混淆了地图的比例。让我们从地图开始:

ggmap(m)

给出:

enter image description here

使用scale_y_continuous切割此地图时:

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

你得到:

enter image description here

然而,当使用coord_cartesian执行类似切片时:

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

你得到:

enter image description here

如您所见,地图水平拉伸,同时保持相同的高度。这会导致地图垂直移动。使用scale_y_continuous时,地图会保持正确的比例。因此,不是向上移动的点,而是向下移动的地图。