问题描述
正如标题所说,当我将 coord_cartesian
添加到我的ggmap时,它会将我所有的点移动。这里有一些数据。
$ pre $ pricingata 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,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8B,8L,8L,8L,8L,8L,8L, ( - 10,0],(0,6],(6,14,8L, ],(14,20),(20,26),(26,30),(30,34),
(34,38),(38 ,(42.66),(46,50),(50,56),(56,62),(62,68),
(68,76),(76,82),(82,90),(90,100),(100,115),(115,125),(125,150),$ b $ (150,200),(200,250),(250,300),(300,400),(400,500),
(500,600),(600,800), ,1e + 03]),
class = c(ordered,factor))),.Names = c(nodename,lat,lon,price,pricecut ),row.names = 75:100,class =data.frame)
这是我的代码加上结果
m base< -ggmap(m,extent ='device')
base + geom_point(aes(x = lon,y = lat,color = pricecut) ,size = 6,alpha = .7,data = pricedata)
这是我期望的结果然而,当我添加 coord_cartesian
事物变得奇怪
base + geom_point(aes(x = lon,y = lat ,color = pricecut),size = 6,alpha = .7,data = pricedata)+ coord_cartesian(xlim = c(-95,-80),ylim = c(38,50))
注意:我从
ggmap
call so中省略了 extent ='device'
你可以看到这个图中的边界是什么。 至于使用 coord_cartesian
,看起来像 coord_cartesian
以某种方式混淆了地图的比例。让我们从地图开始:
ggmap(m)
给出:
当您使用 scale_y_continuous
切片时:
<$ c
$($) >
你得到:
然而,当使用
coord_cartesian
进行类似的分片时:ggmap(m)+
geom_blank()+
coord_cartesian(ylim = c(38,50))
您可以:
正如您所看到的,地图在水平方向伸展,同时保持相同的高度。这导致地图垂直移动。当使用
scale_y_continuous
时,地图会保持正确的比例。因此,不是那些向上移动的点,而是向下移动的地图。As the title says when I add
coord_cartesian
to my ggmap it moves all of my points up. 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 strangebase+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 withscale_x_continuous
andscale_y_continuous
as follows: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 theggmap
call so you can see what the boundaries are in this plot.As with regard to the effect of using
coord_cartesian
, it seems thatcoord_cartesian
somehow messes with the ratios of the map. Let's start with just the map:ggmap(m)
gives:
When you slice this map with
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
: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. It's therefore not the points that get shifted upwards, but the map that gets shifted downwards.这篇关于ggmap,使用coord_cartesian将所有点推向北方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!