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

Dea*_*gor 2 r ggplot2 ggmap

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

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")
Run Code Online (Sandbox Code Playgroud)

这是我的代码加上结果

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这是我期望的结果

但是,当我添加的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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Jaa*_*aap 6

而不是使用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))
Run Code Online (Sandbox Code Playgroud)

这给出了以下地图:

在此输入图像描述

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


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

ggmap(m)
Run Code Online (Sandbox Code Playgroud)

得到:

在此输入图像描述

使用以下方法切割此地图时scale_y_continuous:

ggmap(m) +
  geom_blank() +
  scale_y_continuous(limits = c(38, 50), expand = c(0, 0))
Run Code Online (Sandbox Code Playgroud)

你得到:

在此输入图像描述

但是,在执行类似切片时coord_cartesian:

ggmap(m) +
  geom_blank() +
  coord_cartesian(ylim=c(38,50))
Run Code Online (Sandbox Code Playgroud)

你得到:

在此输入图像描述

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